+ 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
9 Respuestas
+ 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");
}
+ 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.
+ 1
Thx dude
+ 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
+ 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?
+ 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]));
}
}
}
}
}
}
+ 1
So all I had to do was reassign the parsed version of the number to the array value?
+ 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.
+ 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