英文:
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<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
}
}
} 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('knife'), fork: utensils.indexOf('fork')}
}
<!-- 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(['knife', 'fork'].map(k => [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 {
"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));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论