Solution Js
Solution Js
console.log("Laurence");
Laurence
undefined
<head>
<title>Tester</title>
</head>
<body>
[ 425 ]
Appendix
<script>
console.log("hello world");
</script>
</body>
</html>
<head>
<title>Tester</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
Projects
Creating an HTML file and a linked JavaScript file
<!doctype html>
<html>
<head>
<title>JS Tester</title>
</head>
[ 426 ]
Practice Exercise, Project, and Self-Check Quiz Answers
<body>
<script src="myJS.js"></script>
</body>
</html>
// console.log("Laurence");
/*
This is my comment
Laurence Svekis
*/
Self-check quiz
1. <script src="myJS.js"></script>.
2. No.
3. By opening and closing it with /* and */.
4. Comment out the line with //.
[ 427 ]
Appendix
Projects
Miles-to-kilometers converter
//Convert miles to kilometers.
//1 mile equals 1.60934 kilometers.
let myDistanceMiles = 130;
let myDistanceKM = myDistanceMiles * 1.60934;
console.log("The distance of " + myDistanceMiles + " miles is equal to
" + myDistanceKM + " kilometers");
BMI calculator
//1 inch = 2.54 centimetres.
//2.2046 pounds in a kilo
let inches = 72;
let pounds = 180;
let weight = pounds / 2.2046; // in kilos
let height = inches * 2.54; // height in centimetres
console.log(weight, height);
let bmi = weight/(height/100*height/100);
console.log(bmi);
[ 428 ]
Practice Exercise, Project, and Self-Check Quiz Answers
Self-check quiz
1. String
2. Number
3. Line 2
4. world
5. Hello world!
6. Whatever the user enters in
7. 71
8. 4
9. 16 and 536
10. true
false
true
true
false
[ 429 ]
Appendix
myList.sort();
console.log(myList.indexOf("Milk"));
myList.splice(1, 0, "Carrots", "Lettuce");
const myList2 = ["Juice", "Pop"];
const finalList = myList.concat(myList2, myList2);
console.log(finalList.lastIndexOf("Pop"));
console.log(finalList);
[ 430 ]
Practice Exercise, Project, and Self-Check Quiz Answers
Projects
Manipulating an array
theList.pop();
theList.shift();
theList.unshift("FIRST");
theList[3] = "hello World";
theList[2] = "MIDDLE";
theList.push("LAST");
console.log(theList);
[ 431 ]
Appendix
Self-check quiz
1. Yes. You can reassign values within an array declared with const, but cannot
redeclare the array itself.
2. Length
3. The outputs are as follows:
-1
1
Practice exercises
Practice exercise 4.1
const test = false;
console.log(test);
if(test){
console.log("It's True");
}
if(!test){
console.log("False now");
}
[ 432 ]
Practice Exercise, Project, and Self-Check Quiz Answers
[ 433 ]
Appendix
Projects
Evaluating a number game answers
let val = prompt("What number?");
val = Number(val);
let num = 100;
let message = "nothing";
if (val > num) {
message = val + " was greater than " + num;
} else if (val === num) {
[ 434 ]
Practice Exercise, Project, and Self-Check Quiz Answers
[ 435 ]
Appendix
Self-check quiz
1. one
2. this is the one
3. login
4. Welcome, that is a user: John
5. Wake up, it's morning
6. Result:
• true
• false
• true
• true
7. Result:
100 was LESS or Equal to 100
100 is Even
Chapter 5, Loops
Practice exercises
Practice exercise 5.1
const max = 5;
const ranNumber = Math.floor(Math.random() * max) + 1;
//console.log(ranNumber);
let correct = false;
while (!correct) {
let guess = prompt("Guess a Number 1 - " + max);
guess = Number(guess);
if (guess === ranNumber) {
[ 436 ]
Practice Exercise, Project, and Self-Check Quiz Answers
correct = true;
console.log("You got it " + ranNumber);
} else if (guess > ranNumber) {
console.log("Too high");
} else {
console.log("Too Low");
}
}
[ 437 ]
Appendix
tempTable.push(counter);
}
myTable.push(tempTable);
}
console.table(myTable);
}
console.table(grid);
[ 438 ]
Practice Exercise, Project, and Self-Check Quiz Answers
[ 439 ]
Appendix
Alternatively, the following code could be used, replacing continue with break:
console.log(output);
[ 440 ]
Practice Exercise, Project, and Self-Check Quiz Answers
Project
Math multiplication table
const myTable = [];
const numm = 10;
for(let x=0; x<numm; x++){
const temp = [];
for(let y = 0; y<numm; y++){
temp.push(x*y);
}
myTable.push(temp);
}
console.table(myTable);
[ 441 ]
Appendix
Self-check quiz
1. Result:
0
3
6
9
2. Result:
0
5
1
6
2
7
[1, 5, 7]
Chapter 6, Functions
Practice exercises
Practice exercise 6.1
function adder(a, b) {
return a + b;
}
const val1 = 10;
const val2 = 20;
console.log(adder(val1, val2));
console.log(adder(20, 30));
function myFun() {
const question = prompt("What is your name?");
const nameAdj = Math.floor(Math.random() * adj.length);
console.log(adj[nameAdj] + " " + question );
[ 442 ]
Practice Exercise, Project, and Self-Check Quiz Answers
}
myFun();
(function () {
let val = "100"; // local scope variable
[ 443 ]
Appendix
console.log(val);
})();
(function (val) {
console.log(`My name is ${val}`);
})("Laurence");
[ 444 ]
Practice Exercise, Project, and Self-Check Quiz Answers
val--;
return loop2(val);
}
return;
}
loop2(start);
function test1(val){
console.log(val);
}
test1("hello 2");
Projects
Create a recursive function
const main = function counter(i) {
console.log(i);
if (i < 10) {
return counter(i + 1);
}
return;
}
main(0);
[ 445 ]
Appendix
Self-check quiz
1. 10
2. Hello
3. Answer:
Welcome
Laurence
My Name is Laurence
4. 19
5. 16
Chapter 7, Classes
Practice exercises
Practice exercise 7.1
class Person {
constructor(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
let person1 = new Person("Maaike", "van Putten");
let person2 = new Person("Laurence", "Svekis");
console.log("hello " + person1.firstname);
console.log("hello " + person2.firstname);
[ 446 ]
Practice Exercise, Project, and Self-Check Quiz Answers
[ 447 ]
Appendix
Projects
Employee tracking app
class Employee {
constructor(first, last, years) {
this.first = first;
this.last = last;
this.years = years;
}
}
const person1 = new Employee("Laurence", "Svekis", 10);
const person2 = new Employee("Jane", "Doe", 5);
const workers = [person1, person2];
Employee.prototype.details = function(){
return this.first + " " + this.last + " has worked here " +
this.years + " years";
}
workers.forEach((person) => {
console.log(person.details());
});
[ 448 ]
Practice Exercise, Project, and Self-Check Quiz Answers
Self-check quiz
1. class
2. Using the following syntax:
class Person {
constructor(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
3. Inheritance
4. Answers:
• True
• False
• True
• True
• False
5. B
[ 449 ]
Appendix
[ 450 ]
Practice Exercise, Project, and Self-Check Quiz Answers
[ 451 ]
Appendix
Projects
Word scrambler
let str = "JavaScript";
function scramble(val) {
let max = val.length;
let temp = "";
for(let i=0;i<max;i++){
console.log(val.length);
let index = Math.floor(Math.random() * val.length);
temp += val[index];
console.log(temp);
val = val.substr(0, index) + val.substr(index + 1);
console.log(val);
}
return temp;
}
console.log(scramble(str));
Countdown timer
const endDate = "Sept 1 2022";
function countdown() {
const total = Date.parse(endDate) - new Date();
const days = Math.floor(total / (1000 * 60 * 60 * 24));
const hrs = Math.floor((total / (1000 * 60 * 60)) % 24);
const mins = Math.floor((total / 1000 / 60) % 60);
const secs = Math.floor((total / 1000) % 60);
return {
days,
hrs,
mins,
secs
};
}
function update() {
const temp = countdown();
[ 452 ]
Practice Exercise, Project, and Self-Check Quiz Answers
update();
Self-check quiz
1. decodeURIComponent(e)
2. 4
3. ["Hii", "hi", "hello", "Hii", "hi", "hi World", "Hi"]
4. ["hi", "hi World"]
[ 453 ]
Appendix
Projects
Manipulating HTML elements with JavaScript
const output = document.querySelector(".output");
const mainList = output.querySelector("ul");
mainList.id = "mainList";
[ 454 ]
Practice Exercise, Project, and Self-Check Quiz Answers
console.log(mainList);
const eles = document.querySelectorAll("div");
for (let x = 0; x < eles.length; x++) {
console.log(eles[x].tagName);
eles[x].id = "id" + (x + 1);
if (x % 2) {
eles[x].style.color = "red";
} else {
eles[x].style.color = "blue";
}
}
Self-check quiz
1. You should see an object representing the list of elements contained within
body object of the HTML page.
[ 455 ]
Appendix
<title>JS Tester</title>
</head>
<body>
<h1>Test</h1>
<script>
const output = document.querySelector('h1');
output.textContent = "Hello World";
</script>
</body>
</html>
[ 456 ]