Exercise 4.2 Write a javascript program that ask user to enter three numbers, yo
ID: 3811906 • Letter: E
Question
Exercise 4.2 Write a javascript program that ask user to enter three numbers, your program will then output the number, the square of the number and the cube of the number on a table like the following. E.g. if the user entered 3,4 and 5 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 4 16 64 5 25 125
Explanation / Answer
No example table provided as mentioned in the question. so wrote code in my own table format
<!DOCTYPE HTML>
<html>
<head>
<script>
var sq=0,cu = 0;
for(var i=0;i<3;i++){
var val = parseInt(prompt("Enter number"+(i+1)));
if(val<0)
alert("You have entered a negative number");
sq=val*val;
cu=val*val*val;
document.write('<table border="1" cellspacing="0">');
document.write("<tr><td>Square of the "+val+" is "+sq+"</td><td>cube of the "+val+" is "+cu+"</td></tr>");
document.write("</table>");
}
</script>
</head>
<body>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.