简体   繁体   English

为什么我的getColor函数总是返回false?

[英]Why does my getColor function always return false?

I have a function that gets user input and its supposed to validate the user input from a list of options. 我有一个获取用户输入的函数,它应该从选项列表中验证用户输入。 The last part of the function, where I cycle through results and return true, if there's a true boolean value in results doesn't seem to be working properly. 函数的最后一部分,如果在结果中存在真正的布尔值,我将遍历结果并返回true,这似乎无法正常工作。 It keeps returning false. 它不断返回false。

    function getColor(){
        //gets user color and makes all characters lower case;
        let color=prompt("Pick a color, your options are white, yellow, brown, black, tan, red, orange").toLowerCase();

        //stores acceptable colors
        let acceptableColors=["white","yellow", "brown", "black","tan","red","orange"];

        function validate(){
            let results=[];
            for(let i=0; i<acceptableColors.length; i++){//loops through and stores a true value in results if user color is in list of acceptableColors
                    if(color===acceptableColors[i]){
                        results.push(true);
                    }
                    else{
                        results.push(false);
                    }

            }

            results.forEach(function(item){//loops through results to search for true value and returns true, if it finds one
                if(item===true){
                    return true
                }
            });
             return false;//returns false if user entered invalid color

        }
        return validate();

    }

It keeps returning false. 它不断返回false。

Return false inside forEach will not return from getColor and your return false is unconditional forEach内部的Return false不会从getColor返回, 并且false是无条件的

Make it 做了

    return results.some(function(item){
       return item;
    });

Here is a simple way: 这是一个简单的方法:

function validate(){
        let results=[];
        for(let i=0; i<acceptableColors.length; i++){//loops through and stores a true value in results if user color is in list of acceptableColors
                if(color===acceptableColors[i]){
                    return true;
                }    
        }
        return false;//returns false if user entered invalid color

    }

I would prefer doing this way 我宁愿这样

 function validate() { return acceptableColors.indexOf(color) !== -1; } 

Your whole function can be simplified. 您的整个功能可以简化。 There's no need to push booleans in an array, and then check that array for truthy values. 无需在数组中推送布尔值,然后在该数组中检查真实值。 Just use includes to check if the color array includes the user's prompted color. 只需使用includes即可检查颜色数组是否包含用户的提示颜色。

function getColor() {
  let color = prompt("Pick a color, your options are white, yellow, brown, black, tan, red, orange").toLowerCase();
  let acceptableColors = ["white", "yellow", "brown", "black", "tan", "red", "orange"];
  return acceptableColors.includes(color);
}

getColor();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM