Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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.


var x = prompt("Enter a number: ");
var y = prompt("Enter another number: ");

if (x > y){
   writeln("The larger number is: ", x);
   writeln("The smaller number is: ", y);
   writeln("The difference is: ", x - y);
}
else{
   writeln("The larger number is: ", y);
   writeln("The smaller number is: ", x);
   writeln("The difference is: ", y - x);
}
9/17/2016 1:22:13 PM EDT
[#1]
Fuck Java
9/17/2016 1:26:53 PM EDT
[#2]
I have not done programming since 1979 and I failed it then. Can you start with the variables populated with zeroes rather than blank, so it will default to a 09 when you only enter a 9?  Does it even work that way?
9/17/2016 1:27:11 PM EDT
[#3]
Did it assume that both entries must have the same number of decimal places and add one for the single digit?
9/17/2016 1:29:22 PM EDT
[#4]
Quote History
Quoted:

Haha Thank you for the insight! I don't disagree but this does not help me

Fuck Java
View Quote

9/17/2016 1:29:59 PM EDT
[#5]
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)...
9/17/2016 1:31:12 PM EDT
[#6]
Quote History
Quoted:
Did it assume that both entries must have the same number of decimal places and add one for the single digit?
View Quote


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.
9/17/2016 1:31:47 PM EDT
[#7]
Quote History
Quoted:

View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:

Haha Thank you for the insight! I don't disagree but this does not help me

Fuck Java



Java or JavaScript?
9/17/2016 1:33:53 PM EDT
[#8]
Quote History
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)...
View Quote


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.
9/17/2016 1:34:59 PM EDT
[#9]
the issue is that string comparison is comparing the "18" to "9" and seeing that 18[0] == '1'  and 9[0] is '9' and doing the comparison there and then stopping

this is great for alphabetizing things "ABCD" is < "ABD"
9/17/2016 1:37:07 PM EDT
[#10]

Quote History
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.
View Quote View All Quotes
View All Quotes
Quote History
Quoted:



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.
Javascript is a non type specific language.  That why you say 'Var' and not 'int' or 'String'.  It's taking your input as a string, and comparing like 'a' and 'b'.  In a String mode '10' is not greater than '9', just like 'aa' is less than 'b'.  Use the 'parseInt' as described above to set the interpreter straight that you want it treated as a number, and not a String.
9/17/2016 1:37:50 PM EDT
[#11]
Quote History
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)...
View Quote



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.
9/17/2016 1:41:35 PM EDT
[#12]
Yup.  It has be a numeric comparison so you must convert them before the comparison.

Also, it wouldn't hurt to catch the exception or test to see if the entry is numeric before converting it.
9/17/2016 1:52:32 PM EDT
[#13]
Fuck languages that are not strongly typed.

9/17/2016 1:58:12 PM EDT
[#14]
Quote History
Quoted:
Fuck Java
View Quote


java is not javascript