diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..3ec2d1a30 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -2,5 +2,7 @@ let count = 0; count = count + 1; +//In line 3, the assignment operator, '=' assigns the value 'count + 1' to the variable: 'count'. + // 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 diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..4a09edec5 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,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..3021a0f3b 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,44 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +console.log(num); +/* + * Explanation of 'num': + * The 'num' variable represents a random whole integer generated within the range defined by 'minimum' and 'maximum', inclusive. + * + * A step-by-step walkthrough of the expression: + * + * 1. (maximum - minimum + 1): + * - Calculates the size of the desired range. For minimum=1, maximum=100, this is (100 - 1 + 1) = 100. + * - The '+ 1' is crucial to make the range inclusive of both the minimum and maximum values. + * + * 2. Math.random(): + * - Returns a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). + * - E.g., 0.000... to 0.999... + * + * 3. Math.random() * (maximum - minimum + 1): + * - Scales the random number to fit the desired range. + * - E.g., if Math.random() is 0.75, then 0.75 * 100 = 75. + * - This results in a floating-point number between 0 (inclusive) and the range size (exclusive). + * + * 4. Math.floor(...): + * - Rounds the result down to the nearest whole integer. + * - E.g., Math.floor(75.99) = 75. + * - This ensures we get an integer, effectively mapping the 0 to (range-1) values. + * + * 5. ... + minimum: + * - Shifts the integer to the desired starting point. + * - E.g., if the result so far is 75, adding 'minimum' (1) gives 75 + 1 = 76. + * - This transforms the 0 to (range-1) integer into a 'minimum' to 'maximum' integer. + * + * In summary, 'num' will be a random integer chosen uniformly from the set {1, 2, 3, ..., 99, 100}. + */ + + + // 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 + + diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..a0617d569 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/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-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..031839b47 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/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-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..cc3dc8248 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 variable must be defined it's used in the console.log statement \ 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..b56868616 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,9 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); + +//cardNumber need to be converted into a string first, because .slice() only works on strings, not numbers. + + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..eac6e94ce 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,4 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "08:53"; +const twentyFourHourClockTime = "20:53"; + +// Variable names must start with a letter, underscore symbol or , but cannot start with a number. \ 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..07dc15608 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,41 @@ 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 +//Answer: There are 5 function calls: +// - carPrice.replaceAll(...) +// - Number(...) : Line 3 +// - priceAfterOneYear.replaceAll(...) +// - Number(...) : Line 5 +// - 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? +//Answer: +//There was an error in line 5 because a comma was missing. +// Instead of the original example in line, which was: replaceAll(", " "")). I inserted a comma in order to fix the code: replaceAll(", ", "")); + // c) Identify all the lines that are variable reassignment statements +//Answer: +// Lines 4 and 5 are variable reassignments: +//In Line 4 the value changes from "10,000" (a string) to 10000 (a number): carPrice = Number(carPrice.replaceAll(",", "")); +//In line 5 the value changes from "8,543" (a string) to 8543 (a number): priceAfterOneYear = Number(priceAfterOneYear.replaceAll(", ", "")); + // d) Identify all the lines that are variable declarations +//Answer: +// These variable declarations are identified by these keywords: let and const: + +// Line 1: let carPrice = "10,000"; + +// Line 2: let priceAfterOneYear = "8,543"; + +// Line 7: const priceDifference + +// Line 8: const percentageChange + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +//Answer: +//carPrice.replaceAll(",", ""): removed commas from the string "10,000", which became "10000" +//Number(...): converted the string "10000" into a 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 47d239558..9aa32cb21 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,28 @@ 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: movieLength, remainingSecond, totalMinutes, remainingMinutes, totalHours, result // b) How many function calls are there? +// There is one function call: console.log(result) // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// This expression calculates the remainder after dividing by 60 +// This gives the number of seconds left over after removing the full minutes. +// 8784 % 60 = 24, meaning 8784 seconds is 146 full minutes and 24 seconds. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The expression in line 4 means totalMinutes = (8784 - 24) / 60 // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result represents the movie length in hours : minutes: seconds +//A better name for this variable would be: formattedDuration // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// I tested the code with values like 1, 45, 75, 1800, and 10000. The results were accurate for all of them. +// For example, 75 returned 0:1:15, and 1800 returned 0:30:0, which are correct. +// I also tried using -5000, and the output was -1:-23:-20, which isn't consistent with a time format. +// This shows the code does not handle negative numbers correctly. +// In general, it works well with positive whole numbers \ 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..0113de3c5 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,26 @@ 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); +// The purpose of line 3 is to remove the trailing 'p' from the string to leave just the numeric part. +// As a result, "399p" (as seen in line 1) becomes "399". + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//The purpose of line 8 is to ensure that the string is at least 3 characters long. +// It adds zeros at the start if the string is shorter than 3 digits. +// In this case, "399" is already 3 digits long, so nothing is added. + +//4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); +// This takes the last two digits to get the pounds part. +//In this scenario, "399", we get "3" as pounds. + +//5. const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); +// This gets the last two digits from the string to show the pence. +// If the result is only one digit, it adds a zero at the end to make it two digits. +// In this case, "399" gives us "99", so nothing additional is added. + +//6. console.log(`£${pounds}.${pence}`); +// This line prints out the final price in pounds and pence. +// It combines the pounds and pence with a £ symbol and a dot in between. +// In this example, pounds = "3" and pence = "99"; it will print "£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 e7dd5feaf..9242a1a17 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -12,7 +12,24 @@ invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +Answer: +When I run alert("Hello world!") in the Chrome console, a pop-up window appears on the screen. +This pop-up shows the message Hello world! and waits for you to click OK before you can continue using the page. + 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`. +Answer: +const myName = prompt("What is your name?"); + What effect does calling the `prompt` function have? + +Answer: +This opens a pop-up dialog box with the question What is your name? and a text input field. +This enables the user to type the required response (your name) +The value the user enters is saved in the variable myName. + What is the return value of `prompt`? + +Answer: +Clicking Cancel causes prompt to return null. +The return value of prompt is either the string the user types in or null if cancelled. diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..bccc4f293 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,26 @@ 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? +Answer: +The output is: ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? +Answer: +The output is: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} Try also entering `typeof console` +Answer: +The output is: 'object' Answer the following questions: What does `console` store? + +Answer: +The console doesn't permanently store any values. It shows messages, errors, and the results of code you run. displays output and lets you test and store temporary values while the page is open. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +Answer: +The console is a built-in object in JavaScript. It has different tools (called functions) like log and assert that help you debug and show messages while you're testing your code. +The . is used to access a property or method of an object.