As part of a practice challenge for a bootcamp, I’ve been asked to: ‘Fix the jumbled string values – replace them all with versions that are all lowercase’
I’ve tried several approaches based on answers on SO as well as other sites online but they all seem to result in different errors. My code is passing all the other tests but won’t pass this one.
I’ve tried reducing, mapping, and as seen in the code below for-in loops; so not really sure where I’m going wrong.
function sortTheKitchen(kitchen) {
delete kitchen.hoover
kitchen.totalShelves = (kitchen.shelvesInCupboards + kitchen.shelvesNotInCupboards) || kitchen.shelvesInCupboards || kitchen.shelvesNotInCupboards
delete kitchen.shelvesInCupboards && delete kitchen.shelvesNotInCupboards
for (let key in kitchen){
let value = kitchen[key]
if (typeof value === 'string'){
value.toLowerCase()
}
}
// Don't change the code below this line
return kitchen;
}
3
Answers
toLowerCase
returns lowercase string, you have to assign it to the desired variable. For example:In your scenario you’d have to do that:
Assigning new variable
value = kitchen[key];
would also made thekitchen[key]
not changing, because you would assign the lowercase string tovalue
, notkitchen[key]
.As another answer stated,
toLowerCase
returns the modified string so you have to do:instead of just:
if you want that line to do anything.
I’d suggest the whole thing to look something like:
If you need a pure solution you can use
Object.entries
andArray::reduce
to transform your kitchen to a new object: