Write a javascript program that will ask user to enter a number three times. You
ID: 3547353 • Letter: W
Question
Write a javascript program that will ask user to enter a number three times. Your program will output the
number, the square of the number and the cube of the number each time user enters a number. E.g. if the
user entered 3, 5 and 6 then a table similar to the following will display on your browser. While a
negative number is acceptable and its square and cube can be computed, your program will give an alert
to the user saying he/she entered a negative number.
Number Square Cube
3 9 27
5 25 125
6 36 216
Explanation / Answer
<html>
<head>
<script type="text/javascript">
function askNumbers ()
{
var n1 = prompt('Please enter first number : ');
var n2 = prompt('Please enter second number : ');
var n3 = prompt('Please enter third number : ');
if(n1<0){
alert("first number is less than zero!");
}
if(n2<0){
alert("second number is less than zero!");
}
if(n3<0){
alert("third number is less than zero!");
}
document.getElementById( 'msg' ).textContent = "Number Square Cube" ;
document.getElementById( 'msg1' ).textContent = n1 +" "+ n1*n1 +" "+ n1*n1*n1 ;
document.getElementById( 'msg2' ).textContent = n2 +" "+ n2*n2 +" "+ n2*n2*n2 ;
document.getElementById( 'msg3' ).textContent = n3 +" "+ n3*n3 +" "+ n3*n3*n3 ;
}
</script>
<body>
<br>
<h2><center><div id="msg"></div></center></h2>
<h2><center><div id="msg1"></div></center></h2>
<h2><center><div id="msg2"></div></center></h2>
<h2><center><div id="msg3"></div></center></h2>
</body>
</html>
</head>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.