简体   繁体   English

For 循环不能与比较运算符一起正常工作

[英]For Loops isn't working properly with comparison operators

Having trouble getting my code to log Scooby Doo!无法让我的代码登录 Scooby Doo! (as testing as I move forward in my code) Instead I always get the alert! (随着我在代码中前进的测试)相反,我总是收到警报!

I have the user entering data of numbers in an array eg.我让用户在数组中输入数字数据,例如。 [1 2 3 4] and I am trying to get the alert to pop up when someone puts extra spaces in the array to try again. [1 2 3 4]并且当有人在数组中放置额外的空间以重试时,我试图让警报弹出。

And if they haven't added extra spaces to log Scooby Doo!如果他们没有添加额外的空间来记录 Scooby Doo!

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!")
         }
    }

This expression is wrong and it will never be true:这个表达式是错误的,它永远不会是真的:

 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!")
  }

I've wrote your code using some features from ES6 to show you how easy is our life using these features.我已经使用 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" />

I hope it can inspire you =)希望对你有所启发 =)

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

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