From 739e416977c759f3c368775b58933faf2a39c763 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 11 Feb 2026 11:45:06 +0000 Subject: [PATCH 1/6] 1-key-exercises are done --- Sprint-1/1-key-exercises/1-count.js | 3 +++ Sprint-1/1-key-exercises/2-initials.js | 6 +++++- Sprint-1/1-key-exercises/3-paths.js | 13 +++++++++---- Sprint-1/1-key-exercises/4-random.js | 6 ++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..6e091340f 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,6 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing + +// Line 3 is assigning (count + 1) to the variable count. +// In other word adding 1 to the variable count. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..d33d79669 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,11 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. -let initials = ``; +let initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`; +console.log(initials); +console.log(typeof initials); // https://www.google.com/search?q=get+first+character+of+string+mdn +// I used the mdn documentation to know how to get the first character of a string. +// I used charAt() method that takes the index of a string and return the character of it. diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..076f3322f 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -11,13 +11,18 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(lastSlashIndex + 1); +const base = filePath.slice(-8); console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const dir = filePath.slice(0, -8); +const ext = filePath.slice(filePath.length-3); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +console.log(`the dir part of filePath variable is ${dir}`); +console.log(ext); + +// https://www.google.com/search?q=slice+mdn + +// After studying the slice() method I was able to do this task easily. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..f70a1aca6 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,14 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +console.log(num); // In this exercise, you will need to work out what num represents? // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing + +// num is a variable that will be assigned an integer number after the operators are done. +// Math.floor() rounds a decimal to its nearest number to make it an integer. ex = 1.2 => 1. +// Math.random() is a method used to create numbers between (0-1) and usually it creates a decimal like (0,2) or etc. +// each time I run it, I got different number as Math.random() generates different number each time we run it. \ No newline at end of file From f71288bb771930b78d1ef30bd0f79372ad13c733 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 11 Feb 2026 13:27:19 +0000 Subject: [PATCH 2/6] Mandatory errors were fixed and Explained --- Sprint-1/2-mandatory-errors/0.js | 8 +++++++- Sprint-1/2-mandatory-errors/1.js | 8 +++++++- Sprint-1/2-mandatory-errors/2.js | 6 +++++- Sprint-1/2-mandatory-errors/3.js | 11 ++++++++++- Sprint-1/2-mandatory-errors/4.js | 12 +++++++++++- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..680eec82d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,8 @@ + This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +We don't want the computer to run these 2 lines - how can we solve this problem? + +//solution: Simply comment the lines. +// 1- single line comment like: // example ... +// 2- multiline comment like : /* a, b, c */ +// comments are for instruction of guidance of who will read our codes, but the computer wont read them. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..9ca368df0 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,10 @@ // trying to create an age variable and then reassign the value by 1 const age = 33; -age = age + 1; +//age = age + 1; + +// we can't do that with const variable, because JS locks the reference. +// the best choice is to use "let" variable. +let age2 = 33; +age2 = age2 + 1; // or age++ if wanna increase by "1"; +console.log(age2); diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..d851e72c3 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,9 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + //const cityOfBirth = "Bolton"; + +// the problem is that our variable is not declared when we print it. +// to make it work, we should declare our "cityOfBirth" variable before we print it. diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..9f05fb1a3 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,14 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//const last4Digits = cardNumber.slice(-4); +let str = cardNumber.toString().slice(-4); +const last4Digits = Number(str); +console.log(`${last4Digits} is the last 4 digits of ${cardNumber}`); + +//console.log(typeof last4Digits); + +// 1- the code is not working because 'slice()' method is not working with numbers. +// 2- it gives "type error" cardNumber.slice is not a function. +// 3- my prediction was correct as I studied the slice method and knew it. // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..34e30245c 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,12 @@ +/* const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; +*/ +// As far as I know we can not start a variable name with a number in JS. +// Therefore these variables throw error. +// we can start the name of a variable with letters a-z, A-Z, _ ,and $ . +// This is how we correctly name our variables: +const twelveHourClockTime = "20:53"; +const twenty4HourClockTime = "08:53"; +console.log(`Twelve-hour clock: ${twelveHourClockTime}`); +console.log(`24-hour clock: ${twenty4HourClockTime}`); From bb852dd9bc61513e470ba5088a81656e5061c71d Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 11 Feb 2026 17:59:15 +0000 Subject: [PATCH 3/6] Mandatory interprets are done --- .../1-percentage-change.js | 12 ++++---- .../3-mandatory-interpret/2-time-format.js | 15 ++++++---- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 29 ++++++++++--------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..1e346b6db 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +12,13 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made - + // line: 4 and line 5 and it is replaceAll(). // 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? - + // error is coming from line 5. The error is because of not putting comma between the inputs of replaceAll function. + // To fix the problem just add a comma between the inputs of replaceAll function. // c) Identify all the lines that are variable reassignment statements - + // Line 4 and 5. // d) Identify all the lines that are variable declarations - + // we have variable declarations at lines 1, 2, 7 and 8. // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + // the purpose of that expression is to turn into number the string and replace all commas with an empty space. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..0cb4f84de 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 8724; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,17 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? - + // we have 6 variable declarations. // b) How many function calls are there? - + // here we have only one which is console.log(). // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators - + // Remainder operator (%), returns the leftover when one operand divided by the other operand. + // movieLength % 60 expression represents the remaining seconds. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - + // that expression turns the seconds into minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? - + // the result variable represents the movie length in Hour, minutes and seconds. + // a better name can be "movieLength". // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + // as I changed the value of the movieLength the results has changed too.It means this code works with any value. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..ff190904e 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,21 +1,16 @@ const penceString = "399p"; -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); +const penceStringWithoutTrailingP = +penceString.substring(0, penceString.length - 1); // => "399"; -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // => "399"; +const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); // => "3" const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); + .substring(paddedPenceNumberString.length - 2) // => "99" + .padEnd(2, "0"); -console.log(`£${pounds}.${pence}`); +console.log(`£${pounds}.${pence}`); // => "3.99" // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds @@ -24,4 +19,12 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +// 1. const penceString = "399p": initializes a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP uses the substring()method to remove the last character. +// 3. const paddedPenceNumberString tries to add "0" at the beginning, but the length is already 3; so it does nothing. +// Its worthy to talk about padStart() method. we use this to add a character at the beginning of a string. +// 4. const pound uses the substring() method on paddedPenceNumberString variable and remove its last two characters.Its value ll be "3". +// 5. const pence uses substring() on paddedPenceNumberString variable and take its length and then subtract it with 2 ... +// which will remain like this .substring(1), so it take the index 1 till the last which is only one more index that its character is 9 ... +// so the final value ll be "99". then uses padEnd( 2, "0") that can not add "0" at the end, as we set the length "2". +// 6. Lastly, we print the value of our pound and pence variable. which is "3.99". \ No newline at end of file From 242fca7e925a25cf25e4ba19b4df7e8898e7fd4a Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 11 Feb 2026 19:53:59 +0000 Subject: [PATCH 4/6] Answere the questions in objects.md --- Sprint-1/4-stretch-explore/objects.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..d5d99192d 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -13,4 +13,11 @@ Try also entering `typeof console` Answer the following questions: What does `console` store? + +`Answer` => console stores methods(function) that you can call to perform actions like logging, warning, or displaying error. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +`console.log()` prints the out put. +`console.assert()` checks the condition. +`The dot operator` is called member access. It accesses a property or method of an object. \ No newline at end of file From 38532cfe124c970e5d1809599883bce23c3e0db8 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 11 Feb 2026 20:03:42 +0000 Subject: [PATCH 5/6] Answered the question about 'alert & prompt' --- Sprint-1/4-stretch-explore/chrome.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..fb7ab6868 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -12,7 +12,12 @@ invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +`answer` // there ll be a pop-up box showing a message and ask us to press ok in order to get rid of it. + Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. What effect does calling the `prompt` function have? +`answer` // Here we also have a pop-up box with an input, and it asks the user to type something. + What is the return value of `prompt`? +`answer` // if the user type something it returns `string` if the user cancel it then it return `null`. From c3dd177cac105ece97b421da733b11605da2ffd2 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 19 Feb 2026 18:20:47 +0000 Subject: [PATCH 6/6] Fix PR feedback: lastSlashIndex, Math.floor(), const vs let, replaceAll, random min, and safe movie length calculations with edge case handling --- Sprint-1/1-key-exercises/3-paths.js | 20 ++++--- Sprint-1/1-key-exercises/4-random.js | 10 +++- Sprint-1/2-mandatory-errors/0.js | 4 +- Sprint-1/2-mandatory-errors/1.js | 10 ++-- .../1-percentage-change.js | 16 ++++-- .../3-mandatory-interpret/2-time-format.js | 56 ++++++++++++++----- Sprint-1/4-stretch-explore/objects.md | 8 ++- 7 files changed, 87 insertions(+), 37 deletions(-) diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index 076f3322f..97a4c2035 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -11,18 +11,24 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(-8); -console.log(`The base part of ${filePath} is ${base}`); +const base = filePath.substring(lastSlashIndex + 1); +console.log(`Base part: ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = filePath.slice(0, -8); -const ext = filePath.slice(filePath.length-3); +const dir = filePath.substring(0, lastSlashIndex + 1); -console.log(`the dir part of filePath variable is ${dir}`); -console.log(ext); +const dotIndex = filePath.lastIndexOf("."); +const ext = filePath.substring(dotIndex + 1); + +console.log(`Dir part: ${dir}`); +console.log(`Ext part: ${ext}`); // https://www.google.com/search?q=slice+mdn -// After studying the slice() method I was able to do this task easily. \ No newline at end of file +// After studying the slice() method I was able to do this task easily. +// 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 ... +// to solve that we better use substring(lastSlashIndex + 1)method. this method works with any name we add. +// 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. +// thanks to "hkavalikas" because of his feedback I learned this useful method. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index f70a1aca6..5ae64fc00 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -12,4 +12,12 @@ console.log(num); // num is a variable that will be assigned an integer number after the operators are done. // Math.floor() rounds a decimal to its nearest number to make it an integer. ex = 1.2 => 1. // Math.random() is a method used to create numbers between (0-1) and usually it creates a decimal like (0,2) or etc. -// each time I run it, I got different number as Math.random() generates different number each time we run it. \ No newline at end of file +// each time I run it, I got different number as Math.random() generates different number each time we run it. + +//to respond to the question "What would happen if we did Math.floor(1.9);?" +// ===> 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. + +// In addition our formula works we wont have 191, if we had Math.ceil() then the result would exceed our maximum number. + +// Response to question "What does the + minimum do here?" +// ===> + 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. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index 680eec82d..d7040b384 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,6 +1,6 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? +/*This is just an instruction for the first activity - but it is just for human consumption +We don't want the computer to run these 2 lines - how can we solve this problem?*/ //solution: Simply comment the lines. // 1- single line comment like: // example ... diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 9ca368df0..97c5c11c1 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,10 +1,10 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; -//age = age + 1; +let age = 33; +age = age + 1; // ===> or age++ +console.log(age); // ===> 34 // we can't do that with const variable, because JS locks the reference. // the best choice is to use "let" variable. -let age2 = 33; -age2 = age2 + 1; // or age++ if wanna increase by "1"; -console.log(age2); + + diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index 1e346b6db..b6e860c4e 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,13 +12,17 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made - // line: 4 and line 5 and it is replaceAll(). +// ====> Function calls occur on lines 4 and 5 (Number() and replaceAll()), and on line 10 (console.log()). + // 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? - // error is coming from line 5. The error is because of not putting comma between the inputs of replaceAll function. - // To fix the problem just add a comma between the inputs of replaceAll function. +// ====> error is coming from line 5. The error is because of not putting comma between the parameter of replaceAll function. +// To fix the problem, add a comma between the two arguments of the replaceAll function, since this method requires two parameters. + // c) Identify all the lines that are variable reassignment statements - // Line 4 and 5. +// ===> Line 4 (carPrice) and line 5 (priceAfterOneYear). + // d) Identify all the lines that are variable declarations - // we have variable declarations at lines 1, 2, 7 and 8. +// ====> we have variable declarations at lines 1, 2 with let and with const at line 7 and 8. + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? - // the purpose of that expression is to turn into number the string and replace all commas with an empty space. \ No newline at end of file +// ===> 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. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 0cb4f84de..e3dcd0775 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,28 +1,58 @@ -const movieLength = 8724; // length of movie in seconds -const remainingSeconds = movieLength % 60; -const totalMinutes = (movieLength - remainingSeconds) / 60; +const movieLength = 8777; // length of movie in seconds +if (typeof movieLength !== "number") { + throw new Error("Movie length must be a number"); // Checks if it’s a number ==> prevents strings, null, undefined; +} else if (movieLength < 0) { + throw new Error("Movie length cannot be negative"); // Checks if it’s negative; +} else if(!Number.isSafeInteger(movieLength)) { + throw new Error("Movie length is too large"); // Checks if it’s too large ==> prevents unsafe numbers in JS; +} + +let safeMovieLength = Math.floor(Number(movieLength)); // Creates clean and positive integer; + +const remainingSeconds = safeMovieLength % 60; +const totalMinutes = (safeMovieLength - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; -console.log(result); +const movieDuration = `${String(totalHours).padStart(2, "0")}:${String(remainingMinutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; +console.log(`The movie length is --> ${movieDuration}`); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? - // we have 6 variable declarations. + // we have 6 (const) variable declarations. + // b) How many function calls are there? - // here we have only one which is console.log(). + // ===> we have only one, which is console.log() on line 10. // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators - // Remainder operator (%), returns the leftover when one operand divided by the other operand. - // movieLength % 60 expression represents the remaining seconds. + + // The % operator gives the remainder after dividing two numbers. + // 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. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - // that expression turns the seconds into minutes. + // ===> 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; + // e) What do you think the variable result represents? Can you think of a better name for this variable? - // the result variable represents the movie length in Hour, minutes and seconds. - // a better name can be "movieLength". + // ===> The result variable is a string and represents the movie length format in Hour, minutes and seconds. + // ===> A better name can be " movieDuration or movieLengthFormat". and I replaced the result with movieDuration. + + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer - // as I changed the value of the movieLength the results has changed too.It means this code works with any value. \ No newline at end of file + // ===> As I changed the value of the movieLength the results has changed too.It means this code works with any value. + + // ===> Response to PR review question: Can you see any problem if we were to name it movieLength? + // 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. + +// Respond to question: "There are a few cases that might break this. Can you spot and mention a few?" + +// ===> A few cases might break this code: +// If movieLength is negative, we could get negative hours, minutes, or seconds. +// If movieLength is not an integer, the minutes or hours could have decimals. +// Very large values might not display nicely without padding. +// Single-digit minutes or seconds are not padded (like 5 instead of 05). +// If movieLength is not a number, the calculations will fail.” */ + +// to solve the edge cases, we need to write some more lines of code. Which I did add in above codes with comments. \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index d5d99192d..a8d7ed66c 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -4,17 +4,19 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter -What output do you get? +What output do you get? +==> this is the output I got: ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? +==> I got this: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} -Try also entering `typeof console` +Try also entering `typeof console` ===> I got : 'object'. Answer the following questions: What does `console` store? -`Answer` => console stores methods(function) that you can call to perform actions like logging, warning, or displaying error. +`Answer` => console stores methods(function) that you can call to perform actions like logging, warning, or displaying error. What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?