diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..47bb9f618 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,45 @@ // Predict and explain first... // =============> write your prediction here +// 1.*Answer +// We have declared the "str" twice.It is already declared +// in this function's parameter, hence causing the conflict. +// Although "let" allows re-assignment of variables, the issue is redeclaration, +// not reassignment. + // call the function capitalise with a string input +// 2.*Answer +// It throws an error detailed below; +// / home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/tempCodeRunnerFile.js:2 +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// ^ +// SyntaxError: Identifier 'str' has already been declared. + + // interpret the error message and figure out why an error is occurring +// 3.*Answer +// The SyntaxError - Something is invalid that is not following +// Javascript rules. +// The identifier - a name used in the code has a problem and it +// lets m know which one in this case "str". It also let's me know +// what the problem is in this case "str" has already been declared. + +//Fix this code: +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } + +// =============> write your explanation here +//I have re-assigned the "str" variable and it now runs with no errors. +// I also checked with console.log to see the output. + +// =============> write your new code here function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +console.log(capitalise("justice")); + -// =============> write your explanation here -// =============> write your new code here diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..fc73766a5 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,36 @@ // Predict and explain first... - // Why will an error occur when this program runs? // =============> write your prediction here +// 1.*Answer +// We have declared "decimalNumber" twice, which is not allowed. +// It is already declared as a function parameter, causing a conflict. +// console.log(decimalNumber) results in an error because it is outside +// the function scope.It should call console.log(convertToPercentage(decimalNumber)); -// Try playing computer with the example to work out what is going on - -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; - - return percentage; -} +//Try playing computer with the example to work out what is going on +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; -console.log(decimalNumber); +// return percentage; +// } +// console.log(decimalNumber); // =============> write your explanation here +// 2.*Answer +// It throws an error detailed below; +// /home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/1.js:12 +// const decimalNumber = 0.5; +// ^ +// SyntaxError: Identifier 'decimalNumber' has already been declared + +// Removed the const declaration and returned the percentage calculation +// using a template literal to append the percent symbol. // 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 aad57f7cf..38f22399d 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,34 @@ // Predict and explain first BEFORE you run any code... -// this function should square any number but instead we're going to get an error +// This function should square any number but instead we're going to get an error // =============> write your prediction of the error here -function square(3) { - return num * num; -} + +// function square(3) { +// return num * num; +// } // =============> write the error message here +// /home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/2.js:8 +// function square(3) { +// ^ +// The SyntaxError: Unexpected number // =============> explain this error message here +// This occurs because the number "3" is used as a function parameter. +// This is not valid syntax. A function parameter must be a valid variable name (an identifier), +// not a number. JavaScript expects an identifier or no parameter at all. +// That's why it throws a syntax error. +// // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); \ 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 b27511b41..98a3df790 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,30 @@ // Predict and explain first... - +// The first console.log prints 320 +//It throws an error at console.log. // =============> write your prediction here -function multiply(a, b) { - console.log(a * b); -} - -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// function multiply(a, b) { +// console.log(a * b); +// } +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// It return the following output: +// 320 +// The result of multiplying 10 and 32 is undefined + +// why? +//the computer reads the values in the second console.log "multiply(10, 32)" and +//the first console.log only prints the value, it does not store it. +//the second console.log outputs the template literals and undefined +// as it has no value to reach. + // 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)}`); + //prints: The result of multiplying 10 and 32 is 320 \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..bd89836d9 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // =============> write your prediction here -function sum(a, b) { - return; - a + b; -} +// The computer reads the return statement and stops executing the function. +// So the function stops running before it reaches a + b. +// Because of that, nothing is returned, +// and the console.log prints the text and the values are undefined. -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// current output:The sum of 10 and 32 is undefined -// =============> write your explanation here +// Fix the code to make it work: + // function sum(a, b) { + // return a + b; + // } + // console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +//To make it work, I need to return a + b. // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..6deb5b4cd 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,54 @@ // Predict and explain first... +// The function does not take in any parameters, therefore it +// refers to the global const variable = 103 +// So every console.log will print 3 every time. This function is not +// re-useable as it stands. // Predict the output of the following code: // =============> Write your prediction here +//output is 3 for every console.log. -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// function getLastDigit() { +// return num.toString().slice(-1); +// } -console.log(`The last digit of 42 is ${getLastDigit(42)}`); -console.log(`The last digit of 105 is ${getLastDigit(105)}`); -console.log(`The last digit of 806 is ${getLastDigit(806)}`); +// console.log(`The last digit of 42 is ${getLastDigit(42)}`); +// console.log(`The last digit of 105 is ${getLastDigit(105)}`); +// 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 +// "/home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/2-mandatory-debug/2.js" +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 // Explain why the output is the way it is // =============> write your explanation here +// The function refers to the global const variable = 103 +// So every console.log returns 3 every time. + // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; + +function getLastDigit(num) { + return num.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); +// prints: The last digit of 42 is 2 +console.log(`The last digit of 105 is ${getLastDigit(105)}`); +//prints: The last digit of 105 is 5 +console.log(`The last digit of 806 is ${getLastDigit(806)}`); +//prints: The last digit of 806 is 6 + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem +// now the code works correctly + +// I declared "num" as a parameter of the function, +// so the function now uses the value passed into it +// instead of the global variable "num". diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..9157a435d 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -13,7 +13,15 @@ // Given someone's weight in kg and height in metres // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place +// bmi = weight / height**2). + // return the BMI of someone based off their weight and height function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + return (weight/(Math.pow(height, 2))).toFixed(1); + +} +console.log(calculateBMI(100, 1.6)); //print 39.1 +console.log(calculateBMI(55, 1.6)); //print 21.5 + +// I used Math.pow to raise the height to the power of 2 and .toFixed +//for the number of decimal places. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..f8e88850c 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 changeToUpperCaseSnake(string) { + return string.toUpperCase().replaceAll(" ", "_"); +} + +console.log(changeToUpperCaseSnake("hello there")); +console.log(changeToUpperCaseSnake("lord of the rings")); +console.log(changeToUpperCaseSnake("learning to love Javascript")); + +// 1.Capitalise = .toUpperCase +// 2.Find blank space = .replaceAll ("", ...) +// 3.replace with _ = ,replaceAll (..., "_") diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..007f065bd 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,37 @@ // 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 + +//Rewrite this code + // 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}`); + + //Updated code to make it re-useable: + + function toPounds(fromPenceString) { + const penceString = fromPenceString.substring(0, fromPenceString.length -1); + const paddedString = penceString.padStart(3, "0"); + const pounds = paddedString.substring(0, paddedString.length -2); + const pence = paddedString.substring(paddedString.length -2).padEnd(2, "0"); + return `£${pounds}.${pence}`; + } + console.log(toPounds("399p")); // £3.99 + console.log(toPounds("45p")); // £0.45 + console.log(toPounds("1295p")); // £12.95 + console.log(toPounds("5p")); // £0.05 \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..ff1d58574 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,8 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); + // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions @@ -18,17 +20,28 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// It will be called 3 times. 1 for hours, 1 for minutes, 1 for seconds. +// return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; -// Call formatTimeDisplay with an input of 61, now answer the following: +// Call formatTimeDisplay with an input of 61, now answer the following: +// [Running] node "/home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/4-mandatory-interpret/time-format.js" +// 00:01:01 // b) What is the value assigned to num when pad is called for the first time? // =============> write your answer here +// The value is 0. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value is a string "00". // 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 - -// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer +// The value is 1. When the input is 61, It is evaluating number of remaining seconds, +// which equals 1. +// it is evaluating from left to right. +// +// 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 +// The return value is a string "01" because also because .padStart has been used to ensure that the +// value displays as a minimum of 2 digits in this case if less than 2 digits add a leading "0". \ No newline at end of file