CPSC 1045: Assignment 1
TO BE DONE INDIVIDUALLY
Overview
We will manually evaluate complex expressions in order to better understand how JavaScript
interprets these expressions step-by-step. You are required to show your work (i.e., show each
step of how you evaluate/find the solution for each expression). The web console in the browser
can help you check your answer, but it should not do the work for you.
Resources
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
1. [10 marks] Open a new text file and evaluate each of the following 10 expressions. Make
sure to show ALL OF THE STEPS you took to solve the expression. Answers that do
not show enough steps will receive a grade of 0. The first one has been done for you.
1. ”Dog” + 2 > 3 && true
Steps:
“Dog2” > 3 && true
false && true
false
Final Solution: false
2. true || false && true
3. let firstName = “trouble”;
let lastName = “double”;
(firstName === "Batman" || firstName === "Trouble" ) &&
(lastName === "Batman" || lastName === "kong" )
4. let a = 21;
a += 3;
let b = 5;
b -= a;
(a < 1) || (b >= 1) && (a != b)
5. let pet = “alligator”;
let escape = “boat”;
“The “ + pet + “ escaped. It was last seen on a “ + escape;
6. let George = “orge”;
let nickname = “Conquerer”;
let combinedName = George + “ “ + nickname;
(combinedName === George) || (George !== "George")
&&(combinedName === "Conquerer") || (nickname === 42)
7. ((42 === "42") && (42== "42")) || ((42 < "Whistle")
|| (42 > "234"))
8. ((24*23+12/2+22) % 2 === 1)
9. (([Link](3,3) === 27) || ([Link]([Link]) === 0)) ||
([Link]([Link](1.2),1)+[Link]([Link](1.2),2) === 1)
10. let sentence = “The world is green!”;
[Link](4,9) === "world" && [Link] < 20
&& [Link] > 5 && [Link](0,3) === "The
world is red".substring(0,3);
11. let bigCar = true;
let bearTrap;
"The variable bigCar has the value : "+ bigCar + ", while
variable bearTrap is " + bearTrap + "If I compare bearTrap
with undefined I get " + bearTrap===undefined
2. [5 marks] Now we are going to write runnable JavaScript. Download [Link] from D2L and
extract the [Link] and [Link] files. The html file is linked to the javascript file by the script tag
in the head element. There is a button that when pressed will call a function called
nosesAndFeet(). Inside the [Link] file write code inside the nosesAndFeet function to do the
following.
• The user is asked “How many people live at your home?” and will enter a
number.
• The user is asked “How many (4-legged) pets live at your home?” and will enter
a number.
• The webpage will display a message that says “The people and pets at your
home have a total of X noses and Y feet.” where X and Y are replaced
by the appropriate values.
• You can assume the user enters numbers. You don’t have to worry about what
happens if they don’t enter a number.
3. Submission
• Submit to D2L ONE zip/archive file containing the text file and your modified copy
of [Link]
4. Grading
Marks will be assigned as follows:
• 1 mark for each correctly evaluated expression with steps shown
• 5 marks for exercise 2