diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..b89c11267 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 +// The instruction wants to define a function with the input (str) // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// the error is in the use of let str. (str) is already an input and you can't use it again. + -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} // =============> write your explanation here +//In order to fix the problem we need to declare let with capitalise instead of str. +// Because if we use the parameter (str), it creates a naming conflict in the same scope. + + // =============> write your new code here + +function capitlise(str) { + let capitalise =`${str[0].toUpperCase()}${str.slice(1)}`; + return capitalise; +} +console.log(capitlise("good")); \ 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 f2d56151f..3fcb068e1 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,10 +1,11 @@ // Predict and explain first... - +//I think it will create a syntax error.Since decimal number is inside the funtion while running the function. // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here synax error such us decimal number is not defined // Try playing computer with the example to work out what is going on + function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -15,6 +16,21 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// we declared a function convertToPercentage assigned with decimalNumber +//as a parameter then we use const to declare a new variable using the +// parameter's name which is wrong.So here again decimalNumber was either a parameter +// or declared in wrong way inside the function. We notice that console.log is placed outside the function +// and in order to return its value, it will show reference error. You may ask why? Well, simply, because +// there is no such global thing defined outside the function. // 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..4b5612987 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... +//I think we should define number first before returning it. // 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. +//number undefined. + function square(3) { return num * num; } // =============> write the error message here - +//Unexpected number // =============> explain this error message here - +// It is not allowed to use numbers in a variable parameter. // Finally, correct the code to fix the problem // =============> write your new code here +function square(number) { + const result = number * number; + return result; +} + +console.log(square(3)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..a77640184 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... - +// It will print the result as undefined. // =============> write your prediction here +//undefined because there is no return of the value. function multiply(a, b) { console.log(a * b); @@ -9,6 +10,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// when the code run, it will show "the result of multiplying 10 and 32 is undefined because +// // what console.log does is that it prints the result of a*b but it doesn't return any value" // 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 37cedfbcf..f84deabd0 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +// the result would be: the sum of 10 and 32 is undefined. function sum(a, b) { return; a + b; @@ -9,5 +9,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// the function is defined with the name "sum" there is no return for anything. And if we want to print the result +// it would be: the sum of 10 and 32 is undefined and what we are actually looking for is 42. // 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..799c21208 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,12 +1,16 @@ // Predict and explain first... - -// Predict the output of the following code: +//I think the result would be 3 for 42, 105 and 806 because cost num is declared outside the function +// in the global scope. And this means any input would take the result of the global scope. +// Predict the output of the following code:3 // =============> Write your prediction here +// the result would be 3 for 42, 105 and 806. Because cost num =103 is defined outside the function. const num = 103; -function getLastDigit() { +function getLastDigit()) { return num.toString().slice(-1); + + } console.log(`The last digit of 42 is ${getLastDigit(42)}`); @@ -14,11 +18,22 @@ 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 -// Explain why the output is the way it is +// =============> write the output here: the last digit of 42, 105 and 806 is 3 +// Explain why the output is the way it is. Because when we declared the variable const num =103 it is outside the function(global variable) +// so everytime we call the function, it will use the 103 as a reference. // =============> write your explanation here +// The use of global variable outside the function doesn't accept any arguments. It always refers back to 103 and +// return the last digit which is 3 // Finally, correct the code to fix the problem // =============> write your new code here +function getTheLastDigit(number){ + return number.toString().slice(-1); + +} +console.log(`The last digit of 42 is ${getTheLastDigit(42)}`); +console.log(`The last digit of 105 is ${getTheLastDigit(105)}`); +console.log(`The last digit of 806 is ${getTheLastDigit(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..4f6e150a5 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,8 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + const square =weight/ (height*height); + return square.toFixed(1); // return the BMI of someone based off their weight and height -} \ No newline at end of file +} +console.log(calculateBMI(55, 1.65)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..f44340b35 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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(input){ + return input + .trim() + .toUpperCase() + .replace(/\s+/g, '_'); +} +console.log(ToUpperSnakeCase("lord of the rings")); + diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..126077581 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,21 @@ // 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 numericString = penceString.substring(0, penceString.length - 1); + + const padded = numericString.padStart(3, "0"); + + const pounds = padded.slice(0, -2); + const pence = padded.slice(-2); + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); +console.log(toPounds("7p")); +console.log(toPounds("50p")); +console.log(toPounds("120p")); +console.log(toPounds("559P")); \ 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..61730ae46 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,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 : 3 times pad will be called. For HH , MM and SS. // 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 : the first time pad is called is with totalhours and it is 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: it is 1 for the remainigseconds. So we used the 61%60 =1 So remaining seconds 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. \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..14fb094ec 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,10 +4,17 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + if (hours === 0) { + return `12:${minutes} am`; + } + if (hours === 12) { + return `12:${minutes} pm`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; } - return `${time} am`; + return `${String(hours).padStart(2, "0")}:${minutes} am`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +30,6 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +console.assert(formatAs12HourClock("13:00") === "01:00 pm"); +console.assert(formatAs12HourClock("00:00") === "12:00 am"); +console.assert(formatAs12HourClock("17:30")=== "05:30 pm"); \ No newline at end of file