diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..d92cf952a 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,4 @@ 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 +// This line (line 3), is updating the value of the count variable. The = operator is an assignment operator which assigns the value on the right (count + 1) to the variable on the left (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..de9d2d153 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -4,8 +4,7 @@ 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[0] + middleName[0] + lastName[0]; // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..98ab371c3 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/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 = base.slice(base.lastIndexOf(".")); // https://www.google.com/search?q=slice+mdn \ 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..59d1d93fd 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,16 @@ 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 + +// Math.random returns a random number between 0 (inclusive) and 1 (exclusive). +// const minimum = 1; const maximum = 100; +1 so, 100 - 1 + 1 = 100 +// Math.random() * 100 means the decimal number will be multiplied by 100, so the range of possible values is from 0 to 99.9999... +// Math.floor rounds the number down to the nearest whole number, so the possible values are from 0 to 99. +// Eg. 12.9 becomes 12, 0.3 becomes 0, 99.9 becomes 99. +// Brackets(), Multiply*, Function(Math.floor), Addition+ +// 1) Pick a random number between 0 and 1, eg. 0.5 +// 2) Multiply that number by 100, eg. 0.5 * 100 = 50 +// 3) Round that number down to the nearest whole number, eg. Math.floor(50) = 50 +// 4) Add 1 to that number, eg. 50 + 1 = 51 + +// num represents a random whole number between 1 and 100. \ 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 cf6c5039f..9530ff0d7 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/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? +// The forward slash is used to indicate a comment in JavaScript, so the computer will ignore these lines when running the program. diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..885c0bf74 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,8 @@ // 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); + +// The code above will output 34, because we are reassigning the value of age to be the current value of age plus 1. So, 33 + 1 = 34. + diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..89a8735be 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/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}`); + +// The error happens because cityOfBirth is used before it is defined. JavaScript runs code from top to bottom, so the variable must be declared first. \ 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 ec101884d..eed3820d8 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/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); //"4213" // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +8,5 @@ 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 + +//The code didn’t work because slice() is a string method, not a number method, so the card number must be converted to a string first. \ 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 21dad8c5d..15c642d97 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,11 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; + +// Variables cannot start with a number. +// When JavaScript tries to run the code, it throws a SyntaxError. +// To fix the error we can rename the variable to start with a letter, a underescore_ or a dollar sign $ instead of a number. + +const twelveHourClockTime = "8:53"; +const twentyFourHourClockTime = "08:53"; + +// I changed the time to match the 12 and 24 hr clock format. \ 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 e24ecb8e1..5d77c3811 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; @@ -13,10 +13,39 @@ console.log(`The percentage change is ${percentageChange}`); // 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 in this file. They are on lines 1, 2, 4, and 5. The functions being called are replaceAll() and Number(). +// Line 4: replaceAll and Number (2) +// Line 5: replaceAll and Number (2) +// Line 10: console.log (1) + + + // 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 is occuring on line 5. It is occuring because there is a missing comma between the replaceAll "". +// We can fix this by adding a comma between the two empty strings in the replaceAll function on line 5, like this: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + + + // c) Identify all the lines that are variable reassignment statements +// carPrice = Number(carPrice.replaceAll(",", "")); +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + + + // d) Identify all the lines that are variable declarations +// Lines 1,2,7 and 8. +// let carPrice = "10,000"; +// let priceAfterOneYear = "8,543"; +// const priceDifference = carPrice - priceAfterOneYear; +// const percentageChange = (priceDifference / carPrice) * 100; + + + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// carPrice.replaceAll(",", "") - Replaces all commas "," in the string with nothing "". e.g "10,000" becomes "10000". +// Number() - Converts the resulting string "10000" into the number 10000. +// The purpose of this is to take a string with commas (like "10,000") and turn it into a number (10000) that we can do math with. \ 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..c2a29bf4b 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -13,13 +13,41 @@ console.log(result); // a) How many variable declarations are there in this program? +// There are 6 variable declarations in this program. They are on lines 1, 3, 5, 7, 9 and 11. The variables being declared are movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours and result. + + + // b) How many function calls are there? +// There is 1 function calls in this program. It is on lines 10. The functions is the 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 +// % is the remainder operator. +// movieLength % 60 - This gives the remainder when movieLength is divided by 60. +// Since there are 60 seconds in a minute, this tells us how many leftover seconds there are after counting full minutes. +// For example, 8784 % 60 = 24. So there are 24 seconds left after counting the minutes. + + + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// movieLength - remainingSecond - This gives the total number of seconds in the movie after removing the leftover seconds. +// Divide by 60 - this converst the seconds into total full minutes. eg. (8784 - 24) = 8760, 8760 / 60 = 146. + + + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result is a string that represents the movie length in hours:minutes:seconds +// For example: 2:26:24 (2 hours, 26 minutes, 24 seconds) +// Better name can be movieDurationString. + + + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// This code will work for all positive integer values of movieLength. It will correctly calculate the hours, minutes and seconds for any length of movie. However, if movieLength is negative or not an integer, the code may not work as intended. For example, if movieLength is -100, the calculations will not make sense in the context of a movie length. If movieLength is a decimal, it may also cause issues with the calculations. \ 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..2a78c1599 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,8 @@ 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): creates a new string variable that contains the original string without the last character (the "p"). +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): creates a new string variable that pads the previous string with leading zeros until it is at least 3 characters long. eg. "45" becomes "045" and "7" becomes "007". +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): creates a new string variable that contains the first part of the padded string, which represents the pounds. It takes all characters except the last two. eg. "045" becomes "0" and "399" becomes "3". +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): creates a new string variable that contains the last two characters of the padded string, which represents the pence. It also pads it with trailing zeros if necessary to ensure it is 2 characters long. eg. "045" becomes "45" and "7" becomes "70". +// 6. console.log(`£${pounds}.${pence}`): outputs the final price in pounds and pence format to the console. For example, if penceString is "399p", it will output "£3.99". If penceString is "45p", it will output "£0.45". If penceString is "7p", it will output "£0.07". diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..c63b81001 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -10,9 +10,19 @@ Let's try an example. In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; + What effect does calling the `alert` function have? +// It pauses the webpage until you click OK. + 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`. +// const myName = prompt("What is your name?"); +// Shows a pop-up asking: "What is your name?" and lets the user type a string. + What effect does calling the `prompt` function have? +// A dialog box pops up with the message you provide (in this case, "What is your name?"). +// The webpage is paused while this dialog is open — you can’t interact with the page until you respond. + What is the return value of `prompt`? +// prompt returns the value that the user types if they click OK. eg. if the user types "Shay", the return value is "Shay" (a string) and if the user clicks cancel without typing anything, the return value is null. \ 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 0216dee56..0ee32a606 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,23 @@ 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] } + 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 is a built-in object that stores methods (functions) to interact with the developer console. eg. console.log() - prints messages, console.warn() - prints warnings etc. + + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console.log a tool to write messages and console.assert checks if something is true. +The . is called the dot operator, without the dot, you can’t reach the tool inside the object. eg, the log, the assert. \ No newline at end of file