Answer to Question #59880 in AJAX | JavaScript | HTML | PHP for oldmil

Question #59880
Write a program that asks a user to enters pairs of numbers until they enter "quit". As each pair of numbers is entered and validated, add the numbers using a function. The function will have two parameters for the pair of numbers and will return the sum. After the user enters "quit", output all the pairs of numbers and their sums.
1
Expert's answer
2016-05-13T10:08:03-0400
// creating variables and array
var num1 = 0, num2 = 0, arrNum = [1];

// functiond that add 2 numbers
function sum(num1, num2)
{
var sum = 0;
sum = Number(num1) + Number(num2);
return sum;
}

//function that check var number for quit
function checkExit(num)
{
if(num == 'quit')
{
return true;
}
else
return false;
}

function printArr(arrNum)
{
document.write("Entries Sum");
document.write("<br><br>");
for (var i = 1; i < arrNum.length; i+=3)
{
document.write(arrNum[i] + ' ' + arrNum[i+1] + ' ' + arrNum[i+2]);
document.write("<br><br>");
}
}

while(true)
{
//enteting num1 and num2
num1 = prompt('Enter 1 number of pair', 0);
//checking for quit
if(checkExit(num1) == true)
break;
//adding nums to array for output in the end
arrNum.push(num1);

num2 = prompt('Enter 2 number of pair', 0);
if(checkExit(num2) == true)
break;

arrNum.push(num2);
//calculating and adding sum to array
arrNum.push(sum(num1, num2));
}
//printing numbers and sums
printArr(arrNum);








index.html
<!DOCTYPE html>
<html>
<head>
<title>Finding sum</title>
<meta charset="UTF-8">
<script src="program.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
16.05.16, 17:29

Dear customer, Please submit it with all requirements as an assignment to our control panel and we'll assist you.

chico33
14.05.16, 00:52

Can this be done without the array? I can send the file if need be

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS