diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..fe86965985 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,22 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here /* there will be a syntax error because the variable str is being declared twice in the same scope. The first declaration is in the function parameter, and the second declaration is inside the function body. This will cause a conflict and result in an error. */ // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { +/*function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; + } +capitalise("Tobias");*/ -// =============> write your explanation here +// =============> write your explanation here /* when the function `capitalise` is called with the argument "Tobias", it tries to declare a new variable `str` inside the function body using `let`. However, `str` is already declared as a parameter of the function. In JavaScript, you cannot declare a variable with the same name in the same scope, which leads to a syntax error and from the error message thus /home/tobi/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/0.js:8 + /*let str = `${str[0].toUpperCase()}${str.slice(1)}`;SyntaxError: Identifier 'str' has already been declared it is pointing at line 8 as where the syntax error occurred so to fix the error the value on line 8 will need to be reassigned and not redeclared */ // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; + +} +console.log(capitalise("Tobias")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..fc09e548b3 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,38 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here. /* Error will occur when this program is run because a variable already declared inside the function parameter , was redeclared inside the function body. Also the console.log function is calling the variable decimalNumber which is not defined in the global scope. */ // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; +/*function convertToPercentage(decimalNumber) { + decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } +console.log(decimalNumber); */ + +// =============> write your explanation /* the first error SyntaxError pointed at line 9 as to where the error occurred and explained that the identifier-decimalNumber has already been declared. The second error ReferenceError pointed at line 15 as to where the error occurred and explained that the identifier-decimalNumber is not defined in the global scope. */ +/* /home/tobi/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/1.js:9 + const decimalNumber = 0.5; + ^ + +SyntaxError: Identifier 'decimalNumber' has already been declared*/ +/* /home/tobi/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/1.js:15 console.log(decimalNumber); + ^ -// =============> write your explanation here +ReferenceError: decimalNumber is not defined*/ // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + // removed the variable reassignment so that function call will work with any argument passed to it. + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(0.75)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..2cbbc827db 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,24 @@ -// Predict and explain first BEFORE you run any code... +// Predict and explain first BEFORE you run any code...we are going to get a SyntaxError because the function parameter is a number and not a variable name. The function parameter should be a variable name that can be used inside the function body. // 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 // SyntaxError: Unexpected number. -function square(3) { +/*function square(3) { return num * num; -} +}*/ -// =============> write the error message here +// =============> write the error message here /* /home/tobi/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/2.js:8 function square(3) { SyntaxError: Unexpected number*/ -// =============> explain this error message here +// =============> explain this error message here/* this error is saying that something is wrong with the function parameter because it is a number and not a variable name. The function parameter should be a variable name that can be used inside the function body. the SyntaxError denote that the rule is violated, and pointed out that the number is unexpected at line 8 */ // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num*num; +} +square(); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..b0628787a2 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,19 @@ // Predict and explain first... -// =============> write your prediction here - -function multiply(a, b) { +// =============> write your prediction here// The code will print 320 and undefine secondly. +/*function multiply(a, b) { console.log(a * b); } -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/ -// =============> write your explanation here +// =============> write your explanation here. /* The function multiply is called inside the console.log statement, but it does not return any value. Instead, it only logs the result of multiplying a and b to the console. Therefore, when the console.log statement is executed, it will print "The result of multiplying 10 and 32 is undefined" because the multiply function does not return anything. The first console.log inside the multiply function will print 320 to the console, but the second console.log will print undefined. */ // 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..3f6cc60996 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,18 @@ // Predict and explain first... -// =============> write your prediction here - -function sum(a, b) { +// =============> write your prediction here // will print undefined. +/*function sum(a, b) { return; a + b; } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/ + +// =============> write your explanation here.// The code will print undefined because the function sum does not return any value. Instead, it has a return statement with no value, because the semicolon ended the return statement which means it will return undefined by default. The second line of the function, a + b, is never executed because the return statement ends the function execution before it can be reached. -// =============> write your explanation here // 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..431989efed 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,9 +1,9 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here./* The output will be 3 because the function getLastDigit is not using the argument passed to it, instead it is using the global variable num which is set to 103. Therefore, the last digit of 103 is 3 and that is what will be printed for all three console.log statements.*/ -const num = 103; +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -11,14 +11,24 @@ function getLastDigit() { 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 806 is ${getLastDigit(806)}`);*/ // Now run the code and compare the output to your prediction -// =============> write the output here +// =============> write the output here// The out put is 3 for the three console.log calls as predicted // Explain why the output is the way it is -// =============> write your explanation here +// =============> write your explanation here. /* The reason is because the function getLastDigit is not using the argument passed to it, instead it is using the global variable num which is set to 103. Therefore, the last digit of 103 is 3 and that is what will be printed for all three console.log statements.*/ // 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)}`); +console.log(`The last digit of 105 is ${getLastDigit(105)}`); +console.log(`The last digit of 806 is ${getLastDigit(806)}`); + // This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +// Explain why getLastDigit is not working properly - correct the problem// The variable num was a global variable , that is is why it was not working becasue it was declared outside of the function and was fixed by declaring it inside the function as a parameter . diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..7d7491483f 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + return`The BMI of someone with a weight of ${weight}kg and a height of ${height}m is ${(weight / (height * height)).toFixed(1)}`; // return the BMI of someone based off their weight and height -} \ No newline at end of file +} +console.log(calculateBMI(70, 1.73)); // should return 23.4 \ 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 5b0ef77ad9..c7cf3931c9 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,8 @@ // 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 convertToUpperSnakeCase(inputString) { + return inputString.replace(/\s+/g,'_').toUpperCase(); +} +console.log(convertToUpperSnakeCase("there is fire on the montain")); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..fc13d69975 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,24 @@ // 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 +function toPounds(penceString) { + + +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"); + +return(`£${pounds}.${pence}`); +} +console.log(toPounds("30p")); \ 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 17127bc01e..e82c6d398c 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here// three times // 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. // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here// "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 +// =============> write your answer here// 1 // e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> write your answer here// "01" diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..48f20d7150 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -5,8 +5,14 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2,"0")}:00 pm`; } + if (hours===12){ + return `${hours}:00 pm`; +} + if (hours===0){ + return `${12}:00 am`; + } return `${time} am`; } @@ -23,3 +29,26 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +// testing for noon @ "12:00" +const currentOutput3 = formatAs12HourClock("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); +//Testing for Midnight +const currentOutput4 = formatAs12HourClock("00:00"); +const targetOutput4 = "12:00 am"; +console.assert( +currentOutput4 === targetOutput4, +`current output: ${currentOutput4}, target output: ${targetOutput4}` +); +// Testing for leading zeros + +const currentOutput5 = formatAs12HourClock("13:00"); +const targetOutput5 = "01:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); +