2

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>
2
  • Output I'm getting is: 0, , 2, , 4, , 6, , 8, , 10, , 12 ,1,,3,,5,,7,,9,,11 Commented Feb 15, 2020 at 17:02
  • btw, var str1 = oddNumbers.toString; assigns a function, without keeping this reference to the variable. for example, you can not call the function later with str1(). you need to hand over the array (read more: Function#call) with str1.call(oddNumbers), or bind the array to the function with Function#bind. Commented Feb 15, 2020 at 17:14

2 Answers 2

3

You create a sparse array with holes by using indices which are not consecutive.

Just replace

evenNumbers[x] = x;
// or
oddNumbers[x] = x;

with

evenNumbers.push(x);
// or
oddNumbers.push(x);

By usingArray#push, you avoid holes, because every element is filled with a value.

function myDigitChecker() {
    var input = prompt("Please enter two positive digits, decimals will be rounded"),
        digits = Math.round(input), //take the inoput and convert it to a whole number
        evenNumbers = [],
        oddNumbers = [],
        x;

    while (isNaN(digits) || digits < 10 || digits > 99) { //isNaN is a method for checking if something isn't a number
        digits = prompt("Please enter two positive digits, decimals will be rounded");
    }
    
    for (var x = 0; x <= digits; x++) {
        if (x % 2 === 0) {
            evenNumbers.push(x);
        } else {
            oddNumbers.push(x);
        }
    }

    document.getElementById("demo").innerHTML = evenNumbers.join(",");
    document.getElementById("demo2").innerHTML = oddNumbers.join(",");
}
<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>

Sign up to request clarification or add additional context in comments.

Comments

0

Remove the empty values in arrays, replace evenNumbers.join(",") with evenNumbers.filter(x => Number.isInteger(x).join(",").

document.getElementById("demo").innerHTML = evenNumbers.filter(x =>  Number.isInteger(x).join(",");
document.getElementById("demo2").innerHTML = oddNumbers.filter(x =>  Number.isInteger(x).join(",");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.