Posted: 9/17/2016 1:21:18 PM EDT
|
I have a little (very little) background in programming. This semester I am in a class that is using javascript and I've run into an issue that I can't figure out how to fix.
The problem here is the language sees 9 and 09 differently. So for example when I input 9 and 18 it runs and shows 9 as the larger number. However if I put 09 and 18 in then it shows 18 as the larger number. I've tried int instead of var but the same issue happens. Now maybe my instructor doesn't care about this since we are just starting out in the language, but it bothers the shit out of me and I want to know how to fix it.
|
|
Quoted:
Did it assume that both entries must have the same number of decimal places and add one for the single digit? Yea im not exactly sure what's it's seeing. I think it is putting 9 in the ones spot and for 18 the 1 is in the ones spot making 9 the larger number..... That is my best guess. In java I didn't have this issue. |
|
Quoted:
you need to parseInt("your string variable here") to that it sees it as a complete number rather than as the characters 1 and 8 so if (parseInt(x) > parseInt(y)){...... alternatively, you could var x = parseInt(prompt("Enter a number: ")); . . . if (x > y)... This. It's doing a string compare, not a comparison of a numeric value. If you wanted to be more complete you would check that they actually entered a number and kick it back if they didn't. |
|
Quoted: Yea im not exactly sure what's it's seeing. I think it is putting 9 in the ones spot and for 18 the 1 is in the ones spot making 9 the larger number..... That is my best guess. In java I didn't have this issue. Quoted: Quoted: Did it assume that both entries must have the same number of decimal places and add one for the single digit? |
|
Quoted:
you need to parseInt("your string variable here") to that it sees it as a complete number rather than as the characters 1 and 8 so if (parseInt(x) > parseInt(y)){...... alternatively, you could var x = parseInt(prompt("Enter a number: ")); . . . if (x > y)... Thank you! God I've been trying to figure this out all last night and today. I thought int variable might work but I don't even know if that works in js or not. |