continue or return in JS

so i want to do simple functions where it would multiply numbers and not skip any kind of string but i can get why it wouldn't work
21 Replies
ABK | Muneer
ABK | MuneerOP3y ago
i tried break i tried continue and return all same thing the ones with string always gonna be NaN i also tried to set the strings to be 1 so it multiple by 1 and gets the same results but still not working
Jochem
Jochem3y ago
continue skips the current iteration of the loop break exits the loop altogether, so it skips the current and all future iterations return exits the current function and returns a value
ABK | Muneer
ABK | MuneerOP3y ago
i tried to put the console.log inside the loop and it gave more resons but still lots of NaN
ABK | Muneer
ABK | MuneerOP3y ago
ABK | Muneer
ABK | MuneerOP3y ago
what do you think is the best way to approach this
Jochem
Jochem3y ago
In your case you probably want confine instead of break
ABK | Muneer
ABK | MuneerOP3y ago
confine ?
Jochem
Jochem3y ago
Sorry, autocorrect. Continue
ABK | Muneer
ABK | MuneerOP3y ago
yah that was the first solution i thought of
ABK | Muneer
ABK | MuneerOP3y ago
ABK | Muneer
ABK | MuneerOP3y ago
but the problem still accrues could i sent a codepin ?
Joao
Joao3y ago
The thing is you are actually checking if the numbers array is a string. You are not really skipping any value, if (typeof numbers[i] === 'string')
Jochem
Jochem3y ago
I just caught that one too, nice one
Joao
Joao3y ago
This, with continue instead of break, will give you the result you want
ABK | Muneer
ABK | MuneerOP3y ago
oh yahhhhhh my bad forget to add the [i] but now it says that continue is not defined ahhh
Joao
Joao3y ago
In fact you may want to be a little more explicit: if (typeof numbers[i] === 'number' to make sure it's a number. And if you want to include strings that evaluate to numbers, you can use the isNaN function instead to parse strings into numbers.
ABK | Muneer
ABK | MuneerOP3y ago
wait never mind now it works ! oh my god that was just life saver i thought i didn't know the basic of Javascript to that point also worked tried that but because i didn't put [i] it also didn't work the way it should be
Joao
Joao3y ago
One last tip, don't do typeof whatever === typeof 'string'. If you compare that with other data types you'll get unexpected results. For example: typeof whatever === typeof 'number' that would be comparing to a number string (since 'number' is a string). Drop the second typeof
ABK | Muneer
ABK | MuneerOP3y ago
i started with typeof whatever === typeof "string" to make sure i get the write one then i did typeof number === typeof 10 but because i missed the [i] it didn't work thank you all for the help ! this place is the go to when my head is stuck lol
Joao
Joao3y ago
I also recommend taking a break, that does wanders
ABK | Muneer
ABK | MuneerOP3y ago
i had this problem since yesterday even tho i couldn't fix i took long break then i want to continue learning about Javascript but at the same time don't want to miss the way of understanding the language so i didn't want to skip it
Want results from more Discord servers?
Add your server