diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..9048e8cd7 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -9,5 +9,17 @@ function capitalise(str) { return str; } +//because str is already existing parameter we can not declare let str again. +// we can rename the str to fix the problem. + + +function capitalise(str) { + let cap_str = `${str[0].toUpperCase()}${str.slice(1)}`; + return cap_str; +} + +console.log(capitalise("khilola")); + + // =============> 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..10380e641 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -15,6 +15,16 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// decimalNumber is already a function parameter, so it's already declared in the function's scope. + + // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(50); \ 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..efb489e6e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,34 @@ // 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 + +// =============> the parameter can not be a number +// Valid JavaScript Identifiers +// Must start with a letter (a-z, A-Z), underscore (_), or dollar sign ($). +// Cannot start with a number (0-9). +// Can contain letters, numbers, underscores, or dollar signs after the first character. +// Cannot be a reserved keyword (e.g., if, else, function, return). + + function square(3) { return num * num; } // =============> write the error message here +// function square(3) {return num * num } // =============> explain this error message here - +// 3 is not approprate parametr name. +// num is not defined, // Finally, correct the code to fix the problem // =============> write your new code here + //function square(num){ + //return num*num ; + //} + + //console.log(square(4)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..f89c4426a 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -12,3 +12,8 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // 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)}`); \ 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..912914af6 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -9,5 +9,13 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +//The return; statement ends the function immediately. +//The next line (a + b;) is never executed because it's after the return and on a new line. // 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..51eb80f96 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -14,11 +14,23 @@ 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 +// =============> write the output here +//every time we run it we will get same answer 3 + // Explain why the output is the way it is // =============> write your explanation here + //it will only use global num=103 + // 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..b706dfc4a 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,16 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + + +function calculateBMI(weight, height) { + const bmi = weight / (height * height); //calculates the BMI + return parseFloat(bmi.toFixed(1)); // to ensure the result is rounded to 1 decimal place. +}// parselFloat converts the result back to a number from string. + + +console.log(calculateBMI(70, 1.73)); + +// + diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..a6612a87f 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // 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 (inputString) { + return inputString.toUpperCase().replaceAll(" ", "_"); +} +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); //"LORD_OF_THE_RINGS" \ 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..d5b79d902 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,25 @@ // 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}`; +} + +// checking with different inputs + +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("7p")); // £0.07 +console.log(toPounds("62p")); // £0.62 +console.log(toPounds("400p")); // £4.00 +console.log(toPounds("3p")); // £0.03 \ 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..bac9c1fe8 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,22 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad is going to be called 3 times. for 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 +// 0 because 61 seconds is less then hours // 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 last call as 61%60= 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 +//"01" because pad(1) - "1".padStart(2,"0")-"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..0c9763cf8 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -5,7 +5,8 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:00 pm`; //This hardcodes :00, losing the original minutes from the time input. + //Even original minutes aren't 00 it will always return :00 } return `${time} am`; } @@ -23,3 +24,24 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// new fixed code +function formatAs12HourClock(time) { + const [hourStr, minute] = time.split(":"); + const hours = Number(hourStr); + + const isPM = hours >= 12; + const displayHour = hours % 12 === 0 ? 12 : hours % 12; + const suffix = isPM ? "pm" : "am"; + + return `${String(displayHour).padStart(2, "0")}:${minute} ${suffix}`; +} +console.assert( + formatAs12HourClock("23:00") === "11:00 pm", +); +console.assert( + formatAs12HourClock("00:00") === "12:00 am", +); +console.assert( + formatAs12HourClock("12:00") === "12:00 pm", +); \ No newline at end of file