mardi 15 mai 2018

Push Method Is Appending Value Of The Index To Number Array?

I've created a randomly generated array, and made a button that uses .push method to append another randomly generated number. However the number it appends to the array seems to have the index number appended to it also. For example: the button with value "randArrayGen" prints to the page (let's say) : 12,45,23,77,54,67 but then the button w/ value "pusher" which should append a value with no more than two integers appends (let's say): 12,45,23,77,54,67,897 every time i press "pusher" it adds a digit to the array with what seems like it must be the index value for each succesive random value like so : 12,45,23,77,54,67,897, 239,3510,5511. what's going on and how do I prevent it from happening?

<html>
<head>
<Title>rand list</Title>
<script>
var x;
var x1,x2,x3,x4,x5,x6;
    x1 = Math.floor(100*Math.random());
    x2 = Math.floor(100*Math.random());
    x3 = Math.floor(100*Math.random());
    x4 = Math.floor(100*Math.random());
    x5 = Math.floor(100*Math.random());
    x6 = Math.floor(100*Math.random());
var randomArray = [x1,x2,x3,x4,x5,x6];

function randomNumGenerator(){
    x = Math.floor(100*Math.random());
    document.getElementById('xout').innerHTML = x;
}
function randArrayGenerator(){   
document.getElementById("arrayout").innerHTML = randomArray;
}
function pusher(){
    var pushed = randomArray.push(Math.floor(100*Math.random()));
    document.getElementById('arrayout').innerHTML = randomArray + pushed; 
}

function sorter(){
   var sorted = randomArray.sort(function(a,b){
    return a-b;})
    document.getElementById('arrayout2').innerHTML = sorted;

}
</script>
</head>

<body>
    <p>

        <input type = "button" value = "randgen" onclick = "randomNumGenerator();">
<div id = 'xout'></div>
    </p>
<hr>
    <input type="button" value = "randArraygen" onclick ="randArrayGenerator();">
    <input type="button" value = "pusher" onclick ="pusher();">
        
<div id = "arrayout"></div>
<hr>
    <input type="button" value="sortlist" onclick = "sorter();">
<div id = "arrayout2"></div>    
    
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire