1

My goal is to create an array, and when a button is clicked, it goes through each individual item of the array and alerts whether it's a number or not.

<body>
    <form id = "form">
        <p id = "instruction">Enter all values in the textbox, separated by a space.</p>
        <textarea id = "textarea" rows = "8" columns = "60"></textarea>
            <input id = "submit" type = "submit" value = "Sort Alphabetically" onclick = "stringSplitAlpha();">
            <p id = "p"></p>
        </form>
<script>
function stringSplitAlpha() {
    var str = document.getElementById("textarea").value;
    var arr1 = str.split(" ");
    for (var i = 0; i < arr1.length; i++) {
        window.alert(isNaN(arr1[i]));
        if (isNaN(arr1[i]) == 'true') {
            window.alert(arr1[i] + "is not a number.");
        } else {
            window.alert("arr1[i] + "is a number.");
        }
    }
}
</script>
</body>

When I run the code, everything works fine at the beginning. I put in some values in the textbox for the array, and when I click the button, window.alert(isNaN(arr1[i])); shows "true" on a letter and "false" on a number. This is expected.

However, when it says "true" for a letter, the next alert shows "... is a number." This comes from

if (isNaN(arr1[i]) == 'true') {
            window.alert(arr1[i] + "is not a number.");
        } else {
            window.alert("arr1[i] + "is a number.");
        }

, but is totally unexpected. window.alert(isNaN(arr1[i])); came up as "true" so shouldn't it say "... is not a number"? I was stumped here. I thought maybe there was some sort of syntax error, but there doesn't seem to be any. I tried both double and triple equal signs, to no avail.

Just to clarify, if I put in "1 34 hello" in the textbox, this is what would show up in the alerts, in order:

"false," "1 is a number," "false," "34 is a number," "true," "hello is a number". Only the last alert of "hello is a number" is incorrect.

Thanks in advance.

1 Answer 1

1

The isNaN function returns a boolean value, not a string. Instead of comparing it to 'true', try it without the comparison. The other option would be to remove the quotation marks from around true and your original example should work. I further condensed it below since the best practice way you would see this in code is to omit the comparison for a boolean value.

if (isNaN(arr1[i])) {
  window.alert(arr1[i] + " is not a number.");
} else {
  window.alert(arr1[i] + " is a number.");
}

Further Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

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

2 Comments

Yes! It worked! Will accept it when I can. One question - how does the if statement work without any sort of "true" or "false" statement, just the isNaN(arr1[i]) itself?
I'm happy it worked for you! It works because the if statement is designed to evaluate true vs false automatically. A true evaluation will always go into the "if" block and a false evaluation will always go into the "else" block. A boolean value will always be true or false which is what is returned from isNaN.

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.