diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..ecf257ef6e 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -3,4 +3,6 @@ let count = 0; 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 +// The = sign means 'assignment' operator +// In line 3, at first the calculation will be done at R.H.S, +// with the initial count value then the calculated value will be stored at the L.H.S 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 47561f6175..73327f9864 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -2,10 +2,5 @@ let firstName = "Creola"; let middleName = "Katherine"; 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 = ``; - -// https://www.google.com/search?q=get+first+character+of+string+mdn - +let initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`; +console.log(initials); \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28e..4d6f3cb3bf 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -13,11 +13,6 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); 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 = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +const dir = filePath.slice(0, lastSlashIndex); +const dotIndex = base.lastIndexOf("."); +const ext = base.slice(dotIndex); \ 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 292f83aabb..bc736f9cd1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,20 @@ 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? +// num stores the result of the expression after calculating it. // 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 +// There is a range of numbers from 1 to 100. In the expression, +// (maximum - minimum + 1) calculates the size of the possible integers, which is 100. +// The Math.random() function generates a random decimal number in the range 0 ≤ x < 1. +// This random decimal is then multiplied by the range size to scale it to a +// value between 0 and 100. The result is a floating-point number in this interval. +// Next, Math.floor() is used to round the number down to the largest integer less than +// or equal to the calculated value, producing an integer between 0 and 99. +// Finally, by adding minimum, the range is shifted from 0–99 to 1–100, giving a random +// integer within the required range. +// I have run 5 times the program and this produces different values each time because +// Math.random() generates a new random decimal number every time. +// Since the final value of num depends on this function, the result changes on each execution. \ 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 cf6c5039f7..a794883c25 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,5 @@ 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? + +// We can solve this problem by writing those two lines as comments using //, +// because JavaScript ignores commented lines and they will not be executed by the computer. \ 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 7a43cbea76..bb8aba27fd 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,6 @@ -// trying to create an age variable and then reassign the value by 1 const age = 33; age = age + 1; + +// There is a constant variable age, which is fixed and it's value cannot be reassigned. +// If we want to change the value of age, we have to use let instead of const. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..84fa1292d2 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,6 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +// The variable const cityOfBirth = "Bolton"; needs to be declared at first. +// As it is not declared, JavaScript cannot find out the variable cityofBirth in console.log. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884db..c3e23ecd0c 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,19 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +console.log(cardNumber.toString()); +const cardNumber1 = cardNumber.toString() +const last4Digits = cardNumber1.slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Before running the code, make and explain a prediction about why the code won't work +// I think slice () works for strings or arrays not for numbers. That's why the code won't work. // Then run the code and see what error it gives. +// After running code TypeError: cardNumber.slice is not a function this error shows. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? -// Then try updating the expression last4Digits is assigned to, in order to get the correct value +// It gives this error because cardNumber doesn't have slice method. My prediction was beacuse +// cardNumber is number not string and the difference is that slice method called but it +// doesn't exist on that type. +// Then try updating the expression last4Digits is assigned to, in order to get the correct +// value +// Updated the expression in order to get in to the correct value. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..da9533d344 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,2 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const TwelveHourClockTime = "8:53pm"; +const TwentyfourhourClockTime = "20:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..61feeb2726 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,27 @@ 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 +// There are 5 function calls. carPrice = Number(carPrice.replaceAll(",", "")); +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// In the above 2 lines, 2 replaceAll() functions for carPrice and priceAfterOneyear and after executing them again Number() +// functions for the 2 variables. +// At last, console.log() function for printing the executed value. // 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? +// after running the code, the error is coming from this line priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));and +// the error is SyntaxError: missing ) after argument list. There is a comma missing in replaceAll() syntax. It should be +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // c) Identify all the lines that are variable reassignment statements +// There are 2 variable reassignment statements. +// const priceDifference = carPrice - priceAfterOneYear; +// const percentageChange = (priceDifference / carPrice) * 100; // d) Identify all the lines that are variable declarations +// There are 4 variable declarations lines. +// let carPrice = "10,000"; +// let priceAfterOneYear = "8,543"; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Firstly this expression replace all the commas from the variable carPrice = "10,000" to "10000" and then it converts +// string "10,000" to number 10000. \ 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 47d2395587..2cb47d5c85 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,23 @@ 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? +// There are 6 variable declarations. // b) How many function calls are there? +// There is one function call. // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// The expression movieLength % 60 devides the movieLength by 60 seconds and return the reminder +// which will be stroed in the const remainingSeconds. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// Firstly, Removes the leftover seconds from movieLength and then converts the remaining seconds +// into whole minutes which will be stored into totalMinutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result reprents the movieLength in hours:minutes:seconds. The variable should be renamed by totalDuration. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// I have tried with 3 variables, 1515, 9999 & 70052. I observed that this code works succesfully with all the values +// and gave me results for all the different movieLength in hours:minutes:seconds. \ 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 60c9ace69a..0a87d70a67 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,18 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); : a variable declared and a function called +// in the variable with substring method. In the substring method (penceString.length - 1) = 4 -1 = 3 then the function remains +// penceString.substring(0, 3); from which we find const penceStringWithoutTrailingP = 399p.penceString.substring(0, 3); to +// const penceStringWithoutTrailingP = 399 +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); : another variable const paddedPenceNumberString +// declared and a function called in the variable with padStart method. penceStringWithoutTrailingP.padStart(3, "0"); will be 399.padStart(3, "0"); +// and finally const paddedPenceNumberString = "399". Secondly, another variable const pounds declared in which calculates the value in +// pounds. For this, (0, paddedPenceNumberString.length - 2); will be (0, 1) and the const pounds = 399.substring(0, 1); calculates +// const pounds = 3 +// 4. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); : a variable pence declared +// to calculate the value in pence. so, if we put the values it will be const pence = 399.substring(1).padEnd(2, "0"); which at first works for +// substring and then for padEnd and will be const pence = 99; +// 5. console.log(`£${pounds}.${pence}`); : this console function printed the prince in pounds and pence.And finally the printed output +// will be £3.99 \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feafe..2e703aa2d6 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,11 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +// `alert` function deisplays a popup dialogue box with a message and the user have to click 'ok' button. 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? +// `prompt` function displays a dialogue box with a message and asks the the user to give input. What is the return value of `prompt`? +//It returns the value which user inputs. In the question it is myName. \ 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 0216dee56a..a60a70094b 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,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? +//ƒ log() { [native code] } . This is the output. The output indicates it is a function whose name is log() and which is built in +//Javascript. Now enter just `console` in the Console, what output do you get back? +//console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} . this is the output. In which console shows many functions it delt with such as debugging, error, info, log warn etc. Try also entering `typeof console` +//The output is 'object'. Which means it is an object which perform many functions. Answer the following questions: What does `console` store? +`console` stores different methods (functions). What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +//Both are the function reference. If we further call `console.log` then the function runs and it will print output. +console.assert() function writes an error message to the console if the assertion is false. If the assertion is true, nothing happens. `.` used to access the property of the object. In both the functions, `.` accesses `log` or `assert` property of the console object and get the values. \ No newline at end of file