将值转换为小写的几种方法未能正常工作。

huangapple go评论67阅读模式
英文:

Several approaches to converting values to lowercase not working

问题

以下是翻译好的代码部分:

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();
        }
    }
    // 不要更改下面这行以下的代码
    return kitchen;
}
英文:

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

答案1

得分: 0

toLowerCase 返回小写字符串,您必须将其分配给所需的变量。例如:

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

在您的情况下,您需要这样做:

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

分配新变量 value = kitchen[key]; 也不会改变 kitchen[key],因为您会将小写字符串分配给 value,而不是 kitchen[key]

英文:

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

答案2

得分: 0

Here is the translated content:

正如另一个回答所述,toLowerCase 返回修改后的字符串,所以你需要执行以下操作:

value = value.toLowerCase()

而不仅仅是:

value.toLowerCase()

如果你想让那行代码有作用的话。

我建议整个函数看起来像这样:

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()
        }
    }
    // 不要更改以下这行代码
    return kitchen;
}
英文:

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

答案3

得分: 0

如果您需要一个纯净的解决方案,您可以使用 Object.entriesArray::reduce 来将您的厨房转换成一个新的对象:

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

If you need a pure solution you can use Object.entries and Array::reduce to transform your kitchen to a new object:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const kitchen = {
  hoover: &#39;test&#39;,
  shelvesNotInCupboards: 1,
  shelvesInCupboards:1,
  title: &#39;Kitchen&#39;,
  cool: true,
};

const kitchenTransformed = sortTheKitchen(kitchen);
console.log(kitchenTransformed);

function sortTheKitchen(kitchen) {
    const setSum = new Set([&#39;shelvesInCupboards&#39;, &#39;shelvesNotInCupboards&#39;]);
    const setRemove = new Set([&#39;hoover&#39;]);
    return Object.entries(kitchen).reduce((kitchen, [key,val]) =&gt; {
      if(setSum.has(key)){
        kitchen.totalShelves ? kitchen.totalShelves += val : kitchen.totalShelves = val;
      }else if(setRemove.has(key)){
        //skip
      }else if(typeof val === &#39;string&#39;){
        kitchen[key] = val.toLowerCase();
      }else{
        kitchen[key] = val;
      }
      return kitchen;
   }, {});
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月4日 20:42:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定