diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..3cdf69b36 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here - +/* +The variable 'str' must be defined before it be used in a function. As a result, an error of Identifier will occur. +*/ // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +12,13 @@ function capitalise(str) { } // =============> write your explanation here +// The error is occurring because the parameter 'str' is being redeclared with 'let' inside the function. +// This causes a conflict and results in an error. To fix this, we can simply remove the 'let' keyword +// when reassigning the value to 'str'. + // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} +//console.log(capitalise("Texto")) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..837568664 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,29 @@ // Predict and explain first... - +/* +The console is returning decimalNumber, which is the original number instead of the result of the function. +The variable decimalNumber was declared in the function parameters. +*/ // Why will an error occur when this program runs? // =============> write your prediction here - +// An error of variable, because the variable decimalNumber must was declared inside of the function. // 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; } console.log(decimalNumber); // =============> write your explanation here - +// The variable decimalNumber was declared inside the function. Also, the console.log was trying to call the variable decimalNumber. // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} +console.log(convertToPercentage(0.5)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..ebb9a9f67 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,29 @@ // Predict and explain first BEFORE you run any code... - -// this function should square any number but instead we're going to get an error +/* +The function suppose to elevate a number to the square. However, the parameter of the function, +the declaration of the variable 'num' and the execution is wrong. +*/ // =============> write your prediction of the error here +// Parameters error line 11, 3. function square(3) { return num * num; } // =============> write the error message here - +/* +SyntaxError: Unexpected number +function square(3) + ^ +*/ // =============> explain this error message here - +// The parameter of the function expects a name instead of a number. // Finally, correct the code to fix the problem // =============> write your new code here - - +function square(num){ + return num * num; +} +//console.log(square(3)) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..bc9757dea 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,7 +1,7 @@ // Predict and explain first... - +// The function suppose to multiply two numbers. The execution of the function includes a console.log call instead of a return. // =============> write your prediction here - +// The console is going to present two different messages. No one with the expected result. function multiply(a, b) { console.log(a * b); } @@ -9,6 +9,10 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here - +// The console shows the result of the multiplication 10 x 32 = 320, and the text message with the result 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 ad 32 is ${multiply(10,32)}`); \ 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..db6a4ed2b 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,7 @@ // Predict and explain first... // =============> write your prediction here - +// This is a function to sum two numbers, 10 and 32, and show a message with the result. +// Will return undefined error because the 'return' of the function is empty. function sum(a, b) { return; a + b; @@ -9,5 +10,10 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The return of the execution code in the function must be the operation 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)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..3d26ae2ab 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,8 +1,9 @@ // Predict and explain first... - +// The function must show the last digit a number, starting with 103, that is to say 3. +// Then evaluate other numbers; 42 result 2, 105 result 5, and 806 result 6. // Predict the output of the following code: // =============> Write your prediction here - +// The console messages will show undefined results. const num = 103; function getLastDigit() { @@ -15,10 +16,24 @@ 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 +/* +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 +// The function take the the number 103 from the variable num in the execution, getting the result 3. +// However, the parameter of the function was not stablish with the variable. As a result, the return never changed. // =============> write your explanation here +// The parameter of the function was not stablish with the variable. As a result, the return never changed. // 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 32 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..4ca2551d2 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file +return parseFloat((weight/(height*height)).toFixed(1)); +//return Math.round((weight/(height*height))*10)/10; //This is other option to fix the number of decimals to 1. +} +console.log(`According your values of weight and height, your BMI is: ${calculateBMI(90,1.73)}`); \ 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..0382eacc9 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 upperSnakeCase(text){ + return (text).toUpperCase().replaceAll(' ','_'); +} +console.log(upperSnakeCase('code your future')); \ 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 6265a1a70..c4e5fe6b6 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,20 @@ // 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(number){ + if(number.toString().slice(-1) !== 'p'){ + number = (number.toString() + 'p'); + } + const penceString = number; + 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.assert(toPounds('399')==='£3.99','Test fail with; 399'); +console.assert(toPounds('399p')==='£3.99','Test fail with; 399p'); +console.assert(toPounds(40)==='£0.40','Test fail with; 40'); +console.assert(toPounds('1')==='£0.01','Test fail with; 1'); +console.assert(toPounds('34947p')==='£349.47','Test fail with; 34947p'); \ 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..2aa0cae4a 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,24 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } -// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit +// You will need to play computer with this example - use the Python Visualizer https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> 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 +// =============> 0 // c) What is the return value of pad is called for the first time? -// =============> 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 +// =============> 1 the last time when pad is called is pad(remainingSeconds) the remainder of 61/60 = 1 calculated in line 6 // 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 assigned is 01, the remainder was 1 and pad function fill blanks with 0 to get 2 chars. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..1e861dac8 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,23 +3,57 @@ // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (!time.includes(':')){ + time = time.slice(0,-2)+':' + time.slice(-2) } - return `${time} am`; + let hours = Number(time.slice(0, time.indexOf(':'))); + let minutes = time.slice(time.indexOf(':') + 1).padStart(2, '0'); + + let period = 'am'; + + if (hours === 0) { + hours = 12; + } else if (hours === 12) { + period = 'pm'; + } else if (hours > 12) { + hours = hours - 12; + period = 'pm'; + } + + const formattedHours = String(hours).padStart(2, '0'); + + return formattedHours + ":" + minutes + " " + period; + } const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +console.assert(currentOutput === targetOutput,`current output1: ${currentOutput}, target output: ${targetOutput}`); + +const currentOutput7 = formatAs12HourClock("00:01"); +const targetOutput7 = "12:01 am"; // midnight 12:01 am +console.assert(currentOutput7 === targetOutput7, `current output7: ${currentOutput7}, target output: ${targetOutput7}`); + +const currentOutput8 = formatAs12HourClock("12:00"); +const targetOutput8 = "12:00 pm"; // Noon 12:00 pm +console.assert(currentOutput8 === targetOutput8, `current output8: ${currentOutput8}, target output: ${targetOutput8}`); + +const currentOutput2 = formatAs12HourClock("23:01"); +const targetOutput2 = "11:01 pm"; +console.assert(currentOutput2 === targetOutput2,`current output2: ${currentOutput2}, target output: ${targetOutput2}`); + +const currentOutput3 = formatAs12HourClock("07:34"); +const targetOutput3 = "07:34 am"; +console.assert(currentOutput3 === targetOutput3,`current output3: ${currentOutput3}, target output: ${targetOutput3}`); + +const currentOutput4 = formatAs12HourClock("1100"); +const targetOutput4 = ("11:00 am"); +console.assert(currentOutput4 === targetOutput4,`current output4: ${currentOutput4}, target output: ${targetOutput4}`); + +const currentOutput5 = formatAs12HourClock("8:24"); +const targetOutput5 = "08:24 am"; +console.assert(currentOutput5 === targetOutput5,`current output5: ${currentOutput5}, target output: ${targetOutput5}`); + +const currentOutput6 = formatAs12HourClock("1:01"); +const targetOutput6 = "01:01 am"; +console.assert(currentOutput6 === targetOutput6,`current output6: ${currentOutput6}, target output: ${targetOutput6}`);