当我向它推送一个新值时,有没有办法覆盖数组?

By simon at 2018-02-28 • 0人收藏 • 41人看过

也许我做错了,但我想更新我的阵列,以便他们 只需提交最新的内容即可。有没有办法做到这一点? 我似乎无法想到任何东西,推只是增加更多的数组 时间。

$( document ).ready(function() {


var divisTwo = [];
var divisThree = [];

function getNumbers(nums){
    // iterates through these to determine if fiz, buzz or fizzbuzz.
    if(nums % 105 === 0){
        return 'Fizz Buzz Pop'
    } else if (nums % 35 === 0){
        return 'Buzz Pop'
    } else if (nums % 21 === 0){
        return 'Fizz Pop'
    }  else if (nums % 15 === 0){
        return 'Fizz Buzz'
    } else if (nums % 7 === 0){
        return 'Pop'
    } else if(nums % 5 === 0 ){
        return 'Buzz'
    } else if(nums % 3 === 0){
        return  divisThree || 'Fizz'
    } else if (nums % 2 === 0){
        return divisTwo
    } else {
        return nums
    }
};
$('#click').click(function(){
    // Gets value from input box.
    var inputValue = document.getElementById("fizz-input").value;
    // appends text to an h2 in the html
    $("#fizz-buzz-output").text(getNumbers(inputValue));
});
$('#change-names').click(function test(name){
    var multiTwo = document.getElementById("multiples-of-two").value;
    var multiThree = document.getElementById("multiples-of-three").value;


    divisTwo.push(multiTwo);
    divisThree.push(multiThree);
    console.log(divisTwo);
    console.log(divisThree);
});
});

3 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

使用简单的变量而不是数组。

$( document ).ready(function() {


var divisTwo;
var divisThree;

function getNumbers(nums){
    // iterates through these to determine if fiz, buzz or fizzbuzz.
    if(nums % 105 === 0){
        return 'Fizz Buzz Pop'
    } else if (nums % 35 === 0){
        return 'Buzz Pop'
    } else if (nums % 21 === 0){
        return 'Fizz Pop'
    }  else if (nums % 15 === 0){
        return 'Fizz Buzz'
    } else if (nums % 7 === 0){
        return 'Pop'
    } else if(nums % 5 === 0 ){
        return 'Buzz'
    } else if(nums % 3 === 0){
        return  divisThree || 'Fizz'
    } else if (nums % 2 === 0){
        return divisTwo
    } else {
        return nums
    }
};
$('#click').click(function(){
    // Gets value from input box.
    var inputValue = document.getElementById("fizz-input").value;
    // appends text to an h2 in the html
    $("#fizz-buzz-output").text(getNumbers(inputValue));
});
$('#change-names').click(function test(name){
    divisTwo = document.getElementById("multiples-of-two").value;
    divisThree = document.getElementById("multiples-of-three").value;

    console.log(divisTwo);
    console.log(divisThree);
});
});

2018-02-28   #2

你必须将你的数组变量在函数内部清空,这样做 点击事件的每一次火灾,都是空的。也是不能使用流行音乐,因为它 只删除最后一个元素。

var divisTwo = [];
var divisThree = [];

$('#change-names').click(function(){

divisTwo = [];
divisThree = [];

var multiTwo = document.getElementById("multiples-of-two").value;
    var multiThree = document.getElementById("multiples-of-three").value;

    divisTwo.push(multiTwo);
    divisThree.push(multiThree);
    console.log(divisTwo);
    console.log(divisThree);
});

2018-02-28   #3

使用简单的变量而不是数组。

$( document ).ready(function() {


var divisTwo;
var divisThree;

function getNumbers(nums){
    // iterates through these to determine if fiz, buzz or fizzbuzz.
    if(nums % 105 === 0){
        return 'Fizz Buzz Pop'
    } else if (nums % 35 === 0){
        return 'Buzz Pop'
    } else if (nums % 21 === 0){
        return 'Fizz Pop'
    }  else if (nums % 15 === 0){
        return 'Fizz Buzz'
    } else if (nums % 7 === 0){
        return 'Pop'
    } else if(nums % 5 === 0 ){
        return 'Buzz'
    } else if(nums % 3 === 0){
        return  divisThree || 'Fizz'
    } else if (nums % 2 === 0){
        return divisTwo
    } else {
        return nums
    }
};
$('#click').click(function(){
    // Gets value from input box.
    var inputValue = document.getElementById("fizz-input").value;
    // appends text to an h2 in the html
    $("#fizz-buzz-output").text(getNumbers(inputValue));
});
$('#change-names').click(function test(name){
    divisTwo = document.getElementById("multiples-of-two").value;
    divisThree = document.getElementById("multiples-of-three").value;

    console.log(divisTwo);
    console.log(divisThree);
});
});

登录后方可回帖

Loading...