Skip to content

Commit 5ed6319

Browse files
committed
Exercises redone
1 parent 3372770 commit 5ed6319

12 files changed

Lines changed: 151 additions & 12 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ 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+
//ANS:
8+
// Line 3 is incrementing the variable.
9+
// It evaluates the expression on the right using the variable's current state,
10+
// then uses the = operator to store that new result back into the variable, effectively updating it.

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); // Output: "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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,26 @@
1111

1212
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
1313
const lastSlashIndex = filePath.lastIndexOf("/");
14+
// 1. Get the 'base' (file.txt)
1415
const base = filePath.slice(lastSlashIndex + 1);
1516
console.log(`The base part of ${filePath} is ${base}`);
1617

1718
// Create a variable to store the dir part of the filePath variable
1819
// Create a variable to store the ext part of the variable
1920

20-
const dir = ;
21-
const ext = ;
21+
//const dir = ;
22+
//const ext = ;
23+
24+
// 2. Get the 'dir' (/Users/mitch/cyf/Module-JS1/week-1/interpret)
25+
// We slice from the start up to the last slash
26+
const dir = filePath.slice(0, lastSlashIndex);
27+
28+
// 3. Get the 'ext' (.txt)
29+
// We find the last dot and slice from there to the end
30+
const lastDotIndex = filePath.lastIndexOf(".");
31+
const ext = filePath.slice(lastDotIndex);
32+
33+
console.log(`Dir: ${dir}`);
34+
console.log(`Ext: ${ext}`);
2235

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ 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+
11+
//ANS:
12+
//num represents a randomly generated integer between a specified minimum (1) and maximum (100), inclusive.
13+
//Math.random()This function returns a floating-point (decimal) number that is greater than or equal to $0$ but strictly less than $1$
14+
//(maximum - minimum + 1) represents the total number of possible integers within the range (from 1 to 100)
15+
// Math.random() * 100: multiplication of random decimal by range.
16+
// Adding the minimum value (1) to the result "shifts" the entire range upward so it doesn't start at zero.
17+
//Math.floor() rounds a number down to the nearest whole integer.

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
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+
//ANS:
5+
//Use two forward slashes //

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@
22

33
const age = 33;
44
age = age + 1;
5+
6+
7+
//ANS:
8+
//Use let
9+
//let allows you to create a variable whose value can be reassigned as many times as you like.
10+
11+
let age = 33; // Use 'let' instead of 'const'
12+
age = age + 1;
13+
14+
console.log(age);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@
33

44
console.log(`I was born in ${cityOfBirth}`);
55
const cityOfBirth = "Bolton";
6+
7+
8+
//ANS:
9+
//"Cannot access 'cityOfBirth' before initialization."
10+
//SOlution:
11+
12+
const cityOfBirth = "Bolton"; // 1. Define it first
13+
console.log(`I was born in ${cityOfBirth}`); // 2. Use it second

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ const last4Digits = cardNumber.slice(-4);
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+
8+
//ANS: code will throw an error
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?
11+
12+
//ANS:
13+
//The .slice() method is a String (and Array) method. cardNumber is defined as a Number. Numbers in JavaScript do not have a .slice() property
14+
915
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
16+
17+
18+
//ANS:
19+
const cardNumber = 4533787178994213;
20+
21+
// Convert to string first, then slice
22+
const last4Digits = cardNumber.toString().slice(-4);
23+
24+
console.log(last4Digits); // "4213"

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
const 12HourClockTime = "20:53";
2-
const 24hourClockTime = "08:53";
1+
//const 12HourClockTime = "20:53";
2+
//const 24hourClockTime = "08:53";
3+
4+
//FIx:
5+
const time12Hour = "08:53 PM";
6+
const time24Hour = "20:53";

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,32 @@ 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-
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?
17-
15+
//ANS:There are 6 function calls in total.
16+
carPrice.replaceAll(",", "")(Line 4)
17+
Number(...)(Line 4)
18+
priceAfterOneYear.replaceAll("," "")(Line 5)
19+
Number(...)(Line 5)
20+
console.log(...)(Line 10)
21+
percentageChange inside the template literal(the engine calls a string conversion here).
22+
// 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?
23+
//ANS: error here -> priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
24+
//FIX:
25+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1826
// c) Identify all the lines that are variable reassignment statements
19-
27+
//ANS:
28+
carPrice = Number(carPrice.replaceAll(",", ""));
29+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
2030
// d) Identify all the lines that are variable declarations
21-
31+
//ANS:
32+
let carPrice = "10,000";
33+
let priceAfterOneYear = "8,543";
34+
const priceDifference = carPrice - priceAfterOneYear;
35+
const percentageChange = (priceDifference / carPrice) * 100;
2236
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
37+
//ANS:
38+
//This expression is performing Data Cleaning and Type Conversion.
39+
carPrice.replaceAll(",", "") // This finds every comma in the string "10,000" and removes it, resulting in the string "10000".
40+
41+
Number(...) //This takes that cleaned string and converts it into an actual Number data type.
42+
43+
//The Purpose: Computers cannot perform mathematical calculations(like division or subtraction) on strings that contain formatting characters like commas.

0 commit comments

Comments
 (0)