Another javascript:) How do you write a script that asks someone to enter 2 numbers, and outputs HTML text that displays the larger number followed by the words"is larger" in an information message dialog?
Another javascript:) How do you write a script that asks someone to enter 2 numbers, and outputs HTML text tha
If the two inputted numbers are guaranteed not to be equal, you can do something like this.
var int_1 = Number( prompt( "Enter first number.",""));
var int_2 = Number( prompt( "Enter secod number.",""));
if( int_2 %26gt; int_1) {
int_1 ^= int_2;
int_2 ^= int_1;
int_1 ^= int_2;
}
alert( int_1 + " is larger than " + int_2 + ".");
** Notes **
The part in the IF statement is a very old school method of swapping two variables using a NOR operator. This example requires that one number be larger than the other. If you need to test for equality then use this example.
var int_1 = Number( prompt( "Enter first number.",""));
var int_2 = Number( prompt( "Enter secod number.",""));
var descr = " larger than ";
if( int_2 %26gt; int_1) {
int_1 ^= int_2;
int_2 ^= int_1;
int_1 ^= int_2;
} else if (int_1 == int_2) {
descr = " equal to ";
}
alert( int_1 + " is " + descr + int_2 + ".");
** More Notes **
You'll notice that I encased the Prompts inside of Number(). Although not always required, it is good programming practice to explicitly cast a numeric value from a Prompt. A Prompt command returns a String value so you can't perform any mathematical operations on the values it returns.
Remove the Number() from the above examples and try adding the two variables together.
alert( int_1 + int_2);
You'll find that it only concatenates the two String values and does not add them together as you may have expected.
Reply:This is easy:
%26lt;script%26gt;
var a = prompt("Enter a number","");
var b = prompt("Enter another number","");
if(a %26gt; b)
{
alert(a + " is bigger then " + b);
}else
{
if(a == b)
{
alert("They are equal!");
}else
{
alert(b + " is bigger then " + a);
}
}
%26lt;/script%26gt;
I tested this.
If you need more help I would be happy to answer just post!
Enjoy!
printable cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment