简体   繁体   English

Javascript创建一个基于某些参数返回布尔值的函数

[英]Javascript create a function that returns a boolean value based on certain parameters

Thanks for taking the time to look at my problem. 感谢您抽出宝贵的时间来研究我的问题。 What I'm trying to do is create a javascript function that tests whether a sting is a particular length and also whether each element of that string can be found in another string. 我想做的是创建一个JavaScript函数,该函数测试字符串是否为特定长度,以及是否可以在另一个字符串中找到该字符串的每个元素。 The function then needs to return a boolean value of either true or false depending on whether the string is valid. 然后,该函数需要返回true或false的布尔值,具体取决于字符串是否有效。

Here's what I have: 这是我所拥有的:

N_ALPHA = 6;
N_CHOICES = 4;
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var alphabet = ALPHABET.substring(0, N_ALPHA);

function isValidGuess(inStr)
{    var valid;
     var Str = inStr;

     for (i=0; i<Str.length; i++)
     {    if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES.length)
          {    valid = true;
          }
          else
          {    valid = false;
          }
     }  

     return valid;
}

This code is not working at all. 此代码根本不起作用。 It only returns false every time. 每次仅返回false。 Any help you could provide would be greatly appreciated. 您能提供的任何帮助将不胜感激。 Thank you. 谢谢。

N_CHOICES.length return undefined, because variable N_CHOICES is number. N_CHOICES.length返回未定义,因为变量N_CHOICES是数字。 you have to change your condition to 您必须将条件更改为

if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES)

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

相关问题 调用返回布尔值的 JavaScript 函数 - Calling a JavaScript function that returns a Boolean 创建一个 function 在 React 项目中返回一个 boolean - Create a function which returns a boolean in React project Function 以数字作为参数并返回 boolean javascript - Function that takes a number as an argument and returns a boolean javascript 在javascript函数中切换传入变量布尔值 - toggle passed in variable boolean value in javascript function 当被问到“这是一个质数吗?”时,需要创建一个返回布尔值的函数。 - Need to create function that returns a boolean when asked, "Is this a prime number?" 使用function根据JS中另一个function中的Boolean值改变颜色 - Using function to change color based on Boolean value in another function in JS 如何设计一个接受两个布尔参数并在其中一个(或两个)参数为真时返回真的函数? - How to design a function that takes two Boolean parameters and returns true if either (or both) of the parameters are true? SQL如何基于另一个列的值创建一个布尔列 - SQL how create a Boolean column based on the value of another column 基于布尔值的 if 语句 - If statement based on Boolean Value 多个布尔变量作为函数参数 - Multiple boolean variables as function parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM