Skip to content

Commit 0a4e7f6

Browse files
Complete Sprint 1 coursework
1 parent 3372770 commit 0a4e7f6

12 files changed

Lines changed: 151 additions & 16 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ 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+
//answer
9+
// Line 3 reassigns the value of count.
10+
// The = operator assigns the result of (count + 1)
11+
// back to the variable count.
12+
// This increases the previous value of count by 1.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ let middleName = "Katherine";
33
let lastName = "Johnson";
44

55
// Declare a variable called initials that stores the first character of each string.
6-
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
6+
// This should produce the string "CKJ", but you must not write the characters C, K,
7+
// or J in the code of your solution.
78

8-
let initials = ``;
9+
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;
10+
console.log(initials);
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ 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+
22+
const lastDotIndex = filePath.lastIndexOf(".");
23+
24+
const ext = filePath.slice( lastDotIndex+1);
25+
2226

2327
// https://www.google.com/search?q=slice+mdn
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
const minimum = 1;
22
const maximum = 100;
3-
3+
// Generate a random integer between minimum and maximum (inclusive)
44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
console.log(num);
6+
57

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+
/*
14+
Explanation:
15+
16+
num represents a random whole number between 1 and 100 inclusive.
17+
18+
How it works:
19+
20+
1. (maximum - minimum + 1)
21+
Calculates how many numbers are in the range.
22+
100 - 1 + 1 = 100
23+
24+
2. Math.random()
25+
Generates a decimal number such that:
26+
0 ≤ value < 1
27+
28+
3. Multiply:
29+
Math.random() * 100
30+
Produces a decimal between 0 and 99.999...
31+
32+
4. Math.floor()
33+
Removes the decimal part, giving an integer between 0 and 99.
34+
35+
5. + minimum
36+
Shifts the range from 0–99 to 1–100.
37+
6. When running the program multiple times, num changes each time.
38+
The value is always a whole number between 1 and 100.*/

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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+

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

Lines changed: 2 additions & 1 deletion
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...
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+

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
// Original broken version
12
const cardNumber = 4533787178994213;
23
const last4Digits = cardNumber.slice(-4);
4+
console.log(last4Digits);
5+
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+
15+
// Prediction:
16+
// The code will fail because cardNumber is a number.
17+
// Numbers do not have a slice() method.
18+
19+
// After running:
20+
// TypeError: cardNumber.slice is not a function
21+
22+
// Why:
23+
// slice() only works on strings and arrays.
24+
25+
// Fix:
26+
const cardNumberString = "4533787178994213";
27+
const fixedLast4Digits = cardNumberString.slice(-4);
28+
console.log(fixedLast4Digits); // 4213

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 Time12HourClock = "08:53 PM";
2+
const Time24HourClock = "20:53";
3+
console.log(Time12HourClock);
4+
console.log(Time24HourClock);

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,40 @@ 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+
1213
// Read the code and then answer the questions below
1314

1415
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15-
16+
//there are 5 functions calls in line 4, 5 and 10
17+
//carPrice = Number(carPrice.replaceAll(",", ""));: Number(), replaceAll()
18+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));:Number(), replaceAll()
19+
//console.log(`The percentage change is ${percentageChange}`); :console.log()
20+
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?
17-
22+
//The error was coming from line 5:
23+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
24+
//The error occurred because there was a missing comma between the two arguments inside the replaceAll() function.
25+
//To fix the problem, we add the missing comma: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
1826
// c) Identify all the lines that are variable reassignment statements
19-
27+
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
28+
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
2029
// d) Identify all the lines that are variable declarations
30+
// Line 1: let carPrice = "10,000";
31+
// Line 2: let priceAfterOneYear = "8,543";
32+
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
33+
// Line 8: const percentageChange = (priceDifference / carPrice) * 100;
2134

2235
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
36+
// The expression Number(carPrice.replaceAll(",", "")) first removes all commas
37+
// from the string stored in carPrice using replaceAll().
38+
// For example, "10,000" becomes "10000".
39+
// Then Number() converts the cleaned string into a numeric value.
40+
// The purpose of this expression is to convert a formatted string price
41+
// into a number so that mathematical calculations can be performed.

0 commit comments

Comments
 (0)