diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..3d6c381c1 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,16 @@ // Predict and explain first... -// =============> write your prediction here -// call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring + +//function capitalise(str) { //function already declares a parameter named str. +//let str = `${str[0].toUpperCase()}${str.slice(1)}`; //to declare another variable called str using let, which is not allowed +// return str; +//} + + +// here is the new code function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; +let result = `${str[0].toUpperCase()}${str.slice(1)}`; //Taking the first character of the string and converts it to uppercase and rest of the string, starting from index 1. + return result; } -// =============> 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..68c9fd680 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,10 +1,13 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here + +//variable decimalNumber is already declared, we are trying to declare again +// console.log will throw an error since decimal no is not declared outside 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}%`; @@ -12,9 +15,19 @@ function convertToPercentage(decimalNumber) { return percentage; } -console.log(decimalNumber); +console.log(decimalNumber);*/ // =============> write your explanation here +// function call is required and value for the variable 'decimalNumber' should be passed through 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..a9863a842 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,25 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// Variable should be declared as "num" and later value should be passed to "num" > write your prediction of the error here -function square(3) { +/*function square(3) { return num * num; -} +}*/ -// =============> write the error message here +// SyntaxError: Unexpected number > write the error message here -// =============> explain this error message here +// can’t put a number (3) as a parameter name. +//Function parameters must be identifiers (like num, x, or value), not literal values. +//returning num * num, but num is never defined> explain this error message here // 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..c61b7e9e0 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 +// console.log is used to print the output, but here it is used in the function call, which is not the correct approach> write your prediction here -function multiply(a, b) { +/*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 +// We need to return the value instead of just logging it inside the function.> write your explanation here // 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..971c8201a 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,19 @@ // Predict and explain first... -// =============> write your prediction here +// with return we should give some value that comes in the output> write your prediction here -function sum(a, b) { +/*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 +// a+b should be used after return in line 5> 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 57d3f5dc3..a38eb8348 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 +// console.log should be return only 103> Write your prediction here -const num = 103; +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -11,14 +11,28 @@ 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 +/*The last digit of 42 is 3 +The last digit of 105 is 3 +The last digit of 806 is 3> write the output here*/ // Explain why the output is the way it is -// =============> write your explanation here +// because we have provided a constant value 103 to num> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +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: yes +/* here is the new output +The last digit of 42 is 2 +The last digit of 105 is 5 +The last digit of 806 is 6*/ +// now I have defined a function that takes one argument, num. also removed const funtion diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..6ad025443 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 based off their weight and height -} \ No newline at end of file + let bmi = weight / (height * height); + return bmi.toFixed(1); +} +console.log(calculateBMI(70, 1.68)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..85bb6dc81 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,7 @@ // 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 + +const sentence = "hello there"; + +console.log(sentence.toUpperCase().replace(/ /g, "_")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..1603fb911 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,30 @@ // 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) { + // Remove the trailing 'p' + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); + + // Make sure it's at least 3 digits (e.g., '9' -> '009') + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + // Split into pounds and pence parts + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("9p")); // £0.09 +console.log(toPounds("50p")); // £0.50 +console.log(toPounds("1234p")); // £12.34 +console.log(toPounds("7p")); // £0.07 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..6e7255360 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,7 @@ 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 @@ -17,18 +18,20 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// 3 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 +// when pad function is called 3rd time pad(remainingSeconds) -> remainingSeconds = 1 +// 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 +// when pad function is called 3rd time pad(remainingSeconds) -> remainingSeconds = 1, but after padding it becomes 01 and returns it +// 01 diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..e7eb49cce 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,22 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +// Morning times +console.assert(formatAs12HourClock("01:00") === "01:00 am"); +console.assert(formatAs12HourClock("11:30") === "11:30 am"); + +// Noon +console.assert(formatAs12HourClock("12:00") === "12:00 pm"); + +// Afternoon +console.assert(formatAs12HourClock("13:15") === "01:15 pm"); +console.assert(formatAs12HourClock("15:45") === "03:45 pm"); + +// Evening +console.assert(formatAs12HourClock("23:59") === "11:59 pm"); + +// Midnight +console.assert(formatAs12HourClock("00:00") === "12:00 am"); +console.assert(formatAs12HourClock("00:30") === "12:30 am"); + +console.log(" All tests passed!");