Prompt for an integer (you can assume the user will enter an integer). Only if t
ID: 3812092 • Letter: P
Question
Prompt for an integer (you can assume the user will enter an integer). Only if the number isdivisible by 3 or 5, it is positive, and is not greater than 500, will it be considered valid. Theprogram should continue to do this, until the user enters 12345. The number 12345 should stopthe script. (Note: When you use a special value to stop a program, we call that a sentinel value.In this program the sentinel is 12345). After the user enters the sentinel, write to the webpage“No data to print” if no valid numbers were entered. For example, if the user enters -5, 505, 22,and 43, none are valid. If there are one or more valid numbers, print only the largest validnumber X 0 to largest valid number X 10 on the webpage. For example, if the user enters 9, 550,405, 18, and 407, it would print the data for 405, since it is the largest valid number out of the 5.
Explanation / Answer
HTML AND JAVASCRIPT CODE
<!DOCTYPE html>
<html>
<head>
<title>largest number</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
var max=0;
var flag=0;
while(true)
{
n=prompt('Enter a number');
if (n==="12345")
break;
else
{
n=parseInt(n);
if ((n>0) && (n<500) && ((n%3===0 )|| (n%5===0)))
{
if (n >max)
{
max=n;
}
flag=1;
}
}
}
if(flag==0)
{
document.write("No data to print ");
}
else
{
document.write(max);
}
</script>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.