无法使用JavaScript中的用于循环和单独变量的方法,将一个数组的元素设置为与来自单独数组的元素等效的元素

问题描述

我是JS新手,所以我不是100%意识到与程序相关的所有不同功能方法。我想用我的一个数组中的一个元素替换另一个数组中的元素,以用于我正在开发的支票编写程序。这是我当前拥有的代码

    loop = true
    units = ['zero ','one ','two ','three ','four ','five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
    tens = ['','','twenty ','thirty ','fourty ','fifty ','sixty ','seventy ','eighty ','ninety '];
    bigs = ['','hundred ','thousand ','million ','billion '];
    while (loop) {
        userNum = prompt("Enter your money quantity to 2 decimal places \neg: 123.45\n      100\n      3336745.90");
        if (userNum == parseInt(userNum) || userNum == (Math.floor(userNum * 100) / 100).toFixed(1) || userNum == (Math.floor(userNum * 100) / 100).toFixed(2)) {
            loop = false;
        } else {
            alert("That is not a valid number.");
        }
    }
    userNum = userNum.toString().split("");

    if (userNum.includes(".") == true) {
        a = userNum.indexOf(".");
        userNum[a] = "and";
    }

    for (i = 0; i > userNum.length; i++);
    if (Number.isInteger(userNum.lastIndexOf())) {
        num = userNum.indexOf(i);
        userNum.indexOf(i) = units.indexOf(num);
        console.log(userNum.indexOf(i));
    }
    alert(userNum);

我想将“ userNum”的每个元素替换为“ units”中的相应元素,以便我的代码将每个数字作为单词在警报语句中输出到控制台。例如。 76.65变成了七,六,六,五。当前,它仅输出数字并将小数点转换为ands,这使我相信该代码只是跳过了整个代码块。我的语法不正确吗,还是改变元素的方式错误? (顺便说一句,如果影响到任何事情,我正在使用VSCode)

解决方法

代码中有几个问题:

  1. userNum已经是一个字符串,无需将其转换为字符串。

  2. 您的for循环条件错误,应小于nums.length i < userNum.length 在for循环的末尾有分号(;),这是错误的

  3. lastIndexoOf(),方法需要该值来搜索必需的参数,语法为

    string.lastIndexOf(searchvalue,start)

  4. userNum.indexOf(i) = units.indexOf(num);。这里的作业有问题。

  5. 此外,您直到19为止都不需要单位数组,零-9很好。

经过修改的有效解决方案:

var loop = true
var userNum ;

var units = {};

units[0]="zero";
units[1]="one";
units[2]="two";
units[3]="three";
units[4]="four";
units[5]="five";
units[6]="six";
units[7]="seven";
units[8]="eight";
units[9]="nine";
    while (loop) {
        userNum = prompt("Enter your money quantity to 2 decimal places \neg: 123.45\n      100\n      3336745.90");
        if (userNum == parseInt(userNum) || userNum == (Math.floor(userNum * 100) / 100).toFixed(1) || userNum == (Math.floor(userNum * 100) / 100).toFixed(2)) {
            loop = false;
        } else {
            alert("That is not a valid number.");
        }
    }
    userNum = userNum.split("");

    if (userNum.includes(".")) {
        a = userNum.indexOf(".");
        userNum[a] = "and";
    }

    for (i = 0; i < userNum.length; i++){
    if(userNum[i]!=='and'){
        userNum[i] = units[userNum[i]];
        console.log(userNum[i]);
       } 
    }
    alert(userNum);

,

首先,我想指出您的代码中的一些错误:-

  1. 在for循环后您有一个;。您必须将for方法之外的所有alert下的语句都放在花括号内。
  2. indexOf方法返回数组内提供的元素的索引。因此,您不需要它。
  3. 您的if条件是错误的,因为userNum数组中的所有元素都已经是字符串,因此它将始终返回false。
  4. 也将是i < userNum.length循环中的for

我已经编辑了代码。您可以从这里引用它:-

loop = true
units = ['zero ','one ','two ','three ','four ','five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
tens = ['','','twenty ','thirty ','fourty ','fifty ','sixty ','seventy ','eighty ','ninety '];
bigs = ['','hundred ','thousand ','million ','billion '];
while (loop) {
    userNum = prompt("Enter your money quantity to 2 decimal places \neg: 123.45\n      100\n      3336745.90");
    if (userNum == parseInt(userNum) || userNum == (Math.floor(userNum * 100) / 100).toFixed(1) || userNum == (Math.floor(userNum * 100) / 100).toFixed(2)) {
        loop = false;
    } else {
        alert("That is not a valid number.");
    }
}
userNum = userNum.toString().split("");

if (userNum.includes(".") == true) {
    a = userNum.indexOf(".");
    userNum[a] = "and";
}

for (i = 0; i < userNum.length; i++){
    if (userNum[i] !== "and") {
        num = parseInt(userNum[i]);
        userNum[i] = units[num];
    } 
}
alert(userNum);