diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..ddc6cf1dc 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/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: +//Line 3 re-assigns the value of count and declares a new value increasing count by 1 \ 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..a17b870bc 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,14 @@ 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 = ``; +//Answer: +//Solution using charAt() a Javascript string method that returns a character's position, using template literal +// and toUpperCase to ensure it is capitalized. +let initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`.toUpperCase(); +console.log(initials); + +//alternative solution using string indexing, template literal and toUppercase(to make sure it is capitalized) +//let initials - `${firstName[0]}${middleName[0]}${lastName[0]}`.toUpperCase()); // 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..82cdfd0e1 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,19 @@ 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 = ; +//Answer: +//**where the file lives** +// It gives me everything from Index 0 to +// the last slash just before the filename. +const dir = filePath.slice(0, lastSlashIndex); +console.log(dir); + +//Answer: +//**what type of file** +//Uses the the lastIndexOf method to look for the dot. +//the dot is now index 0 so we start from index 1 onwards +// to get the file type. +const ext = filePath.slice(filePath.lastIndexOf(".") + 1); +console.log(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..faffcc881 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -4,6 +4,23 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // In this exercise, you will need to work out what num represents? +//*Answer: +//Number returns a random whole number between 1 and 100. + // Try breaking down the expression and using documentation to explain what it means +/*Answer: +It uses 2 methods to achieve this +Math.random generate a random number between 0 and 1 eg. 0.343, 0.5. 0.67 +Math.floor rounds the number to a whole number. +In this declaration: +Math.random will return a random decimal number and multiplies it by the sum of +((maximum-minimum)+1). eg 0.343_*((100-1)+1) = 34.3. +Math.floor will take the total 34.3 and round it to a whole number 34*/ + // It will help to think about the order in which expressions are evaluated +//*Answer: +//it first works out the random decimal number calculates the other values then evaluates to sum to a whole number. + // Try logging the value of num and running the program several times to build an idea of what the program is doing +//*Answer: +//28, 64, 52, 86 diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..779581a32 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,8 @@ -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?*/ + +//*Answer +// We comment it out, +// We can do this by using a two forward slashes ("//")if it is on one line +//or we use one forward slash with an asterisk at the start("/*") and an asterisk +// and a forward slash at the end ("*/") for block commenting diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..1ddae46c7 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,11 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +//const age = 33; +//age = age + 1; + +//Answer: +//we cannot re-assign variables with "const". +// We have use "let". let allows the variable to be re-assigned. +let age = 33; age = age + 1; +console.log(age); diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..15c9eba46 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,12 @@ // 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}`); +//console.log(`I was born in ${cityOfBirth}`); +//const cityOfBirth = "Bolton"; + +//*Answer +// The console.log is before the variable and it cannot see it. +// The console log needs to be below the variable to compute the data. + const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..d880a5c70 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,24 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//const cardNumber = 4533787178994213; +//const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working + // Before running the code, make and explain a prediction about why the code won't work +//*Answer +//It won't work because cardNumber is not a string and .slice() only works with strings. It will throw an error. + // Then run the code and see what error it gives. +//*Answer +//Uncaught TypeError: cardNumber.slice is not a function + // Consider: Why does it give this error? Is this what I predicted? If not, what's different? +//*Answer +//I was right. cardNumber is a number not a string and .slice() only works with strings +// so it needs to be converted to a string to work using .toString() + // Then try updating the expression last4Digits is assigned to, in order to get the correct value +//*Answer +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..8b2a65708 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"; + +//What's wrong with this code? +//*Answer +//The variable is not named in line with the Javascript naming convention. +// When declaring a variable it must not start with a number, space or reserved word. +// to fix this, we can rename them correctly. + +const twelve_HourClockTime = "20:53"; +const twentyFour_HourClockTime = "08:53"; diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..8bce84a53 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,22 +1,47 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +carPrice = Number(carPrice.replaceAll(",", "")); // returns 10000 +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));//returns 8543 +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// incorrect code missing syntax ie a comma const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; -console.log(`The percentage change is ${percentageChange}`); +console.log(`The percentage change is ${percentageChange}`); //returns The percentage change is 14.57 // 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 +// 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: 2* Number(), 2*.replaceAll() and 1* console.log(). +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); +//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? +// 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 it is missing a comma on line 5. +// Add the comma to fix it: ...replaceAll(",","")); // c) Identify all the lines that are variable reassignment statements +//*Answer +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + // d) Identify all the lines that are variable declarations +//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? +//It converts the carPrice into a number and removes the spaces and commas. + -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..ed32ba369 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,37 @@ 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? +// *Answer +// There are 6. // b) How many function calls are there? +// *Answer +// There is 1; console.log() // c) Using documentation, explain what the expression movieLength % 60 represents +//*Answer +// This expression returns the remainder of seconds, after dividing the movieLength by 60, +// using the modulus operator it returns 24 seconds // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators -// d) Interpret line 4, what does the expression assigned to totalMinutes mean? - -// 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 +// d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// *Answer +// It is converting the seconds to minutes and rounding it to the last whole minute. + +// e) What do you think the variable result represents? +// *Answer +// The result is the duration of the film in hours, minutes and seconds - 2:26:24. + +// Can you think of a better name for this variable? +// *Answer +// movieDuration + +// f) Try experimenting with different values of movieLength. +// Will this code work for all values of movieLength? Explain your answer, +// *Answer +// I changed the value to; +// 1200 returned 0:20:00 +// 92383.7 returned 25:39:43.69999999999709 +// -8784 returned -2:-26:-24 +// it worked with all numbers including decimals and negative numbers. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..9a133d8d3 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,34 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +// 1. const penceString = "399p": initialises a string variable with the value "399p" + +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); +// This takes the variable penceString and uses the .substring() method to extract part of the string. +// It starts at index 0 and ends at penceString.length - 1, which removes the last character and +// outputs all characters except the trailing "p". +// This returns "399". + +// 3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// This takes the value of the penceStringWithoutTrailingP and uses .padStart() method to always return a minimum +// length of 3 characters. if the value computed is not a least 3 characters, it outputs leading 0's to make +// to make the string to 3 characters. If the string is 4 or more characters it just outputs the value without the leading 0's. +// This returns "399". (examples: 45 returns 045, 1295 returns 1295) + +// 4.const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2); +// This takes the value of paddedPenceNumberString and uses the .substring() method to extract the first art of the string. +// It starts at index 0 and ends at paddedPenceNumberString.length - 2, which removes the last two characters. +// This leaves the pounds portion of the value. +// This returns "3". (examples: 45 returns 0, 1295 returns 12) + +// 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// This takes the variable paddedPenceNumberString and extracts the last two characters using .substring(), +// which represent the pence. If the string is shorter than 2 characters, .padEnd(2, "0") adds trailing zeros +// to ensure the pence portion is always 2 digits. +// This returns "99". (examples: 45 returns 45, 1295 returns 95) + +// 6. console.log(`£${pounds}.${pence}`); +// console.log() outputs the formatted money value to the console. It uses a template literal (backticks) +// to embed the variables `pounds` and `pence` directly inside the string using ${} syntax. +// The £ symbol is included at the start, and the dot separates pounds and pence. +// This returns "£3.99" (examples: 45 returns £0.45, 1295 returns £12.95) diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..bceae3885 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -9,10 +9,18 @@ Let's try an example. In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; +alert('Hello World!'); What effect does calling the `alert` function have? +A pop up modal box appears with "Hello World!" 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`. +let myName = prompt("What is your name?"); What effect does calling the `prompt` function have? +It displays a modal dialog box with a text input field which can be completed. + What is the return value of `prompt`? +It is hidden, it does not display anything. The box closes. +When I do a console.log(myName); it returns Angela. + diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..6da4b7fcc 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,21 @@ 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? -What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +It does not store anything. It's purpose is to hold a collection of methods (functions) that help you debug, log, or inspect your code. + +What does the syntax `console.log` or `console.assert` mean?, +They print outputs.It provides methods for debugging, logging, and inspecting code. + +In particular, what does the `.` mean? +The dot is a member access operator that connects the object to its property or method.