Back

Activity 1.2 Arithmetic

divider

Introduction

Activity 1.2

Arithmetic

Topics

  • Arithmetic in programming
  • The Number data type
  • Math operators

Any useful program, from video games to online stores, relies on numbers.

Whether it's calculating scores, processing transactions, or generating graphics, math is everywhere in software.

The Number Data Type

In JavaScript, the number type represents both integers and floating-point (decimal) values.

2.5  401  -65  -123.45  1_000_000  "35"

Demo Print Some Numbers

Use the console.log() function to display some numbers on the screen.

app.js
console.log(-1953);
console.log(1_000_000);

Math Operators

+ Addition
- Subtraction
* Multiplication
/ Division: Divides two numbers and returns the quotient as a decimal.
% Modulus (Mod): Divides two numbers and returns the remainder.

Order of Operations

Use parenthesis for operator precedence.

3 * 4 + 5

3 * (4 + 5)

Demo: Print Some Math Expressions

Use console.log() to display the result of some math expressions.

app.js
console.log(10 * 10.2);
console.log(450 / 5);
console.log(10 % 3);

'F' → Fullscreen

Objectives

  • icon Write Math Expressions
  • icon Output numeric values with console.log()
  • icon Document code with comments
  • icon Practice order of operations
divider

Activity Tasks

  • icon Create a new project named 1-2-arithmetic.js.
  • icon Complete each task individually.

Task 1: Addition and Subtraction

  • icon Display the result of the addition and subtraction expressions using console.log().
1-2-arithmetic.js
// Addition
console.log("I'm adding 2 and 1. The result is:");
console.log(2 + 1);
console.log();
console.log("Adding $1000 and $500.96 gives us:");
console.log(1000 + 500.96);
console.log();
// Subtraction
console.log("I have 3 apples, and I eat one. Now, I have:");
console.log(3 - 1);
console.log();
//

Task 2: Multiplication and Division

  • icon Display the result of the multiplication and division expressions using console.log().
1-2-arithmetic.js
// Addition
console.log("I'm adding 2 and 1. The result is:");
console.log(2 + 1);
console.log();
console.log("Adding $1000 and $500.96 gives us:");
console.log(1000 + 500.96);
console.log();
// Subtraction
console.log("I have 3 apples, and I eat one. Now, I have:");
console.log(3 - 1);
console.log();
// Multiplication
console.log("I'm 30 years old. In months, that's: "); // Replace 30 with your age
console.log(30 * 12); // Replace 30 with your age
console.log();
// Division - Quotient and remainder
console.log("If we divide 8 pizza slices between 2 people, each person gets:");
console.log(8 / 2);
console.log();
console.log("Dividing $100 among 6 people gives each person:");
console.log(100 / 6);
console.log();
//

Task 3: Modulo Operation

  • icon Use the mod operator to find the remainder portion of the division operation.
1-2-arithmetic.js
// Addition
console.log("I'm adding 2 and 1. The result is:");
console.log(2 + 1);
console.log();
console.log("Adding $1000 and $500.96 gives us:");
console.log(1000 + 500.96);
console.log();
// Subtraction
console.log("I have 3 apples, and I eat one. Now, I have:");
console.log(3 - 1);
console.log();
// Multiplication
console.log("I'm 30 years old. In months, that's: "); // Replace 30 with your age
console.log(30 * 12); // Replace 30 with your age
console.log();
// Division - Quotient and remainder
console.log("If we divide 8 pizza slices between 2 people, each person gets:");
console.log(8 / 2);
console.log();
console.log("Dividing $100 among 6 people gives each person:");
console.log(100 / 6);
console.log();
// Remainder using the modulus, or mod, operator
console.log("After dividing $100 among 6 people, the leftover cents are:");
console.log(100 % 6);
console.log();
//

Task 4: Order of Operations

  • icon Use parenthesis to control the order in which the operations are calculated.
1-2-arithmetic.js
// Addition
console.log("I'm adding 2 and 1. The result is:");
console.log(2 + 1);
console.log();
console.log("Adding $1000 and $500.96 gives us:");
console.log(1000 + 500.96);
console.log();
// Subtraction
console.log("I have 3 apples, and I eat one. Now, I have:");
console.log(3 - 1);
console.log();
// Multiplication
console.log("I'm 30 years old. In months, that's: "); // Replace 30 with your age
console.log(30 * 12); // Replace 30 with your age
console.log();
// Division - Quotient and remainder
console.log("If we divide 8 pizza slices between 2 people, each person gets:");
console.log(8 / 2);
console.log();
console.log("Dividing $100 among 6 people gives each person:");
console.log(100 / 6);
console.log();
// Remainder using the modulus, or mod, operator
console.log("After dividing $100 among 6 people, the leftover cents are:");
console.log(100 % 6);
console.log();
// Order of operations
console.log("The expression 4 * 10 + 12 equals:");
console.log(4 * 10 + 12);
console.log("The expression 4 * (10 + 12); equals:");
console.log(4 * (10 + 12));
//
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
I'm adding 2 and 1. The result is:
3
Adding $1000 and $500.96 gives us:
1500.96
I have 3 apples, and I eat one. Now, I have:
2
I'm 30 years old. In months, that's:
360
If we divide 8 pizza slices between 2 people, each person gets:
4
Dividing $100 among 6 people gives each person:
16.666666666666668
After dividing $100 among 6 people, the leftover cents are:
4
The expression 4 * 10 + 12 equals:
52
The expression 4 * (10 + 12); equals:
88
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. How did the use of parentheses change the result in the order of operations example? Why is that important when writing code?
  2. Which math operator did you find most surprising or unfamiliar in JavaScript, and why?
  3. Why is it helpful to include spaces around math operators (like +, -, *, /) when writing code, even though JavaScript works without them?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete