diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..64c9abe21 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,9 @@ 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: line 3 shows increment operation - the variable count gets reassigned a new value which is the sum of the old value and 1. +In other words, the variable count is now adding 1 to its old value to obtain a new value. +*/ + diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..3e9e5b37c 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.charAt(0)}${middleName.charAt(0)}${lastName.charAt(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..be2403a81 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(1,lastSlashIndex); +const ext = filePath.slice(filePath.indexOf(".")); // 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..828bc5aad 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,11 @@ 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. Since Math.random() returns an integer in the interval [0, 1). This means num eventually represents any random integer in the interval [1, 100]. +2. Math.floor() rounds down the expression "Math.random() * (maximum - minimum + 1)"", leading to an integer in the interval [0,99]. + +3. So num returns an integer in the interval [0,99] + 1 which eventually means num return [1, 100] +4. Done! +*/ diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..338322031 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,17 @@ 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 +We don't want the computer to run these 2 lines - how can we solve this problem? + +/* +I got "SyntaxError: Unexpected identifier" error message. It happen because the 2 lines don't make sense. +the computor sees the first word and it assumes a variable or a function is being declared but then when +it reads the resut of the sentence it breaks as this is not how an identifier is being used and formed in +a programme. +To avoid this issue, we can just simply comment the 2 lines, just like what I am doing here! + +*/ + +/* +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? +*/ + diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..e3f3fa385 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,15 @@ const age = 33; age = age + 1; + +/* +I got this error message "TypeError: Assignment to constant variable." after running +my code. It happened because when assigned a value to an identifer using const -short +for constant - it emplies that we have no intention of changing the value. it remains +constant. the second line the variable get reassigned a new value and therefore the +error message above showed up. +To solve this issue the variable age could simply be declared via "let" as seen below: +*/ + +let age = 33; +age = age + 1; \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..71f5a90c4 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,15 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +/* +The error message "ReferenceError: Cannot access 'cityOfBirth' before initialization + at Object. " comes up because when the computor reads the first line it + recognised that cityOfbirth is an identifer however it get confused as it has not + been declaed beforehand and runs into an error as a result. To overcome this issue + and access cityOfBirth, simply declare it in the 1st line and then in the 2nd line + use console.log() to display its value to the terminal as seen below: +*/ + +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..f5c695eeb 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,5 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,12 @@ 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 + + +/* +This error message was thrown after running the code "TypeError: cardNumber.slice is not a function". +It simply happned because ".slice()" is not a method associated with integers but strings & arrays. +To avoid this issue the value of last4Digits should be as below: +*/ + +const last4Digits = cardNumber.toString().slice(-4); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..67d2e39e9 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,7 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +//const 12HourClockTime = "20:53"; +//const 24hourClockTime = "08:53"; + +// to avoid the syntax error after running the above lines of code do as below: + +const clockTime24Hour = "20:53"; +const clockTime12Hour = "08:53"; \ 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..0234cd95d 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; @@ -20,3 +20,17 @@ 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? + +/* +a) There are 5 at: 1)carPrice = Number(carPrice.replaceAll(",", "")); & 2)priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")), +3) & 4) const priceDifference = carPrice - priceAfterOneYear; 5) const percentageChange = (priceDifference / carPrice) * 100; + +b) The error happened due to the missing comma "," in between the 2 parameters/arguments in line 5's function call. Just add a comma to solve +this problem. + +c) line 4 & line 5 + +d) lines 1, 2, 7 & 8 + +e) Firstly, it removes all commas in the string and eventually converts the new string into a number. +*/ diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..a52d13ae9 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 0; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -23,3 +23,13 @@ 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 + + +/* +a) There are 6 in total. +b) Only one function call "console.log(result)". +c) it represents the remainder from the division of movieLength by 60. +d) line 4 represents the total number of mins in the movie (as whole numbers -integers) by getting rid of the decimal parts (in secs). +e) Result represents the total movie duration (in exact hours, exact min and exact sec). movieDuration could be a good name. +f) Yes, It does work when trying different values of movieLength. +*/ diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..cd8cd5f54 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,9 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. ine lines 3-6, penceStringWithoutTrailingP is being declared to get rid of the "p" part of "399p" so that you end up with "399" ONLY. +// 3. Lines 8 paddedPenceNumberString adds "0" to the start of paddedPenceNumberString which is "399" with the aim of it have a total length of 3. +// 4. Lines 9-12, pound is being declared, the value of it aims to extract the "full pounds part" considering 1 pound = 1-- pence. +// 4. Lines 14-16, extract the "pence part" fraction +// 5. Line 18 prints out the sum in pounds and pense. + diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..1d5dac9db 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -11,8 +11,12 @@ In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +// a message with Hello world! message pops up in a small Chrome notification window 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`. +// small Chrome window pops up with "What is your name?" and underneath it a space for the user to enter and submit their name. What effect does calling the `prompt` function have? +//prompt expect input from the user by showing a small window with space for input to be submitted once called. What is the return value of `prompt`? +//when ok clicked the user's input is returned otherwise in cancellation null is returned. diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..3777c4258 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -4,13 +4,19 @@ 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? +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` +// got 'object' as an output Answer the following questions: What does `console` store? +//console is an object that provides a collection of methods and some properties for logging information, debugging JavaScript code, inspecting objects, timing code execution, and profiling performance. The console.log(), console.info(), console.warn(), console.error(), and console.debug() methods all accept any type of value (strings, numbers, objects, etc.) and are used for general, informational, warning, error, and debug logging respectively. console.dir() accepts an object and displays its properties in a tree format, while console.table() takes an array or object and presents it in a table. console.assert() takes a boolean condition and any value, logging the value only if the condition is false. console.count() uses a string label to count how many times it has been called. console.time() and console.timeEnd() take a string label and measure time between the two calls. console.group(), console.groupCollapsed(), and console.groupEnd() use string labels to group logs. console.trace() accepts no arguments and logs the call stack. console.profile() and console.profileEnd() take string labels and record performance data. What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +//console.log log or display values to the console. Console.assert, check if a condition is true and in this case no output. Otherwise, an +// error message shows. As for the `,`, it's called the dot notation and it's used to access properties and methods of an object.