diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..133804580 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 + +// ANSWER +// In line 3, the value of 1 is added to the inital value of count which was 0, therefore the count value is being increasing by one each time the code runs and saves the new result into the count variable. The assignment operator "=" runs the everuthing on the right side and then store the result in the variable on the left hand side. \ 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..51512e506 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -2,9 +2,5 @@ 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 initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; +console.log(initials); diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..fbe113e0f 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,7 @@ 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(".") + 1); // 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..af98153cb 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/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 + +// ANSWER +// num represents a random whole number in the range 0 - 100. +// math.random gives a random decimal between 0 and 1.math floor rounds the value to a whole number. +// (maximum - minimum + 1) evaluates the total count of integers. (+ minimum )adds 1 to the outcome. +// The code starts by evaluating math.random then multiply that by the maximum, math.floor will then round that value into a whole number then finally adding minimum to get num. diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..8c5e17a24 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // 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); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..4cd3c244b 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,8 @@ // 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}`); +//Answer +// The console.log will be execued first because it is before the declared city variable. + const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); \ 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..2a43854ea 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,5 +1,6 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = 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 +8,7 @@ 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 + +// Explanation +// The string slice method does not apply to number variables, therefore the code will not run. +// The number must be changed to a string first. \ 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..a291d0245 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"; + +// A variable name can not start with a number. \ 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..54bf5541b 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,18 @@ 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, number() and replaceAll() +// line 5, number() and replaceAll() +// 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? +// The was a missing comma in line 5. // c) Identify all the lines that are variable reassignment statements +// line 4 and line 5 // d) Identify all the lines that are variable declarations +// line 1,line 2, line 7, line 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// The expression changes the string to a number and removes the comma. This makes it possible for Javascript to perform subtraction since it is no longer a string \ 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..1ade6eb77 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,15 @@ 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? - +// 6, line1,3,4,6,7,9 // b) How many function calls are there? - +// 1, Line 10: 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 - +// The modulo operator divides the movieLength in seconds by 60 and give the remainder // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +// It calculates the total number of whole minutes in the movie by subtracting the remainingSeconds from the total movie then divides the answer by 60 to covert seconds to minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? - +// This variable represents the movie duration in the form of hours:minutes:seconds. better name id duration. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// The code will run for all non negative whole number. \ 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..a761864ab 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,19 @@ 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 :Removes the letter p from the end of the string. +//); +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); : Makes sure that the string has at least 3 characters by adding zeros at the beginning if required. + +// // const pounds = paddedPenceNumberString.substring( +// 0, +// paddedPenceNumberString.length - 2 :This takes the first digit of the number in pence since 100 pence = 1 pound therefore coverting pence to pounds +// ); + +// const pence = paddedPenceNumberString +// .substring(paddedPenceNumberString.length - 2): Takes the pence which is the 2 numbers after the 1st number and ensures that it is exactly 2 digits long. +// .padEnd(2, "0"); + +// console.log(`£${pounds}.${pence}`); : Prints the final output string using the literal to combine currency symbol, pounds and the pence. \ No newline at end of file diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..063bbe91e 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -6,10 +6,14 @@ Just like the Node REPL, you can input JavaScript code into the Console tab and 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? +It created a pop up message in the broswer saying Hello world and an 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? +It displays a pop up message in the browswer asking the user "What is your name?", it also inludes an OK and Cancel button + What is the return value of `prompt`? +I entered my Sima and clicked OK, so the return value is "Sima" which is stored as myName. \ 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..6d7a97d12 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -1,16 +1,19 @@ -## Objects - In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. -Open the Chrome devtools Console, type in `console.log` and then hit enter +Open the Chrome devtools Console, type in console.log and then hit enter What output do you get? +It gives the expression : ƒ log() { [native code] } -Now enter just `console` in the Console, what output do you get back? - -Try also entering `typeof console` +Now enter just console in the Console, what output do you get back? +It gives the expression: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} with all the methods or functions that can be used for debugging. +Try also entering typeof console +It gives the string "object" Answer the following questions: -What does `console` store? -What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +What does console store? +It stores a list of methods that can used in the browser's developer tools. + +What does the syntax console.log or console.assert mean? In particular, what does the . mean? +Console.log means that you are accessing the log method of the console object. The dot (.) is called the member access operator or dot notation, it is used to access methods inside an object. it tells javascript to look inside the object on the left to get the method on the right. \ No newline at end of file