Don't know why this doesn't work... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Don't know why this doesn't work...

Near the top of the code I use a loop to cycle through the numarr array and change each value to a number using parseInt, but it doesn't work, and I'm not sure if I'm using a wrong input field or if my code is just flawed https://code.sololearn.com/WQXg8gvVa6wG/?ref=app

4th Apr 2018, 10:27 PM
jacksonofgames 28
jacksonofgames 28 - avatar
9 Answers
+ 3
I've changed around some things in that top portion that you're talking about and posted it below for you. That should get you over that hump and on to the next piece. Basically, we just need to know if each input is a valid number, rather than worrying about parsing it. So I created a boolean that'll store if it's valid or not. In the loop we'll check if it's a number and if it isn't, change isValid boolean to false and break the loop. That'll display the error message for us. If it's valid, it'll proceed to the sum() function. I didn't mess with the sum() function or even read it, so that's all you to play with. :D If you run into issues, hit me up. Here you go bro: function program() { numbers = input.value; numarr = numbers.split(' '); let isValid = true; for (let z = 0; z < numarr.length; z ++) { if(isNaN(numarr[z])){ isValid = false; break; } } if (isValid){ console.log("Running sum()"); sum(); } else { console.log("Invalid input. Make sure it's a number."); alert("Input must be a number. Please try again"); }
4th Apr 2018, 10:58 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Also, you'll want to state somewhere on the page that the values are separated with a space. That'll let the user know what to do so they're not confused on the proper format of the input.
4th Apr 2018, 11:05 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Thx dude
5th Apr 2018, 2:58 AM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 1
But I still need to convert it to a number, because I have an if statement that says if a + b + c = 0 then print out a, b and c
5th Apr 2018, 5:18 PM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 1
I've been testing the same number conversion with some of my other projects and it doesnt work either. Is there any way to do this?
5th Apr 2018, 5:56 PM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 1
Sorry bro, I've been caught up with work and such; just now seeing your message. This should fix your problem, considering what you're doing with the data later. Basically, when we do the validation check, we'll see if it 'isNaN' and if it isn't, we'll prompt user to re-input their numbers. If it IS a number, we'll replace that element in the array with the parsed version of the number instead. So when the validation check comes back 100% as valid, it'll have also replaced each string with the valid number (int). https://code.sololearn.com/WC8yt5r6O9Pj/#js function program() { numbers = input.value; numarr = numbers.split(' '); let isValid = true; for (let z = 0; z < numarr.length; z ++) { if(isNaN(numarr[z])){ isValid = false; break; } else { numarr[z] = parseInt(numarr[z]); } } if (isValid){ console.log("Running sum()"); sum(); } else { console.log("Invalid input. Make sure it's a number."); alert("Input must be a number. Please try again"); } function sum() { for (let a = 0; a < numarr.length; a ++) { for (let b = a + 1; b < numarr.length; b ++) { for (let c = b + 1; c < numarr.length; c ++) { if (numarr[a] + numarr[b] + numarr[c] == 0) { alert("All Zeros: " + numarr[a] + "," + numarr[b] + "," + numarr[c]); } else { alert("Not zeros: " + numarr[a] + "," + numarr[b] + "," + numarr[c]); alert("Sum Numbers: " + (numarr[a]+numarr[b]+numarr[c])); } } } } } }
6th Apr 2018, 5:13 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
So all I had to do was reassign the parsed version of the number to the array value?
6th Apr 2018, 5:21 PM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 1
@JacksonOfGames28 lol Yeah. ;) Since we're 100% certain that it's valid input by that point, we can easily reassign the value back onto itself to store it as an int instead of a string number. Then calculations are simple from that point.
6th Apr 2018, 6:07 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Thanks for all your help man. Here's the complete thing now, just got done fixing some bugs in a hurry so let me know if you see any https://code.sololearn.com/WQXg8gvVa6wG/?ref=app
6th Apr 2018, 6:32 PM
jacksonofgames 28
jacksonofgames 28 - avatar