In: Computer Science
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12.
Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of the mathematical calculations.
See below example below:
The user enters "5" as their input. Below is the expected output.
Addition Math Facts for "5"
5 + 1 = 6
5 +2 = 7
...
5 + 11 = 16
5 + 12 = 17
Subtraction Math Facts for "5"
12 - 5 =
11 - 5 =
10 - 5 =
...
7 - 5 = 2
6 - 5 = 1
5 - 5 = 0
* Please note the subtraction facts should stop at the input number - the input number because 1st graders are not prepared to learn about negative numbers.
If the user enters a number not between 1 and 12, open an alert window stating that it is an invalid number, and prompt them to enter a number again meeting the criteria. Finally, if the number entered is bellow 6, make the background color blue and the text white, if the number is above 6 make the background color red and the text yellow, and if the number is six then make the background color white and the text black.
* Should be in HTML language.
HINT: use a variable and a window.prompt() to store what the person enters, a conditional statement, and string concatenation using HTML as we discussed in class.
HTML & Javascript Code:
<html>
<head>
<script
type="text/javascript">
function
processMath()
{
while(true)
{
//Reading input and parsing
it to integer
var num =
parseInt(prompt("Please enter a number between 1 and 12",
"0"));
//Validating number
if(num < 1 || num >
12)
{
alert("Invalid Number");
}
else
{
break;
}
}
//Generating Addition and Subtraction
document.writeln("</br> Addition Math
Facts for " + num + " </br>");
var i;
for(i=1; i<=12; i++)
{
document.writeln(num + " + "
+ i + " = " + (num+i) + " </br>");
}
document.writeln("</br> Subtraction Math
Facts for " + num + " </br>");
for(i=12; i>=num; i--)
{
document.writeln(i + " - " +
num + " = " + (i-num) + " </br>");
}
//Changing color
if(num < 6)
{
document.body.style.backgroundColor = "blue";
document.body.style.color =
'white';
}
else if(num > 6)
{
document.body.style.backgroundColor = 'red';
document.body.style.color =
'yellow';
}
else
{
document.body.style.backgroundColor = 'white';
document.body.style.color =
'black';
}
}
</script>
</head>
<body onload="processMath()">
</body>
</html>
___________________________________________________________________________________________________
Sample Run: