diff --git a/.gitignore b/.gitignore index bde36e530..caafbee92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .DS_Store .vscode -**/.DS_Store \ No newline at end of file +testing.js +**/.DS_Store diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..2c97677eb 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,5 @@ 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 +// In line three we assign a new value to the variable "count". +// the = is used to say that this variable should have the value that will be set after the = diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..36ffe3f47 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,11 @@ 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); +initials = initials + middleName.charAt(0); +initials = initials + lastName.charAt(0); + +//console.log(initials); // 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..f705e73e8 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -10,14 +10,19 @@ // (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); + 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 + 1); +const ext = filePath.slice(filePath.lastIndexOf(".")); +// to test >> +//console.log(`The dir part of filePath is (( ${dir} )) AND the ext part of it 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..8def92382 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -1,9 +1,22 @@ const minimum = 1; const maximum = 100; -const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +num = Math.floor(Math.random() * (maximum - minimum) + 1); + +//console.log(num); // In this exercise, you will need to work out what num represents? // 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. const minimum = 1; : a variable has the name `minimum` declared. it stores hte 1 value. `const` means this var is unchangeable. +// 2. const maximum = 100; a variable has the name `maximum` declared. it stores hte 100 value. `const` means this var is unchangeable. +// 4. num = Math.floor(Math.random() * (maximum - minimum) + 1); : in this line, the variable `num` is declared, it stores a random number +// between 1 and 100. Math.random() generates a random decimal number between 0 and 1 (exclusive). +// then it multiplies this random number by (maximum - minimum) to scales it to a range between 0 and 99 (since maximum is 100 and minimum is 1). +// Adding 1 shifts this range to be between 1 and 100. +// the line had a mistake, it was declared as const num = ... but I changed it to num = ... because const means unchangeable, and here we want to change its value each time we run the code. +// Math.floor() then rounds down the result number to the nearest whole number, and ensures that num is an integer between 1 and 100. + +// So the whole code generates a random integer between 1 and 100 (inclusive) and stores it in the variable num. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..29723340e 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/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? + +// we can prevent the pc from running those lines by adding the commenting // lines \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..031839b47 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,4 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +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..2865eb8a4 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,5 @@ // 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}`); diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..091394972 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,17 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); +//console.log(last4Digits); // 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 // 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 assume the code won't work because the value inside "slice()" isn't valid. +// because we should set a valid number or 2 numbers (the first num is to set from where to start, +// the second number is to set where to finish) + +// Update: after the first run of the code I realized that CardNumber is actually a number not a string. +// so I think if I convert it into string the code will work properly. No problem with "slice()" value. diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..e8902c0c5 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,2 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const HourClockTime = "20:53"; +const hourClockTime = "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..ea3a2a94c 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; @@ -12,11 +12,29 @@ 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 +// -) functions called are: +// Number() ||| carPrice = Number(carPrice.replaceAll(",", "")); +// Number() ||| priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// replaceAll() ||| carPrice = Number(carPrice.replaceAll(",", "")); +// replaceAll() ||| priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// console.log()||| 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? +// The error is coming from line: 5. +// This error is occurring because a , is missing inside the replaceAll() so the two inputs aren't separated. +// I will fix this error by adding , after the first input inside replaceAll() +// replaceAll("," "") Changed to >> +// replaceAll(",", "") // c) Identify all the lines that are variable reassignment statements +// Line 4 +// Line 5 // d) Identify all the lines that are variable declarations +// Line 1 +// Line 2 +// Line 7 +// Line 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// it replaces the , that is inside the number with nothing ( removes the , from the 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..f57171a88 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 = 86399; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,30 @@ 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 only 1 function call. // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// this expression divided the movie length (is seconds) by 60 so we get how many minutes the movie length is. +// but since we used % not / that means we only took the reminder left over after dividing the number. +// so basically the code identifies how many seconds will be left over after getting the minutes amount. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// it means that the result of this expression will be the total minutes of the video. +// before that - in line 3 - it took the number of seconds left, and in line 4 it divides the +// movie length ( takes the remaining seconds out ) by 60 because a minute contains 60 seconds. +// so it actually converts the seconds into minutes. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The output of the variable result represents time (period of the movie). +// it actually shows the result of converting seconds into readable time format. +// Better to call it (ReadableMovieTime). // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// It does work with most if the values, BUT if I put a number that's bigger than 86399, which returns: 23:59:59 +// that means it ignores the days, it can be mediated so it can count days, months and years. +// I haven't watched a movie that is longer than a day but just in case :), I think the code should be kind of general sometimes!! +// Also two zeros should be displayed if there're not hours or mins (when I put small numbers that are less that an hour), so it gets more understandable. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..dd548bd21 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,10 +1,9 @@ -const penceString = "399p"; +const penceString = "105p"; const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 ); - const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, @@ -25,3 +24,22 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. +// 3. const penceStringWithoutTrailingP = penceString.substring( : declares new var and assigns the pence string excluding trailing p +// 4. 0, : first value of the function, this is to tell the function to start from the first charachtar in the string +// 5. penceString.length - 1 : second value of the funtion, it takes the string lengs mines 1 (to exclude trailing p) +// 6. ); : closed th funtion. +// 7. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); : it padds the number with 0 from the start (to make number readable and clear) +// 8. const pounds = paddedPenceNumberString.substring( : declares new variable to store pounds, it takes the last two numbers from the number and stores everything that is before the last two numbers because they are pence not pounds. +// 9. 0, : set 0 to start from the first character. +// 10. paddedPenceNumberString.length - 2 : here is where it excludes the last two numbers (the pence). +// 11. ); : closes function. +// 12. +// 13. const pence = paddedPenceNumberString : declares new var to store pence +// 14. .substring(paddedPenceNumberString.length - 2) : identifies the last two numbers (pence) and stores them. +// 15. .padEnd(2, "0"); : to pad the pence with 0 from the end +// 16. +// 17. console.log(`£${pounds}.${pence}`); : to write the result, it prints the two variables, pounds and pence. it adds £ to show that it's pound. + + + diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..22aa35b30 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 small box shows up showing the string "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`. What effect does calling the `prompt` function have? +a small box in the browser shows up and displays a form to fill. + What is the return value of `prompt`? +the return value of the 'prompt' is the string I enter in that box. \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..df4d2527d 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,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? + log() { [native code] } Now enter just `console` in the Console, what output do you get back? +it shows information that look like variables and values, and starts with something like: +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} Try also entering `typeof console` Answer the following questions: What does `console` store? +Console stores several functions inside it, functions like log(), assert(), debug() and more. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +`console.log` means that we want to use the function log() that is inside the object (native object) `console`. the dot . means is like navigating from the object to the function inside that object. +so `console.log` means "use the object `console`, then from the functions inside the object use log()". diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js deleted file mode 100644 index 653d6f5a0..000000000 --- a/Sprint-2/1-key-errors/0.js +++ /dev/null @@ -1,13 +0,0 @@ -// Predict and explain first... -// =============> write your prediction here - -// call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring - -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} - -// =============> write your explanation here -// =============> write your new code here