Skip to content

Commit d249dd2

Browse files
debugged and corrected the errors accordingly.
1 parent b31a586 commit d249dd2

14 files changed

Lines changed: 108 additions & 12 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 is updating the value of the count variable by adding 1 to its current value. In other words, it is re-assignining the count variable to be equal to its previous value plus 1. It is called a statement.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ 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

10-
// https://www.google.com/search?q=get+first+character+of+string+mdn
10+
console.log(initials);
1111

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

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ 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+
console.log(`The dir part of ${filePath} is ${dir}`);
22+
const ext = filePath.slice(filePath.lastIndexOf(".") + 1);
23+
console.log(`The ext part of ${filePath} is ${ext}`);
2224

23-
// https://www.google.com/search?q=slice+mdn
25+
// https://www.google.com/search?q=slice+mdn

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ const maximum = 100;
44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55

66
// In this exercise, you will need to work out what num represents?
7+
// num represents a random integer between the minimum and maximum values, inclusive. The expression math.random() picks any decinmal number between 0 and 1, where then it's multiplied by the change of min & max + 1. Then the Math.floor() rounds the number down to the nearest integer and finally the minimum value is added to ensure the result is withn the specified range.
8+
79
// Try breaking down the expression and using documentation to explain what it means
10+
/* Math.random() generates a random decimal number between 0 (inclusive) and 1(not inclusive).
11+
The expression (maximum - minimum +1) calculates the range of possible values, which is 100 - 1 + 1 = 100.
12+
Then the result of Math.random() is multiplied by the range, which gives a random decimal number between 0 and 100 (not inclusive).
13+
Math.floor() rounds the result down to the nearest integer, which gives a random integer between 0 and 99 (inclusive).
14+
Finally, adding the minimum value of 1 shifts the range up by 1, resulting in a random integer between 1 and 100 (inclusive). */
15+
816
// It will help to think about the order in which expressions are evaluated
917
// Try logging the value of num and running the program several times to build an idea of what the program is doing
18+
console.log(num);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
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?
2+
We don't want the computer to run these 2 lines - how can we solve this problem?
3+
// by adding a comment symbol in front of the lines, so that the computer ignores them when running the code.

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

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

33
const age = 33;
4-
age = age + 1;
4+
age = age + 1; // this line will cause an error because age is declared as constant and cannot be reassigned.
5+
6+
let age = 33;
7+
age += 1; // this line will work because age is declared as a variable and can be reassigned.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
22
// what's the error ?
3-
3+
// the error is that the variable cityOfBirth is being used before it is declared.
44
console.log(`I was born in ${cityOfBirth}`);
55
const cityOfBirth = "Bolton";

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
const last4Digits = `${cardNumber.slice(-4)}`;
33

44
// The last4Digits variable should store the last 4 digits of cardNumber
55
// However, the code isn't working
66
// Before running the code, make and explain a prediction about why the code won't work
7+
// The code won't work because cardNumber is a number, and the slice method is a string method. So the cardNumber value needs to be coverted to a string. It can be done by using the cardNumber.toString() method or by using string(cardNumber) method or even by using template literals as shown in the code snippet.
8+
79
// Then run the code and see what error it gives.
10+
// The error says cardNumber.slice is not a function. This is becuase the slice method is a string method and cardNumber is a number.
11+
812
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
13+
// At first I thought the error would be that last4Digits is not defined, but I learned that the error is actually because the slice method is being called on a number, which doesn't have that method.
914
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
15+
const last4Digits = cardNumber.toString().slice(-4); // first option
16+
const last4Digits = String(cardNumber).slice(-4); // second option
17+
const last4Digits = `${cardNumber}`.slice(-4); // third option

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
const 12HourClockTime = "8:53pm"; // this will throw an error because the variable name started with a number where it shouldn't be.
2+
const 24hourClockTime = "20:53"; // the same happens here as well.

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

Lines changed: 17 additions & 1 deletion
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;
@@ -12,11 +12,27 @@ console.log(`The percentage change is ${percentageChange}`);
1212
// Read the code and then answer the questions below
1313

1414
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
/* There are 5 function calls in this code snippet.
16+
1) carPrice = Number(carPrice.replaceAll(",", "")); --Number(...) & replaceAll(",", "") in this line
17+
2) priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); --Number(...) & replaceAll("," "") in this line as well.
18+
3) console.log(`The percentage change is ${percentageChange}`);
19+
*/
1520

1621
// 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?
22+
/* The error shown says ) missing after arguements in the expression of priceAfterOneYear. However when I looked into it carefully, the comma was missing between the arguments, not the ). I also used debugger in vscode and simply pointed out the problem by as on the debug console suggested.
23+
*/
1724

1825
// c) Identify all the lines that are variable reassignment statements
26+
/* carPrice = Number(carPrice.replaceAll(",", ""));
27+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
28+
*/
1929

2030
// d) Identify all the lines that are variable declarations
31+
/* let carPrice = "10,000";
32+
let priceAfterOneYear = "8,543";
33+
const priceDifference = carPrice - priceAfterOneYear;
34+
const percentageChange = (priceDifference / carPrice) * 100;
35+
*/
2136

2237
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
38+
// This expression is coverting the carPrice value by getting rid of the comma in the number and joining the number together. 10,000 ----> 10000

0 commit comments

Comments
 (0)