diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..fabe658ff1 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -2,5 +2,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 + +//line 3 is updating the value of the variable count to 'count+1 \ 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 47561f6175..5a7c183c5b 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 = ``; +let 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-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28e..8ceed31534 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); +const lastSlashIndex = filePath.lastIndexOf("/");// result gives the index value until the last '/' +const base = filePath.slice(lastSlashIndex + 1); // result will slice the 'filePath value with the number output from lastIndexOf 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) ; +const lastDotIndex=base.lastIndexOf("."); +const ext = base.slice(lastDotIndex); + +console.log(dir); +console.log(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 292f83aabb..e6da4ef993 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/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; - +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 + +//(maximum - minimum + 1) this will give a result of 99+1 +//Math.random() will give a random number lesser 1 +//Math.floor(Math.random() * (maximum - minimum + 1)) this expression will remove the decimal point and round down the value +//+ minimum this is the last part to be evaluated, it will add 1 to the first part + +//Answer: num will give a random value from 0 to 100 \ 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 cf6c5039f7..8f8e5c3c3c 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?*/ + +//I have commented the 2 lines since it is not meant for the computer to run \ 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 7a43cbea76..0364a91f25 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,5 @@ const age = 33; age = age + 1; + +// Age was assigned as a constant variable, it can't be reassigned. We can change it to let, let age=33; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..c7cb3470f0 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,5 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +//Because the cityOfBirth was not defined yet before printing it, there's nothing to print \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884db..0595961861 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -5,5 +5,12 @@ const last4Digits = cardNumber.slice(-4); // 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? +// Consider: Why does it give this error? Is this what I predicted? If not, what's different?-It gives an error because .slice is a string method and not for a number. + + + + // Then try updating the expression last4Digits is assigned to, in order to get the correct value + +// const cardNumber = 4533787178994213; +// const last4Digits = cardNumber.slice(-4); \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d1..a8b51a6aec 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,7 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +//a variable can't start with a number + +// const twelveHourClockTime = "20:53"; +// const twentyFourHourClockTime = "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 e24ecb8e18..f9f6d66d34 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -13,10 +13,33 @@ 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 + /* + There are 5 function calls, + line 4 carPrice.replaceAll() + line 4 Number() + line 5 priceAfterOneYear.replaceAll() + line 5 Number() + 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? + /* There's a syntax error in line 5, there's no comma in between arguments + it should be priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + */ + + // c) Identify all the lines that are variable reassignment statements + /*There are 2 variable reassignments + line 4, carPrice = Number(carPrice.replaceAll(",", "")); + line 5, priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + // d) Identify all the lines that are variable declarations + /*Variable Declarations are lines 1,2,7 and 8 + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + + //It removes all the commas converting the string to a number so it can be used in the math operations + diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..4507e7ebff 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -13,13 +13,26 @@ console.log(result); // a) How many variable declarations are there in this program? -// b) How many function calls are there? + /* + There are 6 variable declarations + const movieLength + const remainingSeconds + const totalMinutes + const remainingMinutes + const totalHours + const result + */ + +// b) How many function calls are there?-There's 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 + //The expression movieLength%60 represents modulo operator, using %60 helps you extract how many seconds remaining. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? + //totalMinutes represents how many complete minutes fit inside the movie length. -// e) What do you think the variable result represents? Can you think of a better name for this variable? +// e) What do you think the variable result represents? Can you think of a better name for this variable?-the result variable formats the movie time to a string showing hours, minutes and seconds, it can be renamed to formattedTime -// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer-Yes as long as the movieLength value is positive diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69a..84e54b4951 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,19 +1,19 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( - 0, +0, penceString.length - 1 -); +);// will remove 'p' so the answer is 399 -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");//this won't do much for the given since it's already 3 numbers const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 -); +);// this code will remove the first digit, const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); + .padEnd(2, "0");//this code will add '0' in front of the pence if it has 1 number console.log(`£${pounds}.${pence}`); @@ -25,3 +25,39 @@ 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, + penceString.length - 1 +); +:remove the trailing 'p' from the string.substring(0, penceString.length - 1) takes characters from index 0 up to (but not including) the last character. +*/ + + +/* +3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +:Ensure the string has at least 3 characters by adding leading zeros if necessary. +*/ + + + +/* +4. const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); +: This will extract the first digit, the pounds portion +*/ + +/* +5.const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0");//this code will add '0' in front of the pence if it has 1 number +: this code extracts the pence portion, makes sure the it has exactly 2 digits +*/ + +/* +6. console.log(`£${pounds}.${pence}`); +:prints the new formatted value of pounds and pence +*/ \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..e5c19f1897 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +//Function capitalise will make thes tring input first letter to Big case // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +10,8 @@ function capitalise(str) { return str; } -// =============> write your explanation here +// =============> write your explanation here : str has been declared in the function parameter and inside the function // =============> write your new code here +function capitalise(str) { + return str `${str[0].toUpperCase()}${str.slice(1)}`; +} \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..317b4d1141 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,8 @@ // Predict and explain first... +//the code was supposed to convert a number input into percentage // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here: An error will occur because "decimalNumber" has been redeclared inside the function // Try playing computer with the example to work out what is going on @@ -15,6 +16,11 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +//a variable can only be declared once, and const value cannot be replaced // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + return `${decimalNumber * 100}%`; +} +console.log(decimalNumber); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..fedbb54e7e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,22 @@ // Predict and explain first BEFORE you run any code... - +//this will not run because variables num are not declared // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> write your prediction of the error here: num is not defined function square(3) { return num * num; } -// =============> write the error message here +// =============> write the error message here: " Illegal return statement" -// =============> explain this error message here +// =============> explain this error message here: The return is outside the function, Javascript is treating is at function since the parameter is a value and not an identifier that's why the return inside was not treated as a function // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..8df5a1d092 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: the code block will display "The result of multiplying 10 and 32 is 320 " function multiply(a, b) { console.log(a * b); @@ -8,7 +8,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here: my prediction is wrong as the first console.log displays nothing as it doesn't have values yet and console.log doesn't hold a value it only displays // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return a * b; +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..14324991ea 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: this will result to an error because return doesn't have anything to perform function sum(a, b) { return; @@ -8,6 +8,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here: Operation a+b is after the return code, after return the function ends immediately + // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..220b99720e 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here: it will always display that the last number of all input is 3 const num = 103; @@ -16,9 +16,17 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`); // Now run the code and compare the output to your prediction // =============> write the output here // Explain why the output is the way it is -// =============> write your explanation here +// =============> write your explanation here :num is declared as const, so num will always be 103 and last digit will always be 3 // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(num) { + return num.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); // 2 +console.log(`The last digit of 105 is ${getLastDigit(105)}`); // 5 +console.log(`The last digit of 806 is ${getLastDigit(806)}`); // 6 + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..6a1746a756 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,6 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + const bmi = weight / (height * height); // divide weight by height squared + return bmi.toFixed(1); // round to 1 decimal place +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..a89fac016f 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,6 @@ // You will need to come up with an appropriate name for the function // Use the MDN string documentation to help you find a solution // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase +function toUpperSnakeCase(str) { + return str.toUpperCase().replace(/ /g, '_'); //.replace(/ /g, '_') → replaces all spaces with underscores (_). +} diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..5e47eb1267 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,14 @@ // You will need to declare a function called toPounds with an appropriately named parameter. // You should call this function a number of times to check it works for different inputs +// Converts kilograms to pounds +function toPounds(kg) { + const pounds = kg * 2.20462; // 1 kg ≈ 2.20462 lbs + return pounds; +} + +// Testing the function with different inputs +console.log(toPounds(1)); // 2.20462 +console.log(toPounds(5)); // 11.0231 +console.log(toPounds(10)); // 22.0462 +console.log(toPounds(70)); // 154.3234 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..3ca16e42a5 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,21 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here: it has been called 3 times, hours, minutes and seconds // Call formatTimeDisplay with an input of 61, now answer the following: // b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here +// =============> write your answer here: 0 hours // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here: 00 0padded to 2digits // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> write your answer here: 1 — the last call pads remainingSeconds, which is 1. // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> write your answer here: "01" — 1 is converted to a string and padded to two digits. + +//"00:01:01" +