i was asked to write out a piece of code that inverts all key value pairs from an object passed in. this is what i wrote out.
invert(object){
let newObj = {};
for(let key in object){
const original = object[key];
newObj = {original : key}
}
return newObj;
}
aparently that dosent work ( and i cant figure out why). the answer to the question was
invert(object){
let invertedObject = {};
for(let key in object){
const originalValue = object[key];
invertedObject = {originalValue : key}
}
return invertedObject;
}
when ran with with a test file, my code fails while the answer code passes. reasoning was mine is returning undefined.
error code from test :
Failed: _.invert({originalKey: "originalValue"})["originalValue"]) returned undefined instead of originalKey