Skip to content

Commit 852b8fe

Browse files
committed
fixing code error, explaining and interpreting codes
1 parent b31a586 commit 852b8fe

12 files changed

Lines changed: 56 additions & 12 deletions

File tree

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+
/* On line 3 it is reassigning the count variable with new variable by incrementing the count by 1
8+
= sign is used as assigning operator */

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
1313
const lastSlashIndex = filePath.lastIndexOf("/");
1414
const base = filePath.slice(lastSlashIndex + 1);
15+
const lastDotIndex = filePath.lastIndexOf(".");
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 = filePath.slice(0, lastSlashIndex);
22+
const ext = filePath.slice(lastDotIndex + 1);
2223

23-
// https://www.google.com/search?q=slice+mdn
24+
// https://www.google.com/search?q=slice+mdn

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ 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+
/* -This function give us any random integer from 1 to 100,including 1 and 100
11+
-Math.floor() is a built-in JavaScript function that rounds a number down to the nearest integer
12+
-Math.random () is a built-in JavaScript function that generates random number greater than or equal to
13+
0 and less than 1
14+
-first of all i will try to get a value [0, 1) from Math.random and multiple it by 100
15+
and apply Math.floor to the result which will give me the integer number and then add the minimum value i.e. 1 */

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
22
// what's the error ?
3-
4-
console.log(`I was born in ${cityOfBirth}`);
53
const cityOfBirth = "Bolton";
4+
console.log(`I was born in ${cityOfBirth}`);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
const last4Digits = cardNumber.toString().slice(-4);
33

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
77
// Then run the code and see what error it gives.
88
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
99
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
10+
/* the code is not working because of the variable type which is .slice method work on
11+
string and array variable types not on number variable types to fix it we can change the variable type to
12+
string variable type by adding double quotation marks to the variable.*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
const 12HourClockTime = "8:53pm";
22
const 24hourClockTime = "20:53";
3+
/*when we give a name for variable(identifiers) we can not start with number or any special characters */

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

Lines changed: 7 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(",", ""));
66

77
const priceDifference = carPrice - priceAfterOneYear;
88
const percentageChange = (priceDifference / carPrice) * 100;
@@ -12,11 +12,17 @@ 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 three function call in this file it is on line 4,5 and 10.
1516

1617
// 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?
18+
// The error is coming from line 5 and it is because it miss a comma between two argument.
1719

1820
// c) Identify all the lines that are variable reassignment statements
21+
// Variables are reassigned in two lines and the lines are in line 4 and 5.
1922

2023
// d) Identify all the lines that are variable declarations
24+
// Variables are declared in four different lines those lines are 1,2,7 and 8
2125

2226
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
27+
/* In this expression there is two function call when we start from the inside we got that replaceAll which will do is
28+
it replace a specific string patter with a given argument, the other one is Number which change the data type from string to number */

0 commit comments

Comments
 (0)