Skip to content

Commit c3dd177

Browse files
committed
Fix PR feedback: lastSlashIndex, Math.floor(), const vs let, replaceAll, random min, and safe movie length calculations with edge case handling
1 parent 38532cf commit c3dd177

File tree

7 files changed

+87
-37
lines changed

7 files changed

+87
-37
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111

1212
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
1313
const lastSlashIndex = filePath.lastIndexOf("/");
14-
const base = filePath.slice(-8);
15-
console.log(`The base part of ${filePath} is ${base}`);
14+
const base = filePath.substring(lastSlashIndex + 1);
15+
console.log(`Base part: ${base}`);
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 = filePath.slice(0, -8);
21-
const ext = filePath.slice(filePath.length-3);
20+
const dir = filePath.substring(0, lastSlashIndex + 1);
2221

23-
console.log(`the dir part of filePath variable is ${dir}`);
24-
console.log(ext);
22+
const dotIndex = filePath.lastIndexOf(".");
23+
const ext = filePath.substring(dotIndex + 1);
24+
25+
console.log(`Dir part: ${dir}`);
26+
console.log(`Ext part: ${ext}`);
2527

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

28-
// After studying the slice() method I was able to do this task easily.
30+
// After studying the slice() method I was able to do this task easily.
31+
// The method I used is not working with all, for example if we change the name of our file from file.txt to best-file-ever.txt then out put is not what we want. ===> ever.txt; so we should find an other way ...
32+
// to solve that we better use substring(lastSlashIndex + 1)method. this method works with any name we add.
33+
// Before I used slice() which was working for the specific name or path but not for all type. and now this substring() method does work well almost on any type of path.
34+
// thanks to "hkavalikas" because of his feedback I learned this useful method.

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ console.log(num);
1212
// num is a variable that will be assigned an integer number after the operators are done.
1313
// Math.floor() rounds a decimal to its nearest number to make it an integer. ex = 1.2 => 1.
1414
// Math.random() is a method used to create numbers between (0-1) and usually it creates a decimal like (0,2) or etc.
15-
// each time I run it, I got different number as Math.random() generates different number each time we run it.
15+
// each time I run it, I got different number as Math.random() generates different number each time we run it.
16+
17+
//to respond to the question "What would happen if we did Math.floor(1.9);?"
18+
// ===> the method Math.floor() rounds the decimal number down always, in this case the final output would be ==> 2 as our 1.9 would be 1 after our method round it and add it to our minimum number which is 1 so ==> 1 + 1 = 2.
19+
20+
// In addition our formula works we wont have 191, if we had Math.ceil() then the result would exceed our maximum number.
21+
22+
// Response to question "What does the + minimum do here?"
23+
// ===> + minimum shifts the range so that the smallest possible number is minimum instead of 0. so if we don't have it our range would be (0, 99) and this as we have 1 as minimum and 100 as maximum, if we don't add + minimum then our formula wont work as we are expecting.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
This is just an instruction for the first activity - but it is just for human consumption
3-
We don't want the computer to run these 2 lines - how can we solve this problem?
2+
/*This is just an instruction for the first activity - but it is just for human consumption
3+
We don't want the computer to run these 2 lines - how can we solve this problem?*/
44

55
//solution: Simply comment the lines.
66
// 1- single line comment like: // example ...

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// trying to create an age variable and then reassign the value by 1
22

3-
const age = 33;
4-
//age = age + 1;
3+
let age = 33;
4+
age = age + 1; // ===> or age++
5+
console.log(age); // ===> 34
56

67
// we can't do that with const variable, because JS locks the reference.
78
// the best choice is to use "let" variable.
8-
let age2 = 33;
9-
age2 = age2 + 1; // or age++ if wanna increase by "1";
10-
console.log(age2);
9+
10+

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +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-
// line: 4 and line 5 and it is replaceAll().
15+
// ====> Function calls occur on lines 4 and 5 (Number() and replaceAll()), and on line 10 (console.log()).
16+
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?
17-
// error is coming from line 5. The error is because of not putting comma between the inputs of replaceAll function.
18-
// To fix the problem just add a comma between the inputs of replaceAll function.
18+
// ====> error is coming from line 5. The error is because of not putting comma between the parameter of replaceAll function.
19+
// To fix the problem, add a comma between the two arguments of the replaceAll function, since this method requires two parameters.
20+
1921
// c) Identify all the lines that are variable reassignment statements
20-
// Line 4 and 5.
22+
// ===> Line 4 (carPrice) and line 5 (priceAfterOneYear).
23+
2124
// d) Identify all the lines that are variable declarations
22-
// we have variable declarations at lines 1, 2, 7 and 8.
25+
// ====> we have variable declarations at lines 1, 2 with let and with const at line 7 and 8.
26+
2327
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
24-
// the purpose of that expression is to turn into number the string and replace all commas with an empty space.
28+
// ===> the purpose of that expression is to remove all commas from the string "1,000" to "1000" and turn the string into number "1000" to 1000.
Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
1-
const movieLength = 8724; // length of movie in seconds
21

3-
const remainingSeconds = movieLength % 60;
4-
const totalMinutes = (movieLength - remainingSeconds) / 60;
2+
const movieLength = 8777; // length of movie in seconds
3+
if (typeof movieLength !== "number") {
4+
throw new Error("Movie length must be a number"); // Checks if it’s a number ==> prevents strings, null, undefined;
5+
} else if (movieLength < 0) {
6+
throw new Error("Movie length cannot be negative"); // Checks if it’s negative;
7+
} else if(!Number.isSafeInteger(movieLength)) {
8+
throw new Error("Movie length is too large"); // Checks if it’s too large ==> prevents unsafe numbers in JS;
9+
}
10+
11+
let safeMovieLength = Math.floor(Number(movieLength)); // Creates clean and positive integer;
12+
13+
const remainingSeconds = safeMovieLength % 60;
14+
const totalMinutes = (safeMovieLength - remainingSeconds) / 60;
515

616
const remainingMinutes = totalMinutes % 60;
717
const totalHours = (totalMinutes - remainingMinutes) / 60;
818

9-
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
10-
console.log(result);
19+
const movieDuration = `${String(totalHours).padStart(2, "0")}:${String(remainingMinutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
20+
console.log(`The movie length is --> ${movieDuration}`);
1121

1222
// For the piece of code above, read the code and then answer the following questions
1323

1424
// a) How many variable declarations are there in this program?
15-
// we have 6 variable declarations.
25+
// we have 6 (const) variable declarations.
26+
1627
// b) How many function calls are there?
17-
// here we have only one which is console.log().
28+
// ===> we have only one, which is console.log() on line 10.
1829
// c) Using documentation, explain what the expression movieLength % 60 represents
1930
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
20-
// Remainder operator (%), returns the leftover when one operand divided by the other operand.
21-
// movieLength % 60 expression represents the remaining seconds.
31+
32+
// The % operator gives the remainder after dividing two numbers.
33+
// We divide the total seconds (movieLength) by 60 to see how many full minutes there are. Also the % operator gives the leftover seconds that do not make a full minute, this is what movieLength % 60 expression represents.
34+
2235
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
23-
// that expression turns the seconds into minutes.
36+
// ===> First we remove the leftover seconds that don’t make a full minute. Then we divide the remaining seconds by 60 to find how many full minutes there are. For example, 8724 seconds minus 24 leftover seconds gives 8700 seconds, and 8700 / 60 = 145 minutes. So totalMinutes = 145;
37+
2438
// e) What do you think the variable result represents? Can you think of a better name for this variable?
25-
// the result variable represents the movie length in Hour, minutes and seconds.
26-
// a better name can be "movieLength".
39+
// ===> The result variable is a string and represents the movie length format in Hour, minutes and seconds.
40+
// ===> A better name can be " movieDuration or movieLengthFormat". and I replaced the result with movieDuration.
41+
42+
2743
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
28-
// as I changed the value of the movieLength the results has changed too.It means this code works with any value.
44+
// ===> As I changed the value of the movieLength the results has changed too.It means this code works with any value.
45+
46+
// ===> Response to PR review question: Can you see any problem if we were to name it movieLength?
47+
// We should not name the result variable movieLength because that name is already used for the total seconds. Using the same name would cause an error or confusion. It’s better to choose a new name like movieDuration or movieLengthFormat.
48+
49+
// Respond to question: "There are a few cases that might break this. Can you spot and mention a few?"
50+
51+
// ===> A few cases might break this code:
52+
// If movieLength is negative, we could get negative hours, minutes, or seconds.
53+
// If movieLength is not an integer, the minutes or hours could have decimals.
54+
// Very large values might not display nicely without padding.
55+
// Single-digit minutes or seconds are not padded (like 5 instead of 05).
56+
// If movieLength is not a number, the calculations will fail.” */
57+
58+
// to solve the edge cases, we need to write some more lines of code. Which I did add in above codes with comments.

Sprint-1/4-stretch-explore/objects.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ In this activity, we'll explore some additional concepts that you'll encounter i
44

55
Open the Chrome devtools Console, type in `console.log` and then hit enter
66

7-
What output do you get?
7+
What output do you get?
8+
==> this is the output I got: ƒ log() { [native code] }
89

910
Now enter just `console` in the Console, what output do you get back?
11+
==> I got this: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}
1012

11-
Try also entering `typeof console`
13+
Try also entering `typeof console` ===> I got : 'object'.
1214

1315
Answer the following questions:
1416

1517
What does `console` store?
1618

17-
`Answer` => console stores methods(function) that you can call to perform actions like logging, warning, or displaying error.
19+
`Answer` => console stores methods(function) that you can call to perform actions like logging, warning, or displaying error.
1820

1921
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
2022

0 commit comments

Comments
 (0)