diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..6d5a2ade4 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 variable count to the value of count plus one so the new value would be 1 as count was originally zero so count is now 1. diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..caeb65f6b 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -4,7 +4,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 = + firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); -const initials = ``; - +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..03aaedf68 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -9,15 +9,13 @@ // (All spaces in the "" line should be ignored. They are purely for formatting.) -const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; -const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(lastSlashIndex + 1); +const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/photo/file.txt"; +const lastSlashIndex = filePath.lastIndexOf("/"); //44 +const base = filePath.slice(lastSlashIndex + 1); //file.txt 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 = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +const dir = filePath.slice(0, lastSlashIndex); +const ext = filePath.slice(filePath.lastIndexOf(".")); +//lastIndexOf() method works better here because it'll look for the last time "." appears in the file path which will always give the correct file ext instead of going a set number back with the slice() method i used previously which may not work for every file extension depending on it length. So now console.log will print .txt or .jpeg if that was the file ext +console.log(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..bf102b41f 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,12 @@ 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) +//output: random whole number + +//So num is storing the value of a random number generated by the method Math.random this will be a decimal number between zero and one that is then multiplied by 100 (100(maximum) -1(minimum) +1) and then the Math.floor method will round that number down to the nearest whole number and then finally whatever this number is, one is added on to that number and this final number is what is stored in the variable num. This final number is what is stored in the variable num. The smallest value it can be is 1, and the largest value it can be is 100 as plus minimum means 1 will always be added. + +//example Math.random () = 0.97 +//0.97 * 100 = 97 +//Math.floor = 97 +//97+1 = 98 diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..044add7ac 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -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? diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..8a597f2bc 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,9 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; + +console.log(age); + +// TypeError: Assignment to constant variable. +//This means javascript cannot reassign const variables. Let must be used in order for the variable to be reassigned so it can change. I have now declared the variable using the let keyword and it now prints (34) in the console. diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..820b1b1dd 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/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 ? - -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + +// the variable was initially declared after console.log so it threw an error message. The error message "cannot access 'cityOfBirth' before initialization at Object" is saying that javascript cannot access the variable before it has been initialised. I have initialised the variable on line 3 now so the message prints in the console now. diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..ac9a4fbf5 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,4 +1,4 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber @@ -7,3 +7,9 @@ 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 + +// I predict that it'll throw a syntax error as its not wrapped in "" so it's registering as a number and but it needs to be a string so the .slice() method can register each individual number in last4Digits? +//console.log(last4Digits) +//output: TypeError: cardNumber.slice is not a function +//it's slightly different to what i expected as i thought it was solely about not using the right syntax but it's a type error which after research type error is about not being able to perform a certain operation on a value so here the slice method wouldn't work as .slice() method can only work on strings and arrays so i need to turn cardNumber into a string before the slice() method can work on it. so whilst i was right that it needed "" my reason for why was wrong. +console.log(last4Digits); diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..14a8da414 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,10 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const TwelveHourClockTime = "8:53pm"; +const TwentyFourHourClockTime = "20:53"; +console.log(TwelveHourClockTime); +console.log(TwentyFourHourClockTime); + +//output: SyntaxError: Invalid or unexpected token +// Javascript variables cannot start with a number +// Updated numbers in each variable from 12HourClockTime to TwelveHourClockTime +//and 24HourClockTime to TwentyFourHourClockTime and both times now print in +//the console. diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..40f4fe428 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; @@ -12,11 +12,14 @@ 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 - +//There are 5 function calls in this File. Line 4(Number and carPrice.replaceAll()), line 5(Number and priceAfterOneYear.replaceAll()) and line 10 (console.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? +// The error message in the terminal tells me there is a syntax error on line 5. There is a comma missing separating the two arguments. I can fix the problem by adding a comma. The console now prints "The percentage change is 14.57". // c) Identify all the lines that are variable reassignment statements +// There are 2 variable reassignment statements in this file carPrice and priceAfterOneYear as both were first declared on line 1 and 2 using the let keyword before being reassigned on lines 4 and 5. // d) Identify all the lines that are variable declarations - +// the variable declarations are on lines 1 and 2 and lines 7 and 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +//It is taking the value stored in the variable carPrice and running the method .replaceAll() on the value. so here it is taking 10,000 and removing the comma and then finally this value is turned from a string into a number by being passed into the number function. diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..a6cdf3acf 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,18 @@ 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? +//There are 6 variable declarations. // b) How many function calls are there? +// There is 1 function call the console.log // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// the expression uses the remainder operator which returns the remainder left over when one value is divided by another. so here it is asking how many times the number 60 goes into the value stored in the variable movieLength and tell me what is left behind which is what will be stored in the variable remainingSeconds (24). // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +// The expression here is taking the value stored in remainingSeconds variable and subtracting it from the value stored in movieLength variable, then whatever this value is is divided by 60 and this final number is what gets stored in the variable totalMinutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? - +// The variable result will print a template string showing the movie length as time in hours, time in minutes and time in seconds using a template string. I think movieDuration is a bit more descriptive than result? // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//The code will work if movieLength is a whole number but it wouldn't work with a decimal number or likely not as expected if it is string unless it contains a number. diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..087d56595 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -2,13 +2,13 @@ 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 @@ -23,5 +23,15 @@ console.log(`£${pounds}.${pence}`); // You need to do a step-by-step breakdown of each line in this program // 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" + +// 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); initialises a variable that takes the value of variable penceString and uses the method substring() to remove the "p" from "399p" so the variable penceStringWithoutTrailingP now stores the string "399" + +//3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); initialises a variable called paddedPenceNumberString that takes the value of variable penceStringWithoutTrailingP and uses the method .padStart() to make sure that the string is always 3 characters long, if it's shorter javascript will pad the value out with a 0 at the start of the value. e.g 39 = 039 or 9 = 009. so paddedPenceNumberString stores "399" + +//4.const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2,); initialises a variable that called pounds that takes the value inside paddedPenceNumberString and uses the method subString() on it to remove the last two numbers from the padded string value. So the variable pounds now stores the string "3" which represents £3 + +//5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +//initialises a variable called pence that takes the value of the variable paddedPenceNumberString and runs the .substring() method on it and isolates the last two numbers returning 99. the method .padEnd() then looks at this value and if it's two characters long it wont do anything, if it's less it'll add a 0 to the end of the value. In this case it produced 99 so it doesn't do anything. So the variable pence now stores "99" which represents £.99 + +//6. console.log(`£${pounds}.${pence}`); This will combine and print the final price using a template literal so the console will print (£3.99) diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..0c6822ea5 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -8,8 +8,11 @@ Let's try an example. In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; What effect does calling the `alert` function have? - +it opens a pop up message with the string I passed it (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`. +const myName = prompt("What is your name?") +console.log(myName) +output: Mars -What effect does calling the `prompt` function have? -What is the return value of `prompt`? +What effect does calling the `prompt` function have? It opens a pop up box asking the user to enter something in a text box here it asked what my name was. +What is the return value of `prompt`? it returns the information entered into the text box as a string so as i entered my name it would return "Mars" and store it in the variable Myname. diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..440111eac 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -5,12 +5,18 @@ 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 `console` store? it stores tools that i can use to test/ check my code. What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console.log means open the console and print a certain value or message into the terminal +console.assert allows me. to run quick tests to make sure my program is working and will only alert me if the code is broken. +the "." translates to find this tool.