diff --git a/.gitignore b/.gitignore index bde36e530..fb65e7e7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules .DS_Store .vscode -**/.DS_Store \ No newline at end of file +**/.DS_Store +package-lock.json +.env \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..7fbbe8264 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 +// Line 3 is reassigning the count value to be the current value of count plus 1, which is also a statement. \ 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..73d5c2975 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,8 @@ 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]}`; +console.log(initials); // 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..1a34f4f1d 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,9 @@ 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(".")); +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); // 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..4a6d61408 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/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 + +// Math.floor - Rounds a number DOWN to the nearest integer +// Math.random - generates a random floating number > 0 and < 1 +// (maximum - minimum + 1) = 100 - 1 + 1 = 100 +// So, num represents a rounded down random integer generated between 1 and 100 +// Edge case: Math.floor(99.99) + 1 = 99 + 1 = 100 +console.log(num); diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..7f0607ec4 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? +// We can comment them out \ 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..05e67ddf3 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,6 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? +// The variable should be declared before it is used in console.log -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); \ 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..a3c2c0ca5 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 = String(cardNumber).slice(-4); + +// A number was not able to be sliced, whereas a string can be sliced +// So, the error is that cardNumber should be a string, not a number +console.log(last4Digits); // 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..f51d4ea5d 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 = "20:53"; +const twentyFourHourClockTime = "08:53"; + +// Change the above names of variables to avoid errors \ 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..56f5efdca 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,27 @@ 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 +// Line 4: carPrice.replaceAll(",", "") +// Line 4: Number(carPrice.replaceAll(",", "")) +// Line 5: priceAfterOneYear.replaceAll("," "") +// Line 5: Number(priceAfterOneYear.replaceAll("," "")) +// Line 8: console.log(`The percentage change is ${percentageChange}`) + // 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 coming from line 5 +// The error is due to a missing comma +// Add a comma between the two quotations + // 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 and Line 8 + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// The expression is replacing all the commas in the string carPrice with nothing, and then converting it to a number type diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..1fd6589a2 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -8,18 +8,37 @@ const totalHours = (totalMinutes - remainingMinutes) / 60; const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; console.log(result); +console.log(remainingSeconds) // 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 + // b) How many function calls are there? + // There is 1 function call + // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators + // % is a remainder operator. + // movieLength % 60 represents the remainder when 8784/60 is calculated, which is 24 seconds + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? + // The expression is calculating the total number of minutes in the movie length + // It does this by subtracting the remaining seconds from the total movie length, and then dividing it by 60 + // e) What do you think the variable result represents? Can you think of a better name for this variable? + // The result represents the movie length in hours:minutes:seconds + // A better name could be hoursMinutesSeconds or movieLengthFormatted + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + + // The code will only work for positive integers + // if the value is 0 or negative, the code will not make sense + // if the value is a decimal, the code will give float results + // if the value is not a number, the code will give an error \ 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..51db3bd85 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,27 @@ 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): +// 2.1 removes the trailing 'p' from the string by taking a substring from index 0 to the second last character +// 2.2 penceStringWithoutTrailingP now = "399" + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): +// 3.1 ensures the string has at least 3 characters by adding zeros to the start if necessary +// 3.2 However, since "399" already has 3 characters, it remains unchanged +// 3.3 paddedPenceNumberString now = "399" + +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): +// 4.1 taking the first characters of the string up to the last two characters +// 4.2 since "399" has only 3 characters, this only takes the first character +// 4.3 pounds now = "3" + +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): +// 5.1 takes the last two characters of the string to represent the pence +// 5.2 since "399" has only 3 characters, this takes the last two characters "99" +// 5.3 padEnd(2, "0") ensures that if there were less than 2 characters, it would add a "0" to the end +// 5.4 pence now = "99" + +// 6. console.log(`£${pounds}.${pence}`): +// 6.1 constructs a formatted string representing the price in pounds and pence +// 6.2 prints "£3.99" to the console \ 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..c7c8238ff 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,13 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +The alert function call a small separate window pop out, written "Hello world!". There is a OK button at the bottom right to dismiss it. 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 pop out a small window similar to alert but it has a input section where I can type my name. + What is the return value of `prompt`? +The return value of prompt is a string. If we did not enter anything and pressed ok, the value is also a string. +However, if we pressed cancel, it will return null. diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..43fa62da0 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,22 @@ 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? +I got ƒ log() { [native code] } +Chrome is telling me console.log a built-in function. Now enter just `console` in the Console, what output do you get back? +The output is the console object, which has many built-in methods, such as assert, clear, and context. Try also entering `typeof console` +The type of console is an object. Answer the following questions: What does `console` store? +A console contains its properties, and many of its properties are built-in methods that we can call. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +The syntax console.log references to the log method built-in the console object, whereas console.assert references to the assert method. +The log method can output message to the console. +The assert method can output a error message when an assertion is false. +In particular, the "." is a accessing method syntax. As a console is an object which has many built-in methods, the "." can refer to the corresponding method after the ".", such as log and assert.