diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..e526d8709 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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: The = is an assignment operator (as opposed to == and === which are comparison operators). +// In line 3, a new value is assigned to the variable count. This is allowed because the variable was +// declared using let. count is reassigned to the old value of count plus 1. diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..b7bf464fa 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,8 @@ const 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. -const initials = ``; +const initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`; + +console.log(initials); // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..7786eea00 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,11 @@ 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); +console.log(`The dir part of ${filePath} is ${dir}`); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +const lastPeriodIndex = filePath.lastIndexOf("."); +const ext = filePath.slice(lastPeriodIndex); +console.log(`The ext part of ${filePath} is ${ext}`); + +// https://www.google.com/search?q=slice+mdn diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..0e990d271 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,20 @@ 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 + +console.log(num); + +// Answer: num is a random number between 1 and 100, including 1 and 100. So if I change the values of minimum +// and maximum it will be a random value between them, including them. + +// Math.random generates a random value between 0 and 1, including 0 but not 1. + +// (maximum - minimum + 1) gives the amounts of values we want to be able to generate. +// If minimum was 60 and maximum was 70, inclusive, that would make 11 possible values + +// Math.random * (maximum - minimum + 1) would generate any value between 0 and 11, including 0 but not 11 + +// Math.floor rounds the value down, leaving us with any value between 0 and 10 inclusive + +// + minimum at the end, 60 in our thought experiment, makes the values range between 60 and 70 inclusive +// which is what we wanted. diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..450020776 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,4 @@ -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: I turned the lines into comments, now they won't run diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..a7cf437c1 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,8 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; + +// Answer: I got the error message TypeError: Assignment to constant variable. +// That is because age was declared using const, which makes it non-reassignable. +// I changed the const to let, so the variable age becomes reassignable diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..bb7172d39 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,9 @@ // 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}`); + +// Answer: I got the error message ReferenceError: Cannot access 'cityOfBirth' before initialization +// This is because the program tries to access cityOfBirth before it has been initialized. +// I switched the lines around, so cityOfBirth gets declared before it is referenced. diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..c5bd3dde5 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,5 +1,6 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +8,16 @@ 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 + +// Prediction: I believe slice counts forward from the start when you use positive values, and +// backwards from the end when you use negative. So if you only put in one negative number, it's counts +// backward from the end to the last 4, but can't "turn around" to count up to the end again..? + +// After running the code: The actual error was a TypeError, saying that cardNumber.slice is not a function. +// That means you can't put anything in parenthesis after cardNumber.slice. However, when I used slice before +// slice definitely takes parameters. This made me think about the fact that cardNumber is actually a +// number, and slice is probably only for strings or arrays. You can't really count positions in a number. + +// I solved it by chaining a toString before slicing. My initial theory was completely wrong, putting a +// negative number in slice is completely fine. Now last4Digits is a string, not a number. But it seems +// easier to work with as a string, if specific digits need to be accessed, so I will keep it as a string. diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..c248f3d61 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const twelveHourClockTime = "8:53pm"; +const twentyfourHourClockTime = "20:53"; + +// Answer: I got the error message SyntaxError: Invalid or unexpected token +// There were arrows pointing at "12" in 12HourClockTime. That is because +// variable names cannot start with numbers in javascript. I changed them to start with letters. diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..051719b4e 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/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,30 @@ 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 +// 5 function calls are made. +// Line 4 calls function replaceAll and Number +// Line 5 calls functions replaceAll and Number +// Line 10 calls 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? +// There is a syntax error. The error says a closing parenthesis is missing, which I think is triggered +// because a comma in the argument list is missing. I added the comma and the error is resolved. + // c) Identify all the lines that are variable reassignment statements +// Line 4 reassigning carPrice +// Line 5 reassigning priceAfterOneYear + // d) Identify all the lines that are variable declarations +// Line 1 declares carPrice with let +// Line 2 declares priceAfterOneYear with let +// Line 7 declares priceDifference with const +// Line 8 declares percentageChange with const + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// The original carPrice is a string. In order to do math and get a percentage the string needs to be converted +// into a number first. And before that the string's comma needs to be removed (it's replaced with nothing +// using replaceAll) diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..49c003163 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 145678; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -13,13 +13,33 @@ console.log(result); // a) How many variable declarations are there in this program? +// 6 + // b) How many function calls are there? +// 1, console.log(result) on line 10 + // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// % is the remainder arithmetic operator. The expression gives the remainder after the number of seconds +// the movie is long is divided by 60. After you have the full amount of minutes this is the amount of +// remaining seconds that don't add up to a full minute. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// removing remainingSeconds from movieLength leaves a number of seconds, slightly shorter than the movie length, +// that are an exact amount of minutes. It is divisible by 60. Dividing that number by 60 gives the exact +// number of minutes. + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// It represents the movie length as a string expressed in hours, minutes and seconds. A different name could +// be moiveLengthDestructured, movieLengthMessage, movieLengthInHMS, movieLengthFormatted, movieLengthTimeFormat + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// It technically works for all movie lengths, but the format gets weird. when a value is a single digit, you +// would expect 05 or 00, but it just says 5 or 0 which isn't a standard way to show time. Also, when the +// hours go above 24 you would expect it to start counting days, but the hours just add up instead as +// you try higher values. diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..d572259cf 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -2,13 +2,14 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 + penceString.length - 1, ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 + paddedPenceNumberString.length - 2, ); const pence = paddedPenceNumberString @@ -25,3 +26,41 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// Answers + +// 2. const penceStringWithoutTrailingP = penceString.substring( +// 0, +// penceString.length - 1 +// ); +// This declares and initialises a new variable penceStringWithoutTrailingP that is a substring of +// penceString, starting at position 0 and up to but not including the last position. So the new variable +// gets the value "399", the p is left behind. + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// We now want our value to be at least 3 digits long, but in case it is shorter, we want to fill up with zeros at +// the start so it becomes 3 digits long. padStart does that, the 3 specifies minimum amount of digits and the "0" +// specifies what to fill with. The result gets saved in new variable paddedPenceNumberString. + +// 4. const pounds = paddedPenceNumberString.substring( +// 0, +// paddedPenceNumberString.length - 2 +// ); +// Now we save the pounds amount into new variable pounds. We get the amount (which could be any amount of digits long) +// by creating a substring from paddedPenceNumberString starting at position 0 and ending before the next to +// last digit. The last two digits are the pence amount and they aren't included in pounds. + +// 5. const pence = paddedPenceNumberString +// .substring(paddedPenceNumberString.length - 2) +// .padEnd(2, "0"); +// Similarly to the previous operation, here we save the pence amount into new variable pence. We get it by +// using substring on paddedPenceNumberString again, this time starting two digits from the end, saving the +// final two digits into pence. Then, if the digits are fewer than 2, we chain a padEnd to add "0" until pence +// is 2 digits long. +// After trying different values, it seems like it is unnecessary to have the padEnd, since the pence will +// always already be 2 digits long. Even if penceString started with just 1 digit, "0"s will have been padded +// earlier at the beginning of the string, so pence can always be 2 digits and the padEnd will never take effect. + +// 6. console.log(`£${pounds}.${pence}`); +// Using a template literal, here we log the £ sign followed by the the pounds amount, a period and the pence +// amount, which is formatted to understand easier than the original penceString was. diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..c405b0a9a 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -9,7 +9,14 @@ In the Chrome console, invoke the function `alert` with one argument, the string What effect does calling the `alert` function have? +Answer: An alert popped up + 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? + +Answer: A poppup prompts me to write my name. + What is the return value of `prompt`? + +Answer: The value I entered in the text box. diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..8279c374d 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -6,11 +6,23 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +Answer: f log() { [native code] } + Now enter just `console` in the Console, what output do you get back? +Answer: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} + Try also entering `typeof console` +Answer: it's an 'object' !!! + Answer the following questions: What does `console` store? + +Answer: A console object with loads of console functions (methods)? + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +Answer: I think the period means it's a method call, for console object methods. and log and assert are the +methods.