This is probably really basic but I keep getting strange output when printing an array, it keeps putting double commas in between values in the arrays. I know it's something simple but I've been at this for a couple of hours now and no joy. You can see at the end I've tried a couple of things but end up with a weird double comma.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to run the digit checker.</p> <!--Text telling the user to hit the button-->
<button onclick="myDigitChecker()" >Submit</button> <!--button that runs my function once they hit-->
<p id="demo"></p>
<p id="demo2"></p>
<script>
function myDigitChecker() {
var input = prompt("Please enter two positive digits, decimals will be rounded");
var digits = Math.round(input); //take the inoput and convert it to a whole number
var evenNumbers = [];
var oddNumbers = [];
while (isNaN(digits)|| digits < 10 || digits >99) {//isNaN is a method for checking if something isn't a number
var digits = prompt("Please enter two positive digits, decimals will be rounded")}
for (x = 0; x <= digits ;x++){
if(x % 2 === 0){
evenNumbers[x] = x;
console.log(evenNumbers);
}else{
oddNumbers[x] = x;
console.log(oddNumbers);
}
}
document.getElementById("demo").innerHTML = (evenNumbers.join(","));
var str1 = oddNumbers.toString;
document.getElementById("demo2").innerHTML = oddNumbers;
}
</script>
</body>
</html>
var str1 = oddNumbers.toString;assigns a function, without keepingthisreference to the variable. for example, you can not call the function later withstr1(). you need to hand over the array (read more:Function#call) withstr1.call(oddNumbers), or bind the array to the function withFunction#bind.