【问题标题】:For Loops isn't working properly with comparison operatorsFor 循环不能与比较运算符一起正常工作
【发布时间】:2021-04-14 00:33:14
【问题描述】:

无法让我的代码登录 Scooby Doo! (正如我在代码中前进时进行的测试)相反,我总是收到警报!

我让用户在数组中输入数字数据,例如。 [1 2 3 4] 我正在尝试在有人在数组中添加额外空格以重试时弹出警报。

如果他们没有添加额外的空间来记录史酷比!

document.getElementById("button").addEventListener("click", myExcelFuns);

function myExcelFuns() {
    var userInputStr = document.getElementById("numbers").value;

    if (userInputStr) {
        console.log(userInputStr);
        userInputStr = userInputStr.trim();
        let userNumberArray = userInputStr.split(" ");
        console.log(userNumberArray);
        let result;


        let newArray = [];
        for (let i = 0; i < userNumberArray.length; i++) {  
            let newArrayValues = parseInt(userNumberArray[i]);
            newArray.push(newArrayValues);
        }    
        console.log(newArray);    

        //Here is the trouble
        let finalArray = [];
        if (newArray === Number && newArray != ""){
            for (let i = 0; i < newArray.length; i++) {
                console.log("Scooby Doo");
                finalArray.push(newArray);
            }
                
         } else {
             alert("Only one space between numbers please!")
         }
    }

【问题讨论】:

  • newArray === Number 永远不会是真的
  • 这是为什么呢?你看到解决方案了吗?
  • newArray != "" 是一种检查数组是否为空的奇怪(和错误)方式。确保您要比较的变量的类型相同。这个条件if (newArray === Number &amp;&amp; newArray != "") 询问一个数组是否等于一个函数,而一个数组是否不等于一个字符串。始终使用===!==
  • @DarioCharles newArray 是一个数组,而 Number 是一个函数。我不确定你打算用这个表达式做什么,所以不,我没有看到解决方案
  • 我不确定为什么要检查中间的多个空格,而您可以轻松地执行 userInputStr.split(/\s+/) 这将忽略多个空格并更加用户友好,但您可以执行 let multiSpaces = userInputStr.match(/\s\s+/); if (multiSpaces) console.log('Multiple spaces detected')

标签: javascript arrays for-loop


【解决方案1】:

这个表达式是错误的,它永远不会是真的:

 if (newArray === Number && newArray != ""){


 if (newArray === Number && newArray != ""){
     for (let i = 0; i < newArray.length; i++) {
          console.log("Scooby Doo");
     // Here you'll push whole newArray everytime it runs, I guess it's wrong as well
          finalArray.push(newArray); 
      }
                
  } else {
      alert("Only one space between numbers please!")
  }

我使用 ES6 中的一些特性编写了你的​​代码,向你展示了使用这些特性是多么容易。

//Here you must pass a valid Id
document.getElementById("myButton").addEventListener("click", myExcelFuns);

function myExcelFuns() {
    var userInputStr = document.getElementById("numbers").value;

    if (userInputStr) {
      
        userInputStr = userInputStr.trim();
        let userNumberArray = userInputStr.split(" ");
      
        //check if some value into arrya is not a number
        const isThereSomeInvalidNumber = userNumberArray.some(x => isNaN(x))
        
        if(isThereSomeInvalidNumber){
          alert("Only one space between numbers and valid numbers please!");
          //here if it's invalid you can clear the input
          document.getElementById("numbers").value = "";
        }

        //map runs into your array and return a new array
        const newArray = userNumberArray.map(x => {
          return parseInt(x)
        })

        console.log(newArray);
    }

}
<button id="myButton">
        click me
    </button>
    <input type="text" id="numbers" />

希望对你有所启发 =)

【讨论】:

  • 键:===(三个 ...)表示“完全等于。”这意味着不仅解释值而且数据类型必须相同。这里不是。
猜你喜欢
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多