Click here to Skip to main content
15,887,083 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: dwg to jpg using javascript Pin
Rajesh R Subramanian20-Jan-17 0:01
professionalRajesh R Subramanian20-Jan-17 0:01 
AnswerRe: Can you tell me what Jquery script this? Pin
ZurdoDev19-Jan-17 2:00
professionalZurdoDev19-Jan-17 2:00 
GeneralRe: Can you tell me what Jquery script this? Pin
Richard Deeming19-Jan-17 2:35
mveRichard Deeming19-Jan-17 2:35 
GeneralRe: Can you tell me what Jquery script this? Pin
ZurdoDev19-Jan-17 3:12
professionalZurdoDev19-Jan-17 3:12 
GeneralRe: Can you tell me what Jquery script this? Pin
Richard Deeming19-Jan-17 3:57
mveRichard Deeming19-Jan-17 3:57 
GeneralRe: Can you tell me what Jquery script this? Pin
ZurdoDev19-Jan-17 4:11
professionalZurdoDev19-Jan-17 4:11 
QuestionHi, I really new to javascript. I was doing a JavaScript program that accept two integers and display the larger. Pin
abdul kabeer18-Jan-17 7:19
abdul kabeer18-Jan-17 7:19 
AnswerRe: Hi, I really new to javascript. I was doing a JavaScript program that accept two integers and display the larger. Pin
Richard Deeming18-Jan-17 8:23
mveRichard Deeming18-Jan-17 8:23 
The prompt() method[^] returns a string[^].

When you compare two strings, you are comparing them character-by-character; if they're not exactly the same length, you won't get a numeric comparison. For example, the string "10" is less than the string "2", because the first character of the first string ("1") is less than the first character of the second string ("2").

To get a numeric comparison, you need to convert the strings to numbers. If you're only expecting whole numbers, use parseInt[^]; for floating-point numbers, use parseFloat[^].

You can also use Math.max()[^] to get the higher number without an if / else block.
JavaScript
var x = prompt("Enter 1st number");
var xAsNumber = parseInt(x, 10);

// If it's not a valid number (NaN), loop until the user enters a valid number:
while (isNaN(xAsNumber)) {
    x = prompt("Enter 1st number");
    xAsNumber = parseInt(x, 10);
}

var y = prompt("Enter 2nd number");
var yAsNumber = parseInt(y, 10);

// If it's not a valid number (NaN), loop until the user enters a valid number:
while (isNaN(yAsNumber)) {
    y = prompt("Enter 2nd number");
    yAsNumber = parseInt(y, 10);
}

var highestNumber = Math.max(xAsNumber, yAsNumber);
alert(highestNumber);

// Or:
if (xAsNumber > yAsNumber) {
    alert(xAsNumber);
}
else {
    alert(yAsNumber);
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Hi, I really new to javascript. I was doing a JavaScript program that accept two integers and display the larger. Pin
abdul kabeer18-Jan-17 8:40
abdul kabeer18-Jan-17 8:40 
GeneralRe: Hi, I really new to javascript. I was doing a JavaScript program that accept two integers and display the larger. Pin
Richard Deeming18-Jan-17 8:50
mveRichard Deeming18-Jan-17 8:50 
GeneralRe: Hi, I really new to javascript. I was doing a JavaScript program that accept two integers and display the larger. Pin
abdul kabeer19-Jan-17 7:38
abdul kabeer19-Jan-17 7:38 
AnswerRe: Hi, I really new to javascript. I was doing a JavaScript program that accept two integers and display the larger. Pin
Pawan Kumar18-Jan-17 8:24
Pawan Kumar18-Jan-17 8:24 
Questiontrying to make a date difference variable auto populate another text box (in another function) Pin
Barnsley711-Jan-17 23:16
Barnsley711-Jan-17 23:16 
AnswerRe: trying to make a date difference variable auto populate another text box (in another function) Pin
Richard Deeming12-Jan-17 7:06
mveRichard Deeming12-Jan-17 7:06 
GeneralRe: trying to make a date difference variable auto populate another text box (in another function) Pin
Barnsley712-Jan-17 22:30
Barnsley712-Jan-17 22:30 
QuestionHow to dynamically Add and Close tab items in Browser Pin
Aniruddha.A10-Jan-17 17:42
Aniruddha.A10-Jan-17 17:42 
QuestionCalendar next / previous links not working javascript Pin
hmkoyan8-Jan-17 2:07
hmkoyan8-Jan-17 2:07 
QuestionRe: Calendar next / previous links not working javascript Pin
ZurdoDev9-Jan-17 7:19
professionalZurdoDev9-Jan-17 7:19 
QuestionHow to override .js file Pin
Member 129321392-Jan-17 22:26
Member 129321392-Jan-17 22:26 
QuestionPopup div Pin
Otekpo Emmanuel25-Dec-16 3:00
Otekpo Emmanuel25-Dec-16 3:00 
AnswerRe: Popup div Pin
Afzaal Ahmad Zeeshan25-Dec-16 3:41
professionalAfzaal Ahmad Zeeshan25-Dec-16 3:41 
GeneralRe: Popup div Pin
Otekpo Emmanuel25-Dec-16 4:42
Otekpo Emmanuel25-Dec-16 4:42 
GeneralRe: Popup div Pin
Afzaal Ahmad Zeeshan25-Dec-16 4:47
professionalAfzaal Ahmad Zeeshan25-Dec-16 4:47 
QuestionRe: Popup div Pin
ZurdoDev28-Dec-16 3:32
professionalZurdoDev28-Dec-16 3:32 
AnswerRe: Popup div Pin
ZurdoDev25-Jan-17 9:02
professionalZurdoDev25-Jan-17 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.