Skip to content

Commit c5c92a4

Browse files
Birmingham | 26-ITP-May | Tobias Amaechina | Sprint 1 | coursework
1 parent b31a586 commit c5c92a4

13 files changed

Lines changed: 96 additions & 19 deletions

File tree

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
let count = 0;
2-
3-
count = count + 1;
4-
5-
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
6-
// Describe what line 3 is doing, in particular focus on what = is doing

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

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

1010
// https://www.google.com/search?q=get+first+character+of+string+mdn
11-
11+
console.log(initials); // Output: "CKJ"

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ 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+
const lastDot = filePath.lastIndexOf(".");
22+
const ext = filePath.slice(lastDot);
23+
24+
console.log(`The dir part of ${filePath} is ${dir}`);
25+
console.log(`The ext part of ${filePath} is ${ext}`);
26+
2227

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

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

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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+
console.log(age);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
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}`);
4+
5+
/*console.log(`I was born in ${cityOfBirth}`);
6+
const cityOfBirth = "Bolton";*/
7+
/* The error is that the variable `cityOfBirth` is being used before it is declared and assigned a value. In JavaScript, variables declared with `const` (or `let`) are not hoisted in the same way as `var`, so you cannot access them before their declaration. To fix this, you should declare and assign the variable before using it in the `console.log` statement:*/
8+
//The variable were being accessed from the Temporal Dead Zone (TDZ) before it was declared and assigned a value. In JavaScript, variables declared with `const` (or `let`) are not hoisted in the same way as `var`, so you cannot access them before their declaration. To fix this, you should declare and assign the variable before using it in the `console.log` statement:
9+
510
const cityOfBirth = "Bolton";
11+
12+
console.log(`I was born in ${cityOfBirth}`);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
//console.log(typeof cardNumber);
3+
const cardNumberStr = cardNumber.toString();
4+
5+
const last4Digits = cardNumberStr.slice(-4);
6+
console.log(last4Digits); // Output: "4213"
37

48
// The last4Digits variable should store the last 4 digits of cardNumber
59
// However, the code isn't working
610
// Before running the code, make and explain a prediction about why the code won't work
711
// Then run the code and see what error it gives.
812
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
913
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
14+
// I suspect the code doesn't work because the slice method is being called on a number, but slice is a method for strings. Therefore, I predict that the code will throw an error saying that slice is not a function for numbers.
15+
16+
// i console the typeof cardNumber and it returns number
17+
18+

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
const twelveHourClockTime = "8:53pm";
2+
const twentyFourHourClockTime = "20:53";
3+
/*names beginning with $ or _ often carry special conventions:
4+
5+
_name often implies a private/internal variable.
6+
$name is commonly associated with libraries like jQuery or special framework objects.*/
7+
/*For ordinary variables, it's usually clearer to start with a letter and use camelCase and avoid special characters, not starting with a number. and choosing meaningful names that describe the variable's purpose.*/

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

Lines changed: 21 additions & 2 deletions
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,30 @@ 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 file. The function calls are made on the following lines:
16+
// Line 1: replaceAll()
17+
// Line 2: replaceAll()
18+
// Line 5: Number()
19+
// Line 6: Number()
20+
// Line 9: console.log()
1521

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

23+
// 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?
24+
// The error is occurring on line 5., and it's a SyntaxError: missing , The error is due to a missing comma in the replaceAll() method.//The error occurs on line 5. A SyntaxError is thrown because a comma is missing between the two arguments passed to the replaceAll() method. JavaScript uses commas to separate arguments in a function call. The programming term that belongs to the blank is call arguments as that is the actual value passed during a function call. The correct syntax should be replaceAll(",", ""). To fix this problem, we need to add the missing comma in the replaceAll() method on line 5
1825
// c) Identify all the lines that are variable reassignment statements
26+
// The variable reassignment statements are on the following lines:
27+
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
28+
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,""));
1929

2030
// d) Identify all the lines that are variable declarations
31+
// The variable declaration statements are on the following lines:
32+
// Line 1: let carPrice = "10,000";
33+
// Line 2: let priceAfterOneYear = "8,543";
34+
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
35+
// Line 8: const percentageChange = (priceDifference / carPrice) * 100;
36+
// A declaration is when a variable is created, using let or const.
37+
38+
2139

2240
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
41+
/* The expression Number(carPrice.replaceAll(",","")) is converting the string value of carPrice, which contains a comma, into a number. The replaceAll() method is used to remove all commas from the string, and then the Number() function is used to convert the resulting string into a number. This allows for mathematical operations to be performed on the value of carPrice without any issues caused by the presence of commas in the string.*/

0 commit comments

Comments
 (0)