maximusmd
maximusmd

Reputation: 23

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

Upvotes: 0

Views: 357

Answers (3)

Alexander Nenashev
Alexander Nenashev

Reputation: 23607

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;
   }, {});
}

Upvotes: 0

Adrian Kokot
Adrian Kokot

Reputation: 3246

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].

Upvotes: 0

Yoan Dzhelekarski
Yoan Dzhelekarski

Reputation: 76

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

Upvotes: 0

Related Questions