Skip to content

Commit 67fc0f6

Browse files
committed
Pull in solutions branch from Module-Structuring-And-Testing-Data
1 parent e43e9f4 commit 67fc0f6

28 files changed

Lines changed: 764 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let count = 0;
2+
3+
count = count + 1;
4+
5+
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
6+
// Describe what line 3 is doing, in particular focus on what = is doing
7+
8+
// =============> assign the value of count + 1 to count
9+
// =============> count held the value 0, so count + 1 = 1
10+
// =============> the value of count is now 1
11+
12+
console.assert(count === 1);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let firstName = "Creola";
2+
let middleName = "Katherine";
3+
let lastName = "Johnson";
4+
5+
// Declare a variable called initials that stores the first character of each string.
6+
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
7+
8+
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;
9+
10+
console.assert(initials === "CKJ");
11+
12+
// https://www.google.com/search?q=get+first+character+of+string+mdn
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The diagram below shows the different names for parts of a file path on a Unix operating system
2+
3+
// ┌─────────────────────┬────────────┐
4+
// │ dir │ base │
5+
// ├──────┬ ├──────┬─────┤
6+
// │ root │ │ name │ ext │
7+
// " / home/user/dir / file .txt "
8+
// └──────┴──────────────┴──────┴─────┘
9+
10+
// (All spaces in the "" line should be ignored. They are purely for formatting.)
11+
12+
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
13+
const lastSlashIndex = filePath.lastIndexOf("/");
14+
const base = filePath.slice(lastSlashIndex + 1);
15+
console.log(`The base part of ${filePath} is ${base}`);
16+
17+
// Create a variable to store the dir part of the filePath variable
18+
// Create a variable to store the ext part of the variable
19+
20+
const dir = filePath.slice(0, lastSlashIndex);
21+
const ext = base.slice(base.lastIndexOf("."));
22+
23+
console.assert(dir === "/Users/mitch/cyf/Module-JS1/week-1/interpret");
24+
console.assert(ext === ".txt");
25+
26+
// https://www.google.com/search?q=slice+mdn
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const minimum = 1;
2+
const maximum = 100;
3+
4+
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
6+
// In this exercise, you will need to work out what num represents?
7+
// Try breaking down the expression and using documentation to explain what it means
8+
// It will help to think about the order in which expressions are evaluated
9+
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
11+
console.log(num);
12+
13+
// following the order of operations, the expression inside the Math.floor() function is evaluated first
14+
// Math.random() generates a random number between 0 and 1
15+
// multiplying this by (maximum - minimum + 1) will give a number between 0 and 100
16+
// moving out of the parentheses, the Math.floor() function rounds this number down to the nearest whole number
17+
// adding the minimum value of 1 to this will give a number between 1 and 100
18+
19+
console.assert(num >= 1 && num <= 100);
20+
console.assert(Number.isInteger(num));
21+
22+
// =========================> Question for reviewer
23+
// I think I understand what it does
24+
// But I'd like to know when I might use the Math.random() function?

‎Sprint-2/2-mandatory-errors/0.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This is just an instruction for the first activity - but it is just for human consumption
2+
// We don't want the computer to run these 2 lines - how can we solve this problem?

‎Sprint-2/2-mandatory-errors/1.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// trying to create an age variable and then reassign the value by 1
2+
3+
let age = 33;
4+
age = age + 1;
5+
6+
// =============> TypeError: Assignment to constant variable.
7+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_const_assignment
8+
// =============> I fixed the error by changing the const keyword to let, which allows the value to be reassigned

‎Sprint-2/2-mandatory-errors/2.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Currently trying to print the string "I was born in Bolton" but it isn't working...
2+
// what's the error ?
3+
4+
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`);
6+
7+
// =============> ReferenceError: Cannot access 'cityOfBirth' before initialization
8+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init
9+
// =============> I fixed the error by moving the console.log() statement below the declaration of the cityOfBirth variable

‎Sprint-2/2-mandatory-errors/3.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const cardNumber = 4533787178994213;
2+
3+
// The last4Digits variable should store the last 4 digits of cardNumber
4+
// However, the code isn't working
5+
// Before running the code, make and explain a prediction about why the code won't work
6+
// Then run the code and see what error it gives.
7+
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
8+
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
9+
10+
// =============> My prediction
11+
// I think the code will throw an error because the cardNumber variable is a Number and not a String data type
12+
// The slice() method is a String method and cannot be used on a Number
13+
// https://www.w3schools.com/js/js_datatypes.asp
14+
// I predict that the error will be a TypeError
15+
16+
// =============> The actual output
17+
// TypeError: cardNumber.slice is not a function
18+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function
19+
20+
// =============> My explanation
21+
// The error is as I predicted. I need to convert the cardNumber variable to a string before using the slice() method
22+
// And then convert the last4Digits variable back to a number
23+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number
24+
25+
const last4DigitsString = cardNumber.toString().slice(-4);
26+
const last4DigitsNumber = Number(last4DigitsString);
27+
28+
console.assert(last4DigitsNumber === 4213);

‎Sprint-2/2-mandatory-errors/4.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// const 12HourClockTime = "20:53";
2+
// const 24hourClockTime = "08:53";
3+
4+
const clockTime12 = "20:53";
5+
const clockTime24 = "08:53";
6+
7+
// ===========> Prediction
8+
// 12HourClockTime is not a valid variable name because it starts with a number.
9+
// If I try to run the code, I will get a SyntaxError
10+
11+
// ===========> Execution
12+
// SyntaxError: Invalid or unexpected token
13+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Identifier_after_number
14+
15+
// ===========> Solution
16+
// I needed to change the variable name to something that starts with a letter
17+
// Variables must begin with a letter, $, or _ . The first character cannot be a number.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
let carPrice = "10,000";
2+
let priceAfterOneYear = "8,543";
3+
4+
carPrice = Number(carPrice.replaceAll(",", ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
6+
7+
const priceDifference = carPrice - priceAfterOneYear;
8+
const percentageChange = (priceDifference / carPrice) * 100;
9+
10+
console.log(`The percentage change is ${percentageChange}`);
11+
12+
// Read the code and then answer the questions below
13+
14+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
// Line 4 calls the replaceAll() function and the Number() function
16+
// Line 5 calls the replaceAll() function and the Number() function
17+
// Line 10 calls the console.log() function
18+
// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
19+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
20+
// ^^^
21+
22+
// SyntaxError: missing ) after argument list
23+
// The error is occurring because there is a missing comma in the replaceAll() function
24+
// The replaceAll() function should be replaceAll(",", "") like the one on line 4
25+
// I've fixed it by adding the comma
26+
27+
// c) Identify all the lines that are variable reassignment statements
28+
// Line 4 and Line 5 are variable reassignment statements. The variables carPrice and priceAfterOneYear are being reassigned to new values
29+
30+
// d) Identify all the lines that are variable declarations
31+
// 1,2,7,8 are variable declarations. The variables carPrice, priceAfterOneYear, priceDifference, and percentageChange are being declared
32+
33+
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
34+
35+
// working from the inside out:
36+
// the replaceAll() function is removing the comma from the string stored in the carPrice variable
37+
// the Number() function is converting the string to a number

0 commit comments

Comments
 (0)