diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..1e1e4e1df 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -4,3 +4,5 @@ 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 reassignment of variable "count". The new assignment adds 1 to current value of count, "=" is to assign value on right-hand side to variable on left. diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..534f8670f 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -1,10 +1,12 @@ -const firstName = "Creola"; -const middleName = "Katherine"; -const 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. - -const initials = ``; - -// https://www.google.com/search?q=get+first+character+of+string+mdn +const firstName = "Creola"; +const middleName = "Katherine"; +const 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. + +const initials = + firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); +console.log(initials); + +// https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..66886d057 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,10 @@ 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, lastSlashIndex); +const ext = filePath.slice(filePath.lastIndexOf(".")); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +console.log(dir); +console.log(ext); + +// https://www.google.com/search?q=slice+mdn diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..5b64108de 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,15 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // 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 represents a variable. The expression assign a value to this variable. +// Math.floor () and Math.random () are methods. +// The value inside Math.floor() is its argument. The return value of this argument is expression within the yellow parenthesis. +// The whole expression of variable "num" is that a float generated by method Math.random() is multiplied by return value of the range specified by value between declared variable by key word constant. +// The method Math.floor() then return greatest integer of the value of this product. +// And finally plus the value of minimum variable declared. + +//`num` is a random integer between maximum and minimum inclusive. +// The smallest value of 'num' can be 1 + +console.log(num); diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..0c7c8f44d 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,3 @@ -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 +//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? +//By adding two slashes at the begining of each line. diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..ad63eeaac 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,9 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; + +console.log(age); + +//This is a type error. For the expected result of 34, age should not be declared as a constant. +//let should be used instead of const because const is a constant variable and cannot be reassigned. diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..99e114511 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,10 @@ // 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}`); + +//The error is that the variable should be declared as a constant before it is used in the console.log statement. + +//This is a ReferenceError. +//In this code, the key word does not need to be constant, it can be let - if this is the case the variable city0fBirth can be reassigned in other lines - instead of constant (which cannot reassign variable). diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..72a78e1a5 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,9 +1,13 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); - // 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 // Then run the code and see what error it gives. // 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 +// Expectation about the error : cardNumber is assigned a number value, a .slice function does not work on number +// The constant last4Digits should be assigned to a String (cardNumber) to perform .slice function. +// The code would result a TypeError. It matches my prediction. +const cardNumber = 4533787178994213; +const last4Digits = String(cardNumber).slice(-4); + +console.log(last4Digits); diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..3a806c9cb 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,7 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +// It was a SyntaxError. An identifier cannot start with a numberical value + +const TweleveHourClockTime = "8:53pm"; +const TwentyFourhourClockTime = "20:53"; + +console.log(TweleveHourClockTime); +console.log(TwentyFourhourClockTime); diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..e0ad07387 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/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,23 @@ 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 five function calls in this code in lines 4, 5, and 10 +// These function calls are Number, replaceAll, and 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? +// The error originally came from the replaceAll call for priceAfterOneYear: the two arguments +// (",", "") were missing a comma between them, causing a SyntaxError. +// The fix was adding the missing comma so replaceAll(",", "") has two properly separated arguments. // c) Identify all the lines that are variable reassignment statements +// Variable reassignment statements on lines 4 and 5. +// Variables carPrice and priceAfterOnYear are originally declared on lines 1 and 2, and reassigned on lines 4 and 5. // d) Identify all the lines that are variable declarations +// They are lines 1,2,7 and 8. +// On lines 1 and 2, variables carPrice and priceAfterOneYear are declared by let. +// On lines 7 and 8, variables priceDifference and percentageChange are declared by const. // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// replaceAll(",", "") removes all comma characters from the string, since commas aren't valid in a numeric value. +// Number() then converts the resulting clean string into an actual number, so it can be used in arithmetic. diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..fa6bdcff0 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = -90.5; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,25 @@ 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 six variable declarations in the program, namely : +// movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result // b) How many function calls are there? +// One function call console.log() in code above. +// The others are declared variables. // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// % is remainder operator. This operator returns the remainder after left operand is divided by 60. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The expression means first exclude odd seconds, then convert the movie length in number of complete minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result represents length of movie in H:M:S format. A better variable name can be movieLength_HMS // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// A value of movieLength 3661 will return a result of 1:1:1 where the place value for second does not conforms with leading zero time format. +//The code does not work for all values: +//It does not pad single digits with a leading zero; +//It does not validate to reject negative (e.g.-90 gives "0:-1:-30" ; -90.5 gives "0:-1:-30.5") or decimal input. diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..4bfea3fe3 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -1,21 +1,25 @@ -const penceString = "399p"; +const penceString = "399p"; // initialises a string variable with the value "399p" const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 -); + penceString.length - 1, +); // use .substring method to extract characters of numerical string, with zero indexing starting from first place of penceString, ending at one digit less than the length of penceString by method .length. Expecting value "399". const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//declares paddedPenceNumberString variable. To target the length of pence number string in three characters. If not, "0" will be added at the beginning of the string. Expecting value "399". const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 -); + paddedPenceNumberString.length - 2, +); //declares variable for pound. .substring method starting from first character, ending by trimming last two characters by method .length. Expecting value "3" const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); + .padEnd(2, "0"); // declares variable for pence. The argument is paddedPenceNumberString.length - 2, +// which for "399" (length 3) evaluates to 1. So this returns .substring(1), giving the last two +// characters "99" — not a literal -2 argument. +// .padEnd method returns character length of two, if not, "0" will be added at the end of string, for examples "90". Here, expecting "99". -console.log(`£${pounds}.${pence}`); +console.log(`£${pounds}.${pence}`); // Prints the return value by Template Literal with '£X.yz" format in console pane. // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds @@ -25,3 +29,9 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// Five Variables declared: +// penceString, penceStringWithoutTrailingP, paddedPenceNumberString,pound, pence +// Three methods used: +//.substring(), .padStart(), .padEnd() .log() +// One property used: .length diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..294c9159e 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -8,8 +8,11 @@ Let's try an example. In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; What effect does calling the `alert` function have? +(The function prompts an alert when pressing enter to run) 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? +(The'prompt'function opens a caveat dialog box, allows user to key in response. ) What is the return value of `prompt`? +(prompt(myName) will return value entered by user) diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..27990e5b0 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -6,11 +6,17 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +(ƒ log() { [native code] }) + Now enter just `console` in the Console, what output do you get back? +(console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}) Try also entering `typeof console` - +('object') Answer the following questions: What does `console` store? +(`console` stores an object. Note for question: where is this object source from?) What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +(`log` is a method to print object belongs to console. The `.` access that object stored in `console`) +(`assert`checks if a condition is true.)