Skip to content

Instantly share code, notes, and snippets.

@rachelmyers
Created January 12, 2011 22:20
Show Gist options
  • Save rachelmyers/777020 to your computer and use it in GitHub Desktop.
Save rachelmyers/777020 to your computer and use it in GitHub Desktop.
Answer for javascript homework class
<!--
Write a program that accepts three numbers from the user
and alerts back the the user: the sum of the numbers,
the total of the numbers multiplied together, the average
of the numbers, and, for extra credit, figure out how to
find the largest number and smallest number.
-->
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Javascript Math is the Coolest</TITLE>
</HEAD>
<BODY>
<SCRIPT type="text/javascript">
x = parseInt(prompt("Give me a first number.")); // should each be between -4294267296 and 4294267296
y = parseInt(prompt("Give me a second number."));
z = parseInt(prompt("Give me a third number."));
sum = x + y + z;
product = x * y * z;
average = sum / 3;
// //find the largest number using nested ifs:
// if (x > y)
// {
// if (x > z)
// {
// largest = x;
// }
// else if (z > x)
// {
// largest = z;
// }
// }
// else if (y > x)
// {
// if (y > z)
// {
// largest = y;
// }
// else if (z > x)
// {
// largest = z;
// }
// }
// else if (x == y)
// {
// if (x == z)
// {
// largest = x;
// }
// }
//
// //find the smallest number using nested ifs.
// if (x < y)
// {
// if (x < z)
// {
// smallest = x;
// }
// else if (z < x)
// {
// smallest = z;
// }
// }
// else if (y < x)
// {
// if (y < z)
// {
// smallest = y;
// }
// else if (z < x)
// {
// smallest = z;
// }
// }
// else if (x == y)
// {
// if (x == z)
// {
// smallest = x;
// }
// }
//
//find the largest number using Math.max
largest = Math.max(x, y, z)
//find the smallest number using Math.min
smallest = Math.min(x, y, z)
alert("The three numbers you gave me were: " + x + ", " + y + ", & " + z);
alert("The sum is: " + sum);
alert("The product is: " + product);
alert("The average is: " + average);
alert("The largest number is: " + largest);
alert("The smallest number is: " + smallest);
</SCRIPT>
</BODY>
</HTML>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment