Javascript reads only first elements(from array) in nested for loops
我有以下问题。我得解决一个问题。
Fruit or Vegetable Write a JS function to print"fruit" ,"vegetable"
or"unknown" depending on the input string.
Fruits are: banana, apple, kiwi, cherry, lemon, grapes, peach
Vegetable are: tomato, cucumber, pepper, onion, garlic, parsley
All others are unknown
The input comes as array of one string element, the name of the
fruit. The output should be printed to the console.Example: input ['banana'] output: fruit
Example: input ['cucumber'] output: vegetable
Example: input ['pizza'] output: unknown
号
我也试过类似的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; for (var i = 0; i < inputArr.length; i++) { for (var j = 0; j < fruits.length; j++) { for (var k = 0; k < vegetables.length; k++) { if (inputArr[i] === fruits[j]) { return ' fruit '; } else if (inputArr[i] === vegetables[k]) { return 'vegetable'; } else { return 'unknown'; } } } } } console.log(fruitOrVegetable(['tomato'])); //Returns vegetable console.log(fruitOrVegetable(['banana'])); //Returns fruit console.log(fruitOrVegetable(['cucumber'])); //Returns unknown console.log(fruitOrVegetable(['pizza'])); // Returns unknown console.log(fruitOrVegetable(['appple'])); //Returns unknown |
不知道为什么,但它只适用于数组的0索引,例如,对于"番茄",它返回蔬菜,但如果我尝试其他蔬菜,它返回未知。如果我去掉最后一句话
1 2 3 | else{ return false; } |
号
然后,黄瓜变成蔬菜,但苹果得不到定义?我有点困惑,所以如果有人向我解释为什么会发生这种事,我会很高兴的。谢谢您。
你在那里做了一个巨大的不必要的嵌套
The input comes as array of one string element, the name of the fruit.
The output should be printed to the console.
号
您只需获取第一个
比如,
1 2 3 4 5 6 7 8 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; var val = inputArr[0]; return fruits.indexOf(val) !== -1 ? 'fruit' : vegetables.indexOf(val) !== -1 ? 'vegetables' : 'unknown'; } |
号
编辑:
让我们打破上面的代码,
1 2 | // This is to grab the string from the first value in `inputArr` and store in `val`. var val = inputArr[0]; |
然后回来,
1
fruits.indexOf(val) !== -1 ? 'fruit' :2
vegetables.indexOf(val) !== -1 ? 'vegetables' : 3
'unknown';
号
这个运算等于,
1 2 3 4 5 6 7 8 9 | if (fruits.indexOf(val) !== -1) { return 'fruit'; } else if (vegetables.indexOf(val) !== -1) { return 'vegetables'; } else { return 'unknown'; } |
。
更多信息:
例如
1 2 3 4 5 | var myval = 1; console.log(myval ? 'true' : 'false'); // true myval = false; console.log(myval ? 'true' : 'false'); // false |
例如
1 2 3 | var myarr = ['test', 'tost', 'aww']; console.log(myarr.indexOf('tost')); // returns 1 console.log(myarr.indexOf('tosts')); // returns -1 (value not found) |
。
建议:
检查当前项目是否为水果,只有确保它不是水果时,才检查它是否为蔬菜。
只有在确认当前项目既不是水果也不是蔬菜之后,才返回
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; var result = []; for (var i = 0; i < inputArr.length; i++) { var isFruit = false; var isVegetable = false; for (var j = 0; j < fruits.length; j++) { if (inputArr[i] === fruits[j]) { result.push('fruit'); isFruit = true; break; } } if (isFruit) { // skip rest of the body of the loop as it is a Fruit continue; } for (var j = 0; j < vegetables.length; j++) { if (inputArr[i] === vegetables[j]) { result.push('vegetable'); isVegetable = true; break; } } if (isVegetable === false) { // at this point we know its neither a fruit nor a vegetable result.push('unknown'); } } return result; } console.log(fruitOrVegetable(['tomato', 'banana', 'cucumber', 'pizza', 'apple'])); // [ 'vegetable', 'fruit', 'vegetable', 'unknown', 'fruit' ] |
。
用
JavaScript数组有很多内置的方法,这将有助于基本的排序和搜索。可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; var result = []; for (var i = 0; i < inputArr.length; i += 1) { if (fruits.indexOf(inputArr[i]) !== -1) { result.push('fruit'); } else if (vegetables.indexOf(inputArr[i]) !== -1) { result.push('vegetable'); } else { result.push('unknown'); } } return result; } console.log(fruitOrVegetable(['tomato', 'banana', 'cucumber', 'pizza', 'apple'])); // [ 'vegetable', 'fruit', 'vegetable', 'unknown', 'fruit' ] |
用
现在,您可以用函数编程方法编写相同的代码。您可以对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; return inputArr.map(function(currentItem) { if (fruits.indexOf(currentItem) !== -1) { return 'fruit'; } else if (vegetables.indexOf(currentItem) !== -1) { return 'vegetable'; } return 'unknown'; }); } console.log(fruitOrVegetable(['tomato', 'banana', 'cucumber', 'pizza', 'apple'])); // [ 'vegetable', 'fruit', 'vegetable', 'unknown', 'fruit' ] |
。
简短回答:你把
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; for (var i = 0; i < inputArr.length; i++) { for (var j = 0; j < fruits.length; j++) { for (var k = 0; k < vegetables.length; k++) { if (inputArr[i] === fruits[j]) { return ' fruit '; } else if (inputArr[i] === vegetables[k]) { return 'vegetable';} } } } return"unknown"; } |
根据评论更新答案
Don't know why , but it works only for the 0 index of the array
号
原因是您正在循环内部返回值。所以它永远不会走到尽头。为了解决这个问题,把
简化代码以避免与之混淆是一个很好的实践。例如,你可以写如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | function isVegetable(inputArr) { var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; for (var i = 0; i < inputArr.length; i++) { for (var k = 0; k < vegetables.length; k++) { if (inputArr[i] === vegetables[k]) { return true; } } } return false; } function isFruit(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; for (var i = 0; i < inputArr.length; i++) { for (var k = 0; k < fruits.length; k++) { if (inputArr[i] === fruits[k]) { return true; } } } return false; } function fruitOrVegetable(inputArr) { if(isVegetable(inputArr)) return"vegetable"; if(isFruit(inputArr)) return"fruit"; return"unknown"; } |
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; if(inputArr === undefined || !inputArr.length) return 'unknown'; if(fruits.indexOf(inputArr[0]) !== -1) return 'fruit'; if(vegetables.indexOf(inputArr[0]) !== -1) return 'vegetable'; return 'unknown'; } console.log(fruitOrVegetable(['banana'])); console.log(fruitOrVegetable(['tomato'])); console.log(fruitOrVegetable(['xxx'])); console.log(fruitOrVegetable([])); console.log(fruitOrVegetable()); |
像这样修改代码,它将工作文件。
功能果蔬(inputar){
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | var fruits = ['banana' , 'apple' , 'kiwi' , 'cherry' , 'lemon' , 'grapes' , 'peach']; var vegetables = ['tomato' , 'cucumber' , 'pepper' , 'onion' , 'garlic' , 'parsley']; for(var i = 0 ; i < inputArr.length ; i++) { for(var j = 0 ; j < fruits.length ; j++) { if(inputArr[i] === fruits[j]){ return ' fruit '; } } for(var k = 0 ; k < vegetables.length ; k++) { if(inputArr[i] === vegetables[k]){ return 'vegetable'; } } } return 'unknown'; } |
号
结果:
水果或蔬菜([‘番茄’);"蔬菜"水果蔬菜([‘香蕉’);"水果"
果蔬([‘qwe’);"未知"
如果比你更可取的话,你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 | function fruitOrVegetable(inputArr) { var fruits = ['banana', 'apple', 'kiwi', 'cherry', 'lemon', 'grapes', 'peach']; var vegetables = ['tomato', 'cucumber', 'pepper', 'onion', 'garlic', 'parsley']; if ( fruits.indexOf( inputArr[0] ) > -1 ){ return 'fruits'; }else if ( vegetables.indexOf( inputArr[0] ) > -1 ){ return 'vegetable'; }else{ return 'unknown'; } } |
号
首先,
下一件事是不要在彼此之间嵌套太多循环。相反,在大的for循环中单独使用check来检查蔬菜或水果:
抱歉,无法完成代码,因为我正在从电话中写入