使用for循环javascript替换数组中的元素无法正确替换元素

时间:2018-03-11 17:33:25

标签: javascript arrays loops

我在这里有一个功能,它取代了3& 3倍数的元素。 5" Fizz"和" Buzz"分别。我运行了这个函数,它只用#34; Fizz"替换了第一个元素,没有别的。我不确定为什么它只替换第一个元素,因为它不匹配我拥有的任何if else语句。我不相信我需要在函数中包含数组,是吗,因为这意味着在我尝试替换元素时正在创建数组。

var listOfNumbers = new Array(100);

var i, j;
for (i = 0; i < listOfNumbers.length; i++){
    listOfNumbers[i] = i + 1;
}

function fizzBuzz(listOfNumbers){                
    for (j = 0; j < listOfNumbers.length; j++) {
        if (j % 3 == 0 && j % 5 == 0){
            listOfNumbers[j] = "FizzBuzz";
        }
        else if (j % 3 == 0) {
            listOfNumbers[j] = "Fizz";
        }
        else if (j % 5 == 0) {
            listOfNumbers[j] = "Buzz";
        }
        else {
            listOfNumbers[j] = j;
        }
        return listOfNumbers;
    }
    document.getElementById("Nums").innerHTML = fizzBuzz();
}

Array in the console.

我不确定

另外,我有一个显示数组的按钮,但点击它时似乎没有显示数组。

<h4>Fizz Buzz</h4>
<p>An idea that prints the elements of an array from 1 to 100, but it prints "Fizz" for every multiple of 3, "Buzz" for ever multiple of 5, and finally "FizzBuzz" for every multple of 15.
    Idea taken from <a href="https://github.com/karan/Projects#text">here</a>.</p>
    <button type="button" onclick="fizzBuzz()">Click to see the results</button>
    <p id="Nums"></p>

4 个答案:

答案 0 :(得分:2)

你需要

  • 交出数组,

  • 检查数组的值,而不是索引,因为它被移一,

  • 首先检查 FizzBu​​zz ,以获取35可分割的号码,

  • 在最后返回数组,

  • 另外声明一个局部变量j

function getFizzBuzz() {
    var listOfNumbers = new Array(100),
        i;

    for (i = 0; i < listOfNumbers.length; i++) {
        listOfNumbers[i] = i + 1;
    }
    document.getElementById("Nums").innerHTML = fizzBuzz(listOfNumbers);
}

function fizzBuzz(array) {
    var j;

    for (j = 0; j < array.length; j++) {
        if (array[j] % 3 == 0 && array[j] % 5 == 0) {
            array[j] = "FizzBuzz";
        } else if (array[j] % 3 == 0) {
            array[j] = "Fizz";
        } else if (array[j] % 5 == 0) {
            array[j] = "Buzz";
        }
    }
    return array;
}
<button onclick="getFizzBuzz()">FizzBuzz!!!</button><br><div id="Nums"></div>

答案 1 :(得分:1)

return语句在for循环中,这就是它在第一次迭代时返回的原因。请查看以下代码以获得澄清:

var listOfNumbers = new Array(100);

var i, j;
    for (i = 0; i < listOfNumbers.length; i++){
        listOfNumbers[i] = i + 1;
    }

function fizzBuzz(listOfNumbers){

for (j = 0; j < listOfNumbers.length; j++){
        if (j % 3 == 0){
            listOfNumbers[j] = "Fizz";
        }
        else if (j % 5 == 0){
            listOfNumbers[j] = "Buzz";
        }
        else{
            listOfNumbers[j] = j;
        } 
       
    }
  return listOfNumbers;
}
console.log(fizzBuzz(listOfNumbers));

答案 2 :(得分:0)

您可以使用map方法:

function fizzBuzz(listOfNumbers) {
    return listOfNumbers.map(x => {
        if(x % 3 == 0) x = 'Fizz';
        else if(x % 5 == 0) x = 'Buzz';
        return x;
    });
}

答案 3 :(得分:0)

编程大师如何使用JavaScript进行Fizz Buzz

<!DOCTYPE html>
<html>
<head>
    <title>Fizz Buzz</title>
</head>
<style type="text/css">

ol {
    width: 500px;
    line-height: 1.5em;

}
</style>
<body>
<h1>Fizz Buzz</h1>

<ol id="Nums">
</ol>
<script type="text/javascript">
var listOfNumbers = new Array(100);


function fizzBuzz(listOfNumbers){

for (j = 0; j < listOfNumbers.length; j++){
        if ((j + 1) % 3 == 0 && (j + 1) % 5 == 0){
            listOfNumbers[j] = "FizzBuzz";
        }
        else if ((j + 1) % 3 == 0){
            listOfNumbers[j] = "Fizz";
        }
        else if ((j + 1) % 5 == 0){
            listOfNumbers[j] = "Buzz";
        }
        else{
            listOfNumbers[j] = j + 1;
        } 

    }
  return listOfNumbers;
}

function printer(list){
    fizzBuzz(listOfNumbers);
    for (var i = 0; i < 100; i++){
        var item = document.createElement("li");
        var text = document.createTextNode(listOfNumbers[i]);
        item.appendChild(text);
        var list = document.getElementById("Nums");
        list.appendChild(item);
    }
}
printer();
</script>
</body>
</html>