diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..baa67cfba 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 + +//Answer: The = means assignment. The value of count is 0 but it increments by 1 \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..8ffb7ed7a 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -1,10 +1,16 @@ const firstName = "Creola"; const middleName = "Katherine"; const lastName = "Johnson"; +// const initial = "initials"; +// const index = 1; +// console.log('The ${Initial} ${index} is ${firstName.charAt(index)}'); // 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[0] + middleName[0] + lastName[0]; +console.log(initials); // https://www.google.com/search?q=get+first+character+of+string+mdn + +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..244114af6 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -13,11 +13,17 @@ 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}`); +// console.log('The dir path of ${dir} is ${filePath}.${lastSlashIndex}'); + // 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); +const ext = base.slice(base.lastIndexOf(".")); + +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${base} is ${ext}`); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +// https://www.google.com/search?q=slice+mdn +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..728951db4 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -2,8 +2,15 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +// (0.68 * 100 ) + 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 + +// Answer: For calculations i utilised BODMAS formula solving numbers in brackets first, multiplication, subtraction and addition +// I used 0.68 for math.floor(random number) + 1 +// Sum = 69 +// CN - added comment to have a clean commit diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..686f4e9db 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,5 @@ -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? + +// I have commented out the lines. The computer removes commented lines from code compilation +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..c67081945 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,10 @@ // 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) + +// The TypeError: Assignment to constant variable implies we are trying to reassign the variable twice. +// This case, I have used let instead in order to allow the variable to be reused. +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..fa92713c9 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}`); + + +// The error is ReferenceError: Cannot access 'cityOfBirth' before initialization +// Answer: I switched the order by declaring the const first before calling /printing diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..2f9f9550f 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,5 +1,7 @@ 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 +9,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 + +//Prediction was the code would run successfully without error although with the wrong results due to absence of syntax errors in the file +// Error returned: TypeError: cardNumber.slice is not a function +// Lesson learnt here; slice method is only available for strings or arrays not numbers +// So converted the cardNumber into a string first +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..b9c0d88d3 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,7 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const twelveHourClockTime = "8:53pm"; +const twentyFourHourClockTime = "20:53"; +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); + +// Error - SyntaxError: Invalid or unexpected token +// lesson learnt - variables cannot start with a number \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..5e410b9be 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,20 @@ 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 - +// 2 +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ",")); // 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) error = SyntaxError: missing ) after argument list +// (",", ",")); - added , between quoted values // 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? +// e) cleans the amount format by removing characters such as , and leaving only number +// CN - added comment to have a clean commit diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..3e4fe242e 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 = -10; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -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 variables // b) How many function calls are there? - +// 2 // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// It means 60 % remainder of Movie length // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// totalMinutes is assigned a value of the result from (movieLength - remainingSeconds) / 60; // e) What do you think the variable result represents? Can you think of a better name for this variable? +// I think its the total movie length with a timer. Based on research it appears to be template literal variable as it mixes static text with dynamic data. Sorry I don't fully understand this bit yet // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//any number greater than zero returns a valid positive hour, minute or seconds result. Changing the length to 0 returns 0:0:0. Any negative length returns negative values + +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..0229893c3 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -1,19 +1,13 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); + 0, penceString.length - 1); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); +const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); + +const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); console.log(`£${pounds}.${pence}`); @@ -25,3 +19,9 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP = penceString.substring(0): sets penceStringWithoutTRailingP = 399.0 - penceString -1 = 39 +// 3. const paddedPenceNumberString - ensures the figure is 3 characters to taking us back to 399 +// 4 const pounds - removes 2 characters from the amount = 3 +// 5. const pence - adds the amount by 2 characters taking us back to either 39 or 99 +// 6. console displays the figures in pounds and pence = 3.99 +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/4-stretch-explore/chrome.md b/Sprint-2/4-stretch-explore/chrome.md index 962580b82..0773d48ec 100644 --- a/Sprint-2/4-stretch-explore/chrome.md +++ b/Sprint-2/4-stretch-explore/chrome.md @@ -13,3 +13,4 @@ Now try invoking the function `prompt` with a string input of `"What is your nam What effect does calling the `prompt` function have? What is the return value of `prompt`? +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/4-stretch-explore/objects.md b/Sprint-2/4-stretch-explore/objects.md index 0216dee56..e540040c2 100644 --- a/Sprint-2/4-stretch-explore/objects.md +++ b/Sprint-2/4-stretch-explore/objects.md @@ -14,3 +14,4 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +// CN - added comment to have a clean commit \ No newline at end of file diff --git a/Sprint-2/README.md b/Sprint-2/README.md index a47afc540..1967d9e89 100644 --- a/Sprint-2/README.md +++ b/Sprint-2/README.md @@ -33,3 +33,4 @@ https://developer.mozilla.org/en-US/docs/Web/JavaScript ## 4 Explore - Stretch 💪 This stretch activity will get you to start exploring new concepts and environments by yourself. It will do so by prompting you to reflect on some questions. +. \ No newline at end of file diff --git a/Sprint-2a/1-key-exercises/1-count.js b/Sprint-2a/1-key-exercises/1-count.js new file mode 100644 index 000000000..baa67cfba --- /dev/null +++ b/Sprint-2a/1-key-exercises/1-count.js @@ -0,0 +1,8 @@ +let count = 0; + +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 = means assignment. The value of count is 0 but it increments by 1 \ No newline at end of file diff --git a/Sprint-2a/1-key-exercises/2-initials.js b/Sprint-2a/1-key-exercises/2-initials.js new file mode 100644 index 000000000..8da507f7e --- /dev/null +++ b/Sprint-2a/1-key-exercises/2-initials.js @@ -0,0 +1,14 @@ +const firstName = "Creola"; +const middleName = "Katherine"; +const lastName = "Johnson"; +// const initial = "initials"; +// const index = 1; + +// console.log('The ${Initial} ${index} is ${firstName.charAt(index)}'); +// 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[0] + middleName[0] + lastName[0]; +console.log(initials); + +// https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2a/1-key-exercises/3-paths.js b/Sprint-2a/1-key-exercises/3-paths.js new file mode 100644 index 000000000..6becd448d --- /dev/null +++ b/Sprint-2a/1-key-exercises/3-paths.js @@ -0,0 +1,28 @@ +// The diagram below shows the different names for parts of a file path on a Unix operating system + +// ┌─────────────────────┬────────────┐ +// │ dir │ base │ +// ├──────┬ ├──────┬─────┤ +// │ root │ │ name │ ext │ +// " / home/user/dir / file .txt " +// └──────┴──────────────┴──────┴─────┘ + +// (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}`); +// console.log('The dir path of ${dir} is ${filePath}.${lastSlashIndex}'); + + +// 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 = filePath.slice(0, lastSlashIndex); +const ext = base.slice(base.lastIndexOf(".")); + +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${base} is ${ext}`); + +// https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-2a/1-key-exercises/4-random.js b/Sprint-2a/1-key-exercises/4-random.js new file mode 100644 index 000000000..7ac6f11d6 --- /dev/null +++ b/Sprint-2a/1-key-exercises/4-random.js @@ -0,0 +1,15 @@ +const minimum = 1; +const maximum = 100; + +const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +// (0.68 * 100 ) + 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 + +// Answer: For calculations i utilised BODMAS formula solving numbers in brackets first, multiplication, subtraction and addition +// I used 0.68 for math.floor(random number) + 1 +// Sum = 69 diff --git a/Sprint-2a/2-mandatory-errors/0.js b/Sprint-2a/2-mandatory-errors/0.js new file mode 100644 index 000000000..58b5e69c3 --- /dev/null +++ b/Sprint-2a/2-mandatory-errors/0.js @@ -0,0 +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? + +// I have commented out the lines. The computer removes commented lines from code compilation \ No newline at end of file diff --git a/Sprint-2a/2-mandatory-errors/1.js b/Sprint-2a/2-mandatory-errors/1.js new file mode 100644 index 000000000..58a6230d8 --- /dev/null +++ b/Sprint-2a/2-mandatory-errors/1.js @@ -0,0 +1,9 @@ +// trying to create an age variable and then reassign the value by 1 + +let age = 33; +age = age + 1; + +console.log(age) + +// The TypeError: Assignment to constant variable implies we are trying to reassign the variable twice. +// This case, I have used let instead in order to allow the variable to be reused. \ No newline at end of file diff --git a/Sprint-2a/2-mandatory-errors/2.js b/Sprint-2a/2-mandatory-errors/2.js new file mode 100644 index 000000000..fa92713c9 --- /dev/null +++ b/Sprint-2a/2-mandatory-errors/2.js @@ -0,0 +1,9 @@ +// Currently trying to print the string "I was born in Bolton" but it isn't working... +// what's the error ? + +const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + + +// The error is ReferenceError: Cannot access 'cityOfBirth' before initialization +// Answer: I switched the order by declaring the const first before calling /printing diff --git a/Sprint-2a/2-mandatory-errors/3.js b/Sprint-2a/2-mandatory-errors/3.js new file mode 100644 index 000000000..13addc153 --- /dev/null +++ b/Sprint-2a/2-mandatory-errors/3.js @@ -0,0 +1,16 @@ +const cardNumber = 4533787178994213; +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 +// 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 + +//Prediction was the code would run successfully without error although with the wrong results due to absence of syntax errors in the file +// Error returned: TypeError: cardNumber.slice is not a function +// Lesson learnt here; slice method is only available for strings or arrays not numbers +// So converted the cardNumber into a string first diff --git a/Sprint-2a/2-mandatory-errors/4.js b/Sprint-2a/2-mandatory-errors/4.js new file mode 100644 index 000000000..b9c0d88d3 --- /dev/null +++ b/Sprint-2a/2-mandatory-errors/4.js @@ -0,0 +1,7 @@ +const twelveHourClockTime = "8:53pm"; +const twentyFourHourClockTime = "20:53"; +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); + +// Error - SyntaxError: Invalid or unexpected token +// lesson learnt - variables cannot start with a number \ No newline at end of file diff --git a/Sprint-2a/3-mandatory-interpret/1-percentage-change.js b/Sprint-2a/3-mandatory-interpret/1-percentage-change.js new file mode 100644 index 000000000..a173aa8b5 --- /dev/null +++ b/Sprint-2a/3-mandatory-interpret/1-percentage-change.js @@ -0,0 +1,30 @@ +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; + +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 +// 2 +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ",")); +// 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) error = SyntaxError: missing ) after argument list +// (",", ",")); - added , between quoted values +// 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? +// e) cleans the amount format by removing characters such as , and leaving only number \ No newline at end of file diff --git a/Sprint-2a/3-mandatory-interpret/2-time-format.js b/Sprint-2a/3-mandatory-interpret/2-time-format.js new file mode 100644 index 000000000..a85b69b25 --- /dev/null +++ b/Sprint-2a/3-mandatory-interpret/2-time-format.js @@ -0,0 +1,31 @@ +const movieLength = -10; // length of movie in seconds + +const remainingSeconds = movieLength % 60; +const totalMinutes = (movieLength - remainingSeconds) / 60; + +const remainingMinutes = totalMinutes % 60; +const totalHours = (totalMinutes - remainingMinutes) / 60; + +const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +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 variables + +// b) How many function calls are there? +// 2 +// c) Using documentation, explain what the expression movieLength % 60 represents +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// It means 60 % remainder of Movie length + +// d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// totalMinutes is assigned a value of the result from (movieLength - remainingSeconds) / 60; + +// e) What do you think the variable result represents? Can you think of a better name for this variable? +// I think its the total movie length with a timer. Based on research it appears to be template literal variable as it mixes static text with dynamic data. Sorry I don't fully understand this bit yet + +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//any number greater than zero returns a valid positive hour, minute or seconds result. Changing the length to 0 returns 0:0:0. Any negative length returns negative values + diff --git a/Sprint-2a/3-mandatory-interpret/3-to-pounds.js b/Sprint-2a/3-mandatory-interpret/3-to-pounds.js new file mode 100644 index 000000000..d69518f17 --- /dev/null +++ b/Sprint-2a/3-mandatory-interpret/3-to-pounds.js @@ -0,0 +1,26 @@ +const penceString = "399p"; + +const penceStringWithoutTrailingP = penceString.substring( + 0, penceString.length - 1); + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + + +const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + +console.log(`£${pounds}.${pence}`); + +// This program takes a string representing a price in pence +// The program then builds up a string representing the price in pounds + +// 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): sets penceStringWithoutTRailingP = 399.0 - penceString -1 = 39 +// 3. const paddedPenceNumberString - ensures the figure is 3 characters to taking us back to 399 +// 4 const pounds - removes 2 characters from the amount = 3 +// 5. const pence - adds the amount by 2 characters taking us back to either 39 or 99 +// 6. console displays the figures in pounds and pence = 3.99 \ No newline at end of file diff --git a/Sprint-2a/4-stretch-explore/chrome.md b/Sprint-2a/4-stretch-explore/chrome.md new file mode 100644 index 000000000..962580b82 --- /dev/null +++ b/Sprint-2a/4-stretch-explore/chrome.md @@ -0,0 +1,15 @@ +Open a new window in Chrome, right click an empty space on the page, select **Inspect** from the dropdown, then locate the **Console** tab. + +Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). +Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. + +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? + +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? +What is the return value of `prompt`? diff --git a/Sprint-2a/4-stretch-explore/objects.md b/Sprint-2a/4-stretch-explore/objects.md new file mode 100644 index 000000000..0216dee56 --- /dev/null +++ b/Sprint-2a/4-stretch-explore/objects.md @@ -0,0 +1,16 @@ +## Objects + +In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. + +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? + +Try also entering `typeof console` + +Answer the following questions: + +What does `console` store? +What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? diff --git a/Sprint-2a/README.md b/Sprint-2a/README.md new file mode 100644 index 000000000..a47afc540 --- /dev/null +++ b/Sprint-2a/README.md @@ -0,0 +1,35 @@ +# 🧭 Guide to Sprint 2 exercises + +> https://curriculum.codeyourfuture.io/itp/javascript-fundamentals/sprints/2/prep/ + +> [!TIP] +> You should always do the prep work _before_ attempting the coursework. +> The prep shows you _how_ to do the coursework. +> There is often a step by step video you can code along with too. +> Do the prep. + +This README will guide you through the different sections for this week. + +## 1 Exercises + +In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 2 Errors + +In this section, you'll need to go to each file in `errors` directory and run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. + +## 3 Interpret + +In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. + +You must use documentation to make sense of anything unfamiliar - learning how to look things up this way is a fundamental part of being a developer! + +You can also use `console.log` to check the value of different variables in the code. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 4 Explore - Stretch 💪 + +This stretch activity will get you to start exploring new concepts and environments by yourself. It will do so by prompting you to reflect on some questions.