Trying to create a function that does not allow an object to crate a new key if the value of the key has already been used.
So the object cannot have name = crystal and favoriteRock = crystal Once value has been declared, no other key within the object can have that value.
Not sure why I keep getting undefined
let keyAdderUniqueVal = function (object, key, value) {
let allvalues = Object.values(object)
if (!allvalues.includes(value)) {
object[key] = value
} else {
return object
}
}
This is what a working code is suppose to do
let horse = { name: 'Spirit', color: 'brown' };
keyAdderUniqueVal(horse, "food", "carrot"); // => {name: "Spirit", color: "brown", food: "carrot"}
keyAdderUniqueVal(horse, "hair", "brown"); // => {name: "Spirit", color: "brown", food: "carrot"}
console.log(horse); // { name: "Spirit", color: "brown", food: "carrot" }