使用JavaScript返回数组中刀和叉的索引位置。

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

Returning object with index positions of knife and fork in array using JavaScript

问题

将下面的代码添加到你的函数中,以在刀子或叉子不在数组中时显示-1:

if (knife === 0) {
    obj.knife = -1;
}

if (fork === 0) {
    obj.fork = -1;
}

你的函数应该像这样:

function findKnifeAndFork(utensils) {
    console.log(utensils)
    let knife = 0;
    let fork = 0;
    let obj = {};

    for(let i = 0; i < utensils.length; i++){
         if(utensils[i] === 'knife'){
            knife = i
            obj.knife = knife;    
        }
    }

    for(let i = 0; i < utensils.length; i++){
        if(utensils[i] === 'fork'){
            fork = i
            obj.fork = fork;
        }
    }

    if (knife === 0) {
        obj.knife = -1;
    }

    if (fork === 0) {
        obj.fork = -1;
    }

    return obj;
}

这将确保如果刀子或叉子不在输入数组中,它们的属性值将是-1。

英文:

could you see where I would add code to display -1 if knife or fork is not present in the array?

This is the question I am trying to solve - The function's return value should be an object with two properties, knife and fork, which contain the index positions of the knife and fork from the input array. If either the knife and/or fork are not present in the input array, that property's value should be -1.

    function findKnifeAndFork(utensils) {
    console.log(utensils)
    let knife = 0;
    let fork = 0;
    let obj ={};

    for(let i = 0; i&lt;utensils.length; i++){
         if(utensils[i] === &#39;knife&#39;){
            knife = i
            obj.knife=knife    
    };

    for(let i = 0; i&lt;utensils.length; i++){
    if(utensils[i] === &#39;fork&#39;){
    fork = i
    obj.fork=fork    
    }
    
    } 
    }   return obj
    }

I have added it within each for loop as an else and outside both loops as a standalone for loop and neither are working. ( the below is currently working to display the index code of knife and fork - when they are both present in the array)

答案1

得分: 0

你的代码看起来没问题,除了需要在 obj 中最初将刀和叉设置为 -1,以处理数组中没有找到的情况。

indexOf 方法如果找不到匹配项就会返回 -1,所以你可以用这样的一行代码来实现你的目标:

function findKnifeAndFork(utensils) {
  return {knife: utensils.indexOf('knife'), fork: utensils.indexOf('fork')}
}

为了避免一些重复,你也可以这样做:

function findKnifeAndFork(utensils) {
  return Object.fromEntries(['knife', 'fork'].map(k => [k, utensils.indexOf(k)]))
}
英文:

Your code looks fine, except that you need to initially set knife and fork to -1 in obj to cover the case when either is not found in the array.

The indexOf method already returns -1 if no match is found, and so you can achieve your goal with a one-liner like this:

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

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

function findKnifeAndFork(utensils) {
  return {knife: utensils.indexOf(&#39;knife&#39;), fork: utensils.indexOf(&#39;fork&#39;)}
}

<!-- end snippet -->

To avoid some repetition, you can also do:

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

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

function findKnifeAndFork(utensils) {
  return Object.fromEntries([&#39;knife&#39;, &#39;fork&#39;].map(k =&gt; [k, utensils.indexOf(k)]))
}

<!-- end snippet -->

答案2

得分: 0

indexOf是专门为这种情况设计的。如果使用它,您可以简化很多内容,并避免循环。

function findKnifeAndFork(utensils) {
  return {
    "fork": utensils.indexOf("fork"),
    "knife": utensils.indexOf("knife")
  }
}

let onlyFork = ["fork", "spoon", "ladle", "hammer"];
console.log(findKnifeAndFork(onlyFork));

let onlyKnife = ["screwdriver", "spoon", "knife", "hammer"];
console.log(findKnifeAndFork(onlyKnife));

let forkAndKnife = ["screwdriver", "spoon", "knife", "hammer", "fork"];
console.log(findKnifeAndFork(forkAndKnife));
英文:

indexOf is purpose-built for this sort of thing. You can simplify quite a bit if you use it... and avoid looping.

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

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

function findKnifeAndFork(utensils) {
  return {
    &quot;fork&quot;: utensils.indexOf(&quot;fork&quot;),
    &quot;knife&quot;: utensils.indexOf(&quot;knife&quot;)
  }
}

let onlyFork = [&quot;fork&quot;, &quot;spoon&quot;, &quot;ladle&quot;, &quot;hammer&quot;];
console.log(findKnifeAndFork(onlyFork));

let onlyKnife = [&quot;screwdriver&quot;, &quot;spoon&quot;, &quot;knife&quot;, &quot;hammer&quot;];
console.log(findKnifeAndFork(onlyKnife));

let forkAndKnife = [&quot;screwdriver&quot;, &quot;spoon&quot;, &quot;knife&quot;, &quot;hammer&quot;, &quot;fork&quot;];
console.log(findKnifeAndFork(forkAndKnife));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 05:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402507.html
匿名

发表评论

匿名网友

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

确定