Skip to content

Commit 0e2a1e7

Browse files
committed
Completed the key exerices
1 parent 21b7a96 commit 0e2a1e7

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ 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+
//Line 3 is adding 1 to count variable and then saving this new value to count variable
9+
// = sign is being used to assign the new value of count to the count variable

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ let lastName = "Johnson";
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

88
let initials = ``;
9+
initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
910

1011
// https://www.google.com/search?q=get+first+character+of+string+mdn
11-

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
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-
console.log(`The base part of ${filePath} is ${base}`);
15+
console.log(`The base part of ${filePath} is: \n ${base}` + "\n");
1616

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(1, lastSlashIndex);
21+
console.log(`The dir part of ${filePath} is: \n ${dir}` + "\n");
22+
const ext = filePath.slice(filePath.lastIndexOf("."));
23+
console.log(`The ext part of ${filePath} is: \n ${ext}`);
2224

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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@ const minimum = 1;
22
const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
console.log(num);
56

67
// In this exercise, you will need to work out what num represents?
78
// Try breaking down the expression and using documentation to explain what it means
89
// It will help to think about the order in which expressions are evaluated
910
// Try logging the value of num and running the program several times to build an idea of what the program is doing
11+
12+
//num in the above code represents integer numbers from 1 to 100
13+
// Math.random() method used in the above code returns floating numbers between 0(inclusive) and 1(exclusive)
14+
/*and (maximum - minimum + 1) will give use the number that when multiplied by values between
15+
0(inclusive) and 1(exclusive) we will get results from 0 to 99.99999999..... including any decimal values in between
16+
and the Math.floor is used to get rid of the decimal values and return only numbers; so our
17+
values will become 0 to 99 only numbers
18+
and at the end part of the equation we are adding minimum value i.e. 1 which will result in
19+
assigning values from 1 to 100 in num; that's why we are getting only values from 1 to 100 in our num
20+
and every time we print the num in console.log() we can get any values between 1 to 100 depending on the radom value returned
21+
by the Math.random() method.*/

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ 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("," "")); //error line of code
6+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , "")); //fixed
7+
68

79
const priceDifference = carPrice - priceAfterOneYear;
810
const percentageChange = (priceDifference / carPrice) * 100;
@@ -12,11 +14,33 @@ console.log(`The percentage change is ${percentageChange}`);
1214
// Read the code and then answer the questions below
1315

1416
// a) How many function calls are there in this file? Write down all the lines where a function call is made
17+
/* There are three function calls in this file as shown by the following lines from the code:
18+
carPrice = Number(carPrice.replaceAll(",", ""));
19+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
20+
console.log(`The percentage change is ${percentageChange}`);
21+
22+
replaceAll and log are the functions in the above lines */
1523

1624
// 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?
25+
/* The error is coming from the following line inside the replaceAll method that accepts two parameters separated by comma
26+
but in our code there is no comma for the separation and that is the problem
27+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
28+
we can fix this by simply adding the comma after first parameter value as follows:
29+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , "")); */
1730

1831
// c) Identify all the lines that are variable reassignment statements
32+
/* The following two lines are the variable reassignment statements:
33+
carPrice = Number(carPrice.replaceAll(",", ""));
34+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); */
1935

2036
// d) Identify all the lines that are variable declarations
37+
/* All the lines that are variable declarations are as follows:
38+
carPrice = Number(carPrice.replaceAll(",", ""));
39+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); //error line of code
40+
const priceDifference = carPrice - priceAfterOneYear;
41+
const percentageChange = (priceDifference / carPrice) * 100; */
2142

2243
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
44+
/* This code is replacing all the comma characters with an empty character inside the carPrice String and then converting that String to a Number
45+
The purpose of doing this is because in the later part of code we are doing some Math calculations on this number
46+
and, therefore, we can't use String, we need to remove the commas in the String and convert that String into a number

0 commit comments

Comments
 (0)