Skip to content

Commit 59e0e37

Browse files
committed
Complete Sprint 1 key exercises, mandatory errors, and interpret exercises
1 parent b31a586 commit 59e0e37

12 files changed

Lines changed: 40 additions & 18 deletions

File tree

Sprint-1/1-key-exercises/1-count.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ count = count + 1;
44

55
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
66
// Describe what line 3 is doing, in particular focus on what = is doing
7+
// LIne 3 adds 1 to the count and saves the new value back into count. here, = means "store this value", not "equals" like in maths.

Sprint-1/1-key-exercises/2-initials.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let lastName = "Johnson";
55
// Declare a variable called initials that stores the first character of each string.
66
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
77

8-
let initials = ``;
8+
let initials = firstName[0] + middleName[0] + lastName[0];
99

1010
// https://www.google.com/search?q=get+first+character+of+string+mdn
1111

Sprint-1/1-key-exercises/3-paths.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ console.log(`The base part of ${filePath} is ${base}`);
1717
// Create a variable to store the dir part of the filePath variable
1818
// Create a variable to store the ext part of the variable
1919

20-
const dir = ;
21-
const ext = ;
22-
20+
const dir = filePath.slice(0, lastSlashIndex);
21+
const lastDotIndex = filePath.lastIndexOf(".");
22+
const ext = filePath.slice(lastDotIndex);
2323
// https://www.google.com/search?q=slice+mdn

Sprint-1/1-key-exercises/4-random.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
77
// Try breaking down the expression and using documentation to explain what it means
88
// It will help to think about the order in which expressions are evaluated
99
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
// num is a random whole number between 1 and 100. Math.random() gives a decimal between 0 and 1, multiplying it stretches that range to 0-100, Math.floor rounds it down to a whole number, and + minimum shifts it so the lowest possible value is 1 instead of 0.
11+
12+

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +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?
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-1/2-mandatory-errors/1.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// trying to create an age variable and then reassign the value by 1
22

3-
const age = 33;
3+
let age = 33;
44
age = age + 1;
5+
// const variables cannot be reassigned once created. Using let instead allows the value to change.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
22
// what's the error ?
33

4-
console.log(`I was born in ${cityOfBirth}`);
54
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`);
6+
7+
// The console.log was written before the variable was declared, so JavaScript didn't know what cityOfBirth was yet. Moving the declaration above the console.log fixes it.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
const last4Digits = cardNumber.toString().slice(-4);
3+
4+
// cardNumber is a number, but .slice() only works on strings. Converting it with .toString() first lets us slice the last 4 digits.
35

46
// The last4Digits variable should store the last 4 digits of cardNumber
57
// However, the code isn't working

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
const twelveHourClockTime = "8:53pm";
2+
const twentyFourHourClockTime = "20:53";
3+
4+
// Variable names cannot begin with a number in JavaScript. Renaming them to start with a letter fixes the error.

Sprint-1/3-mandatory-interpret/1-percentage-change.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ let carPrice = "10,000";
22
let priceAfterOneYear = "8,543";
33

44
carPrice = Number(carPrice.replaceAll(",", ""));
5-
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
66

77
const priceDifference = carPrice - priceAfterOneYear;
88
const percentageChange = (priceDifference / carPrice) * 100;
@@ -11,12 +11,12 @@ console.log(`The percentage change is ${percentageChange}`);
1111

1212
// Read the code and then answer the questions below
1313

14-
// a) How many function calls are there in this file? Write down all the lines where a function call is made
14+
// a) 5 function calls: replaceAll and Number on line 4, replaceAll and Number on line 5, console.log on line 10.
1515

16-
// 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?
16+
// b) The error is on line 5. There is a comma missing between the two arguments of replaceAll, it should be replaceAll(",", "") not replaceAll("," "").
1717

18-
// c) Identify all the lines that are variable reassignment statements
18+
// c) Lines 4 and 5 are variable reassignment statements.
1919

20-
// d) Identify all the lines that are variable declarations
20+
// d) Lines 1, 2, 7, and 8 are variable declarations.
2121

22-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
22+
// e) Number(carPrice.replaceAll(",", "")) removes the commas from the string using replaceAll, then Number() converts that string into an actual number so maths can be done with it.

0 commit comments

Comments
 (0)