小编San*_*oza的帖子

在 JavaScript 上使用 for 重复函数 5 次

我试图用不同的输入重复一个函数 5 次。问题是,该函数在 for 循环之外可以正常工作,但在它内部它只能工作 1 次。这是我的代码:

    var string;
var num;
function comandos(string, num){

    let resultado = "";

    for (i=0; i<string.length; i++){

        if (string.charAt(i) == "i"){
            num = num + 1;
        }else if (string.charAt(i) == "d"){
            num = num - 1;
        }else if (string.charAt(i) == "c"){
            num = Math.pow(num, 2);
        }else if (string.charAt(i) == "p"){
            resultado = resultado + "*" + num + "*";
        }
    }

return resultado;

}

for (i=0; i<5; i++){
    string = prompt("Ingrese secuencia de comandos (i, …
Run Code Online (Sandbox Code Playgroud)

javascript loops for-loop function

1
推荐指数
1
解决办法
93
查看次数

标签 统计

for-loop ×1

function ×1

javascript ×1

loops ×1