diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..2620f4098 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 + +// On line 3 is the = sign is an assignment operator and it is assigning thr count + 1 expression into the count variable. \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..5db4b44a3 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,6 @@ 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 = ``; +const initials = `${firstName.charAt()}${middleName.charAt()}${lastName.charAt()}`; // 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..fbd7abc3f 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(-3); + +console.log(dir); +console.log(ext); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..30e931387 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,10 @@ 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 + +// 1. num is a variable that stores the final result of the expression assigned to it. +// 2. The Math.random() method generates a random decimal number from 0 up but not excluding 1. +// 3. The Math.floor() method round a decimal number down to a whole number. +// 4. (maximum - minimum + 1) calculates how many possible whole numbers there are. +// Java Script evaluates the expressions inside the inner parentheses first. +// + minimum adds 1 to move the range from 0 to the minimum 1. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..65ad3030d 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -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? \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..031839b47 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,4 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..2cbc75439 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,7 @@ // 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}`); + +// Java script cannot access the variable cityOfBirth before initialization. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..ebccef95d 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,5 +1,7 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = Number(String(cardNumber).slice(-4)); + +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +9,8 @@ const last4Digits = cardNumber.slice(-4); // 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 + +// Prediction: slice() is a string method. That is why it is not working on numbers. +// Error message: type error +// Convert the number to string +// If we need to keep number we need to convert it back from string to number using Number() method \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..015e13cb7 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,4 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const twelveHourClockTime = "8:53pm"; +const twentyFourHourClockTime = "20:53"; + +// Variable names cannot start with a number in Java Script. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..8cf1d95b0 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; @@ -20,3 +20,16 @@ console.log(`The percentage change is ${percentageChange}`); // d) Identify all the lines that are variable declarations // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// Answers + +// a) There are no function or function calls in this code. + +// b) The error was online 5 inside replaceAll method parentheses a comma is missing between the double quotes. + +// c) Line 4 carPrice. Line 5 priceAfterOneYear. + +// d) Line 1, 2, 7 and 8. + +// e) The method replaceALL is removing the comma. Then the Number method is converting the string into numbers. +// The purpose is to use number operations and calculate the percentage change. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..b13af55bb 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -23,3 +23,17 @@ console.log(result); // e) What do you think the variable result represents? Can you think of a better name for this variable? // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// Answers + +// a) There are 6 variable declarations. + +// b) There are no function calls. + +// c) The reminder (%) operator returns the reminder left over when one operand is divided by a second operand. + +// d) movieLength(8784) - remainingSeconds(24) = 8760(seconds). 8760 / 60 gives us 146 minutes. totalMInutes = 146 + +// e) result represents the length of the movie in hours, minutes, and seconds format. It can be renamed movieDuration. + +// f) When displaying single digit hour, minute, or seconds it doesn't include 0 in front of the digit. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..88e2009d9 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -1,19 +1,11 @@ const penceString = "399p"; -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); +const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); +const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); +const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); console.log(`£${pounds}.${pence}`); @@ -25,3 +17,24 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// Answers + +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): +// the substring() method returns the characters starting at index 0(3) up to and excluding the end(9) of penString variable. +// This way only 399 is extracted of the string. Then the result is assigned to penceStringWithoutTrailingP variable. + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): +// The method padStart(3, "0") adds the string 0 before if the length of penceStringWithoutTrailingP string is less than 3 characters. +// It then assign it to paddedPenceNumberString variable. For example if the string is 11, it returns 011 or the string is 1, it returns 001. + +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) +// Again the method substring() returns the characters starting at length 0 and removing the last two characters which are the pence(00). +// This gives us only the pounds value and it the assigned to the pounds variable. + +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): +// substring method first returns the last two characters of the paddedPenceNumberString string. +// Then the padEnd() method adds a 0 if the length of the string is less than 2. Then it assigns it to pence variable. + +// 6. console.log(`£${pounds}.${pence}`): +// Finally console.log prints the pounds and pence adding the the pound sign £ first using template literal as £3.99 diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..1769d727c 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -13,3 +13,11 @@ Now try invoking the function `prompt` with a string input of `"What is your nam What effect does calling the `prompt` function have? What is the return value of `prompt`? + +Answer + +1. 'alert' display a message on pop up window. + +2. 'prompt' asks for an input of information. + +3. 'prompt' return value is string. \ No newline at end of file diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..66b5b4b35 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -14,3 +14,18 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + + +Answer + +1. When 'console.log' entered it gave the output: 'Uncaught ReferenceError: consol is not defined'. + +2. When 'console' entered it gave the output: 'console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}'. + +4. 'console; stores object. + +5. 'console.log' print/display a value. + +6. 'console.assert' checks weather a condition is true or false. + +7. '.' means access the value inside the object. \ No newline at end of file