diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..3986d0991 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,7 @@ 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 is assignment statement,its taking the current value to count,then make the count to a new value. \ 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..5ee11d5e5 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 = ``; +Answer: +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..255e6c3d1 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -16,8 +16,10 @@ 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 = ; +Anwer: +const dir = filePath.slice(0, lastSlashIndex); +const ext = base.slice(lastDotIndex + 1); +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..53f89bb75 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 + +Answer: + +//num is a random number between 1 and 100. + +//Math.random() makes a number between 0 and 1. + +//times (maximum - minimum + 1) makes it bigger. + +//Math.floor cuts off the decimal. + +//+ minimum makes sure the random number starts at 1 instead of 0 + diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..4d14d2c7d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,6 @@ 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? + +Answer: +//We can turn those lines into comments by putting // at the start. +//Then computer ignores them but humans can still read/see them. diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..d1cd24c47 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,10 @@ const age = 33; age = age + 1; + +Answer: + +//This gives an error because we used const, which can't be changed. +// If we want to update the value, we should use let instead of const. +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..9560bcd9e 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,12 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + + +Answer: + +//Error because cityOfBirth is used before it is declared. +//We need to define the variable first, then use it. + +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..606c38bba 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -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 + +Answer: + +//I guessed it breaks because slice is for strings but cardNumber is a number. +//When I try it, the error says slice is not a function (so I was right). +//Fix: change the number to string first, then slice works. +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); // 4213 diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..98dd10aba 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,10 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + + +Answer: + +// The first line is wrong cuz variable names can't start with a number. +// We should rename it like hour12ClockTime or something that doesn't start with a digit. +const hour12ClockTime = "20:53"; +const hour24ClockTime = "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..84caa4b02 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,31 @@ 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 +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? +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +fix; +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 +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? +replaceAll gets rid of the commas → "10,000" becomes "10000". + +Number() turns that string into the number 10000. + +so we can do maths with it. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..ebf1a9c67 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,21 @@ 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? +6: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result. // b) How many function calls are there? +Only 1 ; 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 +% gives the leftover after dividing, so movieLength % 60 = extra seconds // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +(movieLength - remainingSeconds)/60 → takes away seconds, then gets whole minutes + // e) What do you think the variable result represents? Can you think of a better name for this variable? +It’s the time of the movie written as hours:minutes:seconds. A better name could be movieTime or formattedTime. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +works for normal numbers, but long movies or single-digit mins/secs look weird \ 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..6243ddf9a 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,21 +1,30 @@ const penceString = "399p"; +sets the price string with "p" at the end + const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 ); +//removes the p from the end + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 ); +// makes sure it has at least 3 digits, adds 0 at start if needed + const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); + // takes last 2 digits → pence, adds 0 at end if needed console.log(`£${pounds}.${pence}`); +// prints the price in pounds like £3.99 + // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..ea58a5721 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? +*It shows a popup message on the screen.* 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? +*pops up a box asking you to type something* + What is the return value of `prompt`? +*whatever the user typed (as a string)* diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..366db3207 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -7,10 +7,14 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? Now enter just `console` in the Console, what output do you get back? +*shows a big list of all the stuff it can do, like log, assert, and other functions* Try also entering `typeof console` Answer the following questions: What does `console` store? +*console stores all the console functions you can use.* + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +*the . means “use a function from console.*