You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log(`The base part of ${filePath} is${base}`);
14
+
constbase=filePath.substring(lastSlashIndex+1);
15
+
console.log(`Base part: ${base}`);
16
16
17
17
// Create a variable to store the dir part of the filePath variable
18
18
// Create a variable to store the ext part of the variable
19
19
20
-
constdir=filePath.slice(0,-8);
21
-
constext=filePath.slice(filePath.length-3);
20
+
constdir=filePath.substring(0,lastSlashIndex+1);
22
21
23
-
console.log(`the dir part of filePath variable is ${dir}`);
24
-
console.log(ext);
22
+
constdotIndex=filePath.lastIndexOf(".");
23
+
constext=filePath.substring(dotIndex+1);
24
+
25
+
console.log(`Dir part: ${dir}`);
26
+
console.log(`Ext part: ${ext}`);
25
27
26
28
// https://www.google.com/search?q=slice+mdn
27
29
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.
Copy file name to clipboardExpand all lines: Sprint-1/1-key-exercises/4-random.js
+9-1Lines changed: 9 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -12,4 +12,12 @@ console.log(num);
12
12
// num is a variable that will be assigned an integer number after the operators are done.
13
13
// Math.floor() rounds a decimal to its nearest number to make it an integer. ex = 1.2 => 1.
14
14
// 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.
// 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
+
22
35
// 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
+
24
38
// 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
+
27
43
// 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.
0 commit comments