Get Programming with JavaScript - Listing 4.07
Listing 4.07 - Using the findTotal function to display a calculation
JS Bin
var number1 = 1000;
var number2 = 66;
var result;
var findTotal;
findTotal = function () {
result = number1 + number2;
};
findTotal();
console.log(number1 + " + " + number2 + " = " + result);
Further Adventures
Listing 4.07 - Using the findTotal function to display a calculation - Task 1
- Change number1 and number2 and run the program.
var number1 = 100; // Change number1
var number2 = 6; // Change number2
var result;
var findTotal;
findTotal = function () {
result = number1 + number2;
};
findTotal();
console.log(number1 + " + " + number2 + " = " + result);
Listing 4.07 - Using the findTotal function to display a calculation - Task 2
- Add a third variable, number3, and assign it a value.
var number1 = 100;
var number2 = 6;
var number3 = 50; // A third variable and value
var result;
var findTotal;
findTotal = function () {
result = number1 + number2;
};
findTotal();
console.log(number1 + " + " + number2 + " = " + result);
Listing 4.07 - Using the findTotal function to display a calculation - Task 3
- Update the findTotal function to add the three numbers.
var number1 = 100;
var number2 = 6;
var number3 = 50;
var result;
var findTotal;
findTotal = function () {
result = number1 + number2 + number3; // Add the three numbers
};
findTotal();
console.log(number1 + " + " + number2 + " = " + result);
Listing 4.07 - Using the findTotal function to display a calculation - Task 4
- Update the console.log line to show the new calculation.
var number1 = 100;
var number2 = 6;
var number3 = 50;
var result;
var findTotal;
findTotal = function () {
result = number1 + number2 + number3;
};
findTotal();
// Update console.log
console.log(number1 + " + " + number2 + " + " + number3 + " = " + result);