Skip to content

Commit 6818ea5

Browse files
committed
Complete task for the Sprint-1
1 parent b31a586 commit 6818ea5

12 files changed

Lines changed: 75 additions & 44 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ let count = 0;
33
count = count + 1;
44

55
// 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
6+
// Line 3 is a variable reassignment. The = (assignment operator) takes the result of count + 1
7+
// (which evaluates to 1) and stores it back into the existing count variable, replacing the old value.

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: 2 additions & 2 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 = ;
20+
const dir = filePath.slice(0, lastSlashIndex);
21+
const ext = base.slice(base.lastIndexOf("."));
2222

2323
// https://www.google.com/search?q=slice+mdn

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55

6+
console.log(num);
7+
68
// 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
9+
// num represents a random integer between 1 and 100 (inclusive).
10+
//
11+
// Breakdown:
12+
// 1. Math.random() generates a random decimal in [0, 1)
13+
// 2. (maximum - minimum + 1) = 100, so Math.random() * 100 gives a decimal in [0, 100)
14+
// 3. Math.floor(...) rounds down to an integer in [0, 99]
15+
// 4. Adding minimum (1) shifts the range to [1, 100]
16+
//
17+
// So num is a random whole number from 1 to 100.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
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?
3+
// Solution: Prefix each line with // to make them comments, as done here.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// trying to create an age variable and then reassign the value by 1
2+
// Error: Assignment to constant variable.
3+
// const variables cannot be reassigned. Fix: use let instead.
24

3-
const age = 33;
5+
let age = 33;
46
age = age + 1;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
2-
// what's the error ?
2+
// Error: ReferenceError - cityOfBirth is used before it is declared.
3+
// const is not hoisted like var. Fix: declare the variable before using it.
34

4-
console.log(`I was born in ${cityOfBirth}`);
55
const cityOfBirth = "Bolton";
6+
console.log(`I was born in ${cityOfBirth}`);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
// Prediction: .slice() is a string method, not a number method, so it will throw a TypeError.
2+
// Error: TypeError: cardNumber.slice is not a function
3+
// Fix: convert the number to a string first.
4+
15
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
6+
const last4Digits = cardNumber.toString().slice(-4);
37

8+
console.log(last4Digits);
49
// The last4Digits variable should store the last 4 digits of cardNumber
5-
// However, the code isn't working
6-
// Before running the code, make and explain a prediction about why the code won't work
7-
// Then run the code and see what error it gives.
8-
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
9-
// Then try updating the expression last4Digits is assigned to, in order to get the correct value

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
// Error: variable names cannot start with a digit.
2+
// Fix: rename the variables to valid identifiers.
3+
const twelveHourClockTime = "8:53pm";
4+
const twentyFourHourClockTime = "20:53";
5+
6+
console.log(twentyFourHourClockTime);
7+
console.log(twelveHourClockTime);

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@ 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;
99

1010
console.log(`The percentage change is ${percentageChange}`);
1111

12-
// Read the code and then answer the questions below
12+
// a) Function calls:
13+
// Line 4: replaceAll(",", "") and Number(...)
14+
// Line 5: replaceAll(",", "") and Number(...)
15+
// Line 10: console.log(...)
1316

14-
// a) How many function calls are there in this file? Write down all the lines where a function call is made
17+
// b) The error on line 5 is a missing comma between the two arguments to replaceAll:
18+
// replaceAll("," "") should be replaceAll(",", ""). Fixed above.
1519

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?
20+
// c) Variable reassignment statements: lines 4 and 5 (carPrice = ..., priceAfterOneYear = ...)
1721

18-
// c) Identify all the lines that are variable reassignment statements
22+
// d) Variable declarations: lines 1, 2 (let), lines 7, 8 (const)
1923

20-
// d) Identify all the lines that are variable declarations
21-
22-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
24+
// e) Number(carPrice.replaceAll(",","")) first removes all commas from the string "10,000"
25+
// turning it into "10000", then converts that string into the number 10000.
26+
// This is necessary because you cannot do arithmetic on strings.

0 commit comments

Comments
 (0)