skip to Main Content

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


  1. toLowerCase returns lowercase string, you have to assign it to the desired variable. For example:

    let str = "Some String";
    str = str.toLowerCase();
    

    In your scenario you’d have to do that:

    if (typeof kitchen[key]=== 'string'){
      kitchen[key] = kitchen[key].toLowerCase()
    }
    

    Assigning new variable value = kitchen[key]; would also made the kitchen[key] not changing, because you would assign the lowercase string to value, not kitchen[key].

    Login or Signup to reply.
  2. 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;
    }
    
    Login or Signup to reply.
  3. If you need a pure solution you can use Object.entries and Array::reduce to transform your kitchen to a new object:

    const kitchen = {
      hoover: 'test',
      shelvesNotInCupboards: 1,
      shelvesInCupboards:1,
      title: 'Kitchen',
      cool: true,
    };
    
    const kitchenTransformed = sortTheKitchen(kitchen);
    console.log(kitchenTransformed);
    
    function sortTheKitchen(kitchen) {
        const setSum = new Set(['shelvesInCupboards', 'shelvesNotInCupboards']);
        const setRemove = new Set(['hoover']);
        return Object.entries(kitchen).reduce((kitchen, [key,val]) => {
          if(setSum.has(key)){
            kitchen.totalShelves ? kitchen.totalShelves += val : kitchen.totalShelves = val;
          }else if(setRemove.has(key)){
            //skip
          }else if(typeof val === 'string'){
            kitchen[key] = val.toLowerCase();
          }else{
            kitchen[key] = val;
          }
          return kitchen;
       }, {});
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search