Search code examples
javascriptlowercasefor-in-loop

Several approaches to converting values to lowercase not working


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;
}

Solution

  • As another answer stated, toLowerCase returns the modified string so you have to do:

    value = value.toLowerCase()
    

    instead of just:

    value.toLowerCase()
    

    if you want that line to do anything.

    I'd suggest the whole thing to look something like:

    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){
            if (typeof kitchen[key] === 'string'){
                kitchen[key] = kitchen[key].toLowerCase()
            }
        }
        // Don't change the code below this line
        return kitchen;
    }