Skip to content

Commit 49747e6

Browse files
committed
key exercises and mandatory errors completed.
1 parent 124ae45 commit 49747e6

File tree

8 files changed

+29
-13
lines changed

8 files changed

+29
-13
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ 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+
8+
//The right-hand side count + 1 is evaluated first to produce the value 1, then = assigns that results to count and overwrites the previous value of 0.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
99

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
// (All spaces in the "" line should be ignored. They are purely for formatting.)
1111

1212
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
13-
const lastSlashIndex = filePath.lastIndexOf("/");
14-
const base = filePath.slice(lastSlashIndex + 1);
13+
const lastSlashIndex = filePath.lastIndexOf("/"); // 44
14+
const base = filePath.slice(lastSlashIndex + 1); // "file.txt"
1515
console.log(`The base part of ${filePath} is ${base}`);
1616

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);
2221

23-
// https://www.google.com/search?q=slice+mdn
22+
const lastDotIndex = base.lastIndexOf(".");
23+
const ext = base.slice(lastDotIndex + 1);
24+
25+
// https://www.google.com/search?q=slice+mdn

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
const age = 33;
44
age = age + 1;
5+
6+
// The code will throw an error because age is declared as a const, which means it cannot be reassigned after its initial assignment. To fix this, age should be declared using let instead of const.

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 error is that the variable cityOfBirth is being used before it is declared and assigned a value. To fix this, the declaration should be before the console.log statement.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
3+
//
4+
5+
const last4Digits = cardNumber.toString().slice(-4);
36

47
// The last4Digits variable should store the last 4 digits of cardNumber
58
// However, the code isn't working
69
// Before running the code, make and explain a prediction about why the code won't work
710
// Then run the code and see what error it gives.
811
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
912
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
13+
14+
//Initially, the code will not work because slice is a method that exist on strings and arrays. While the cardNumber is a number, so it will throw an error when trying to slice it
15+
16+
//To fix this, we can convert the cardNumber to a string using the toString() method before applying the slice method.

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 = "20:53";
2-
const 24hourClockTime = "08:53";
1+
const twelveHourClockTime = "20:53";
2+
const twentyFourHourClockTime = "08:53";
3+
4+
// A variable name cannot start with a number.

0 commit comments

Comments
 (0)