Skip to content

Commit 7ad1f91

Browse files
committed
I completed 4 exercises of week 1
- Key exercises: count, initials, paths, random numbers - Mandatory errors: fix all 5 error files (0.js - 4.js) - Mandatory implementations: percentage change, time format, currency conversion - Stretch goals: Chrome console and objects exploration
1 parent 8f3d6cf commit 7ad1f91

File tree

15 files changed

+3551
-12
lines changed

15 files changed

+3551
-12
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@ let count = 0;
22

33
count = count + 1;
44

5+
console.log(count); // should print 1
6+
57
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
68
// Describe what line 3 is doing, in particular focus on what = is doing
9+
10+
11+
// On line 3, the code `count = count + 1;` updates the value of the variable `count`. JavaScript first takes the current value stored in `count`, adds `1` to it, and then assigns the new result back into the same variable.
12+
// The `=` symbol here is the **assignment operator**. It does not mean “equals” in mathematics. Instead, it means “take the value on the right-hand side and store it in the variable on the left-hand side.”
13+
// After this line runs, `count` now contains the value `1` instead of `0`.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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]}`;
9+
10+
console.log(initials); // should print "CKJ"
911

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

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ 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); // dir part is "/Users/mitch/cyf/Module-JS1/week-1/interpret"
21+
const ext = base.slice(base.lastIndexOf(".")); // ext part is ".txt"
22+
23+
console.log(`The dir part of ${filePath} is ${dir}`); // print: The dir part of /Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt is /Users/mitch/cyf/Module-JS1/week-1/interpret
24+
console.log(`The ext part of ${filePath} is ${ext}`); // print: The ext part of /Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt is .txt
2225

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

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

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

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

6+
console.log(num); // Will output numbers like: 5, 42, 87, 100, etc.
7+
68
// In this exercise, you will need to work out what num represents?
79
// Try breaking down the expression and using documentation to explain what it means
810
// It will help to think about the order in which expressions are evaluated
911
// Try logging the value of num and running the program several times to build an idea of what the program is doing
12+
13+
// Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive)
14+
// Examples: 0.1, 0.5, 0.99
15+
// (maximum - minimum + 1) calculates the range size
16+
// 100 - 1 + 1 = 100
17+
// Math.random() * 100 creates numbers from 0 to 99.999...
18+
// Examples: 0.1 × 100 = 10, 0.5 × 100 = 50, 0.99 × 100 = 99
19+
// Math.floor() rounds down to the nearest integer
20+
// Results in integers from 0 to 99
21+
// + minimum shifts the range
22+
// Adds 1 to move from 0-99 to 1-100
23+
// The expression generates a random integer between the minimum value (1) and the maximum value (100), inclusive of both endpoints.
24+
// Final Answer: num is a random integer from 1 to 100

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
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+
4+
// We can use comments to "comment out" these lines

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
22

3-
const age = 33;
3+
let age = 33;
44
age = age + 1;
5+
6+
console.log(age);

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 ?
33

4-
console.log(`I was born in ${cityOfBirth}`);
54
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`); // moved the console.log below const

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+
const last4Digits = cardNumber.toString().slice(-4);
3+
4+
console.log(last4Digits); // should print 4213
35

46
// The last4Digits variable should store the last 4 digits of cardNumber
57
// However, the code isn't working
68
// Before running the code, make and explain a prediction about why the code won't work
79
// Then run the code and see what error it gives.
810
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
911
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
12+
13+
14+
// Before running the code, I predicted: The code will throw an error because cardNumber is defined as a number (not a string), and numbers don't have a .slice() method.
15+
// The .slice() method is only available on strings and arrays, not on number primitives
16+
// To fix the code, we need to convert the number to a string first, then use .slice()

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+
// updated the variable names to be more descriptive

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

Lines changed: 38 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(",", "")); // Fixed syntax error by adding missing comma
66

77
const priceDifference = carPrice - priceAfterOneYear;
88
const percentageChange = (priceDifference / carPrice) * 100;
@@ -20,3 +20,40 @@ console.log(`The percentage change is ${percentageChange}`);
2020
// d) Identify all the lines that are variable declarations
2121

2222
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
23+
24+
25+
// Answers
26+
27+
// a) Function Calls (5 total):
28+
29+
// carPrice.replaceAll(",", "") (Line 3)
30+
// Number() (Line 3)
31+
// priceAfterOneYear.replaceAll(",", "") (Line 4)
32+
// Number() (Line 4)
33+
//console.log() (Line 8)
34+
35+
// b) Error Fix:
36+
37+
// Error line: Line 4 - missing comma in .replaceAll("," "")
38+
// Fix: Change to .replaceAll(",", "")
39+
// Error type: SyntaxError: missing ) after argument list
40+
41+
// c) Variable Reassignments:
42+
43+
// carPrice = Number(carPrice.replaceAll(",", "")) (Line 3)
44+
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")) (Line 4)
45+
46+
// d) Variable Declarations:
47+
48+
//let carPrice = "10,000" (Line 1)
49+
//let priceAfterOneYear = "8,543" (Line 2)
50+
// const priceDifference = carPrice - priceAfterOneYear (Line 6)
51+
// const percentageChange = (priceDifference / carPrice) * 100 (Line 7)
52+
53+
// e) Expression Purpose:
54+
55+
// Number(carPrice.replaceAll(",", "")) converts a formatted currency string with commas into a numeric value for mathematical calculations:
56+
// Removes commas: "10,000" → "10000"
57+
// Converts to number: "10000" → 10000
58+
59+
// Output: The percentage change is 14.57 (14.57% price decrease)

0 commit comments

Comments
 (0)