diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..29c6cc3c2 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 code gives an error because there is a variable declaration with the same name as the parameter of the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + let newStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return newStr; } -// =============> write your explanation here -// =============> write your new code here + +// =============> write your explanation here: The error is occurring because the variable newStr is declared inside the function capitalise, +// but it is being accessed outside the function. + +// =============> write your new code here: + function capitalise(str) { + let newStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return newStr; + } + let newStr = capitalise("hello"); + console.log(newStr); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..30147aa27 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,28 @@ -// Predict and explain first... +// Predict and explain first: Decimalnumber is already declared as a parameter of the function convertToPercentage, +// so when we try to declare it again inside the function, it will give an error. // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here : The error will occur because we are trying to declare a variable with the same name as the parameter 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); +console.log(convertToPercentage(0.5)); -// =============> write your explanation here +// =============> write your explanation here : I removed the variable variation inside the function and directly calculated the percentage. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> 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..9ace2f4b8 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,23 @@ -// Predict and explain first BEFORE you run any code... +// Predict and explain first BEFORE you run any code: the function is supposed to return the square of the number. // 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: Maybe because we used a number instead of a variable inside the function declaration. -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +// } -// =============> write the error message here +// =============> write the error message here : syntax error: Unexpected number (3) -// =============> explain this error message here +// =============> explain this error message here: we shouldn't use a number as a parametre name of the function. // 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..5f52aa414 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ -// Predict and explain first... +// Predict and explain first: This function is supposed to multiply two numbers and return the result. -// =============> write your prediction here +// =============> write your prediction here : The result will be the multiplication of the two numbers. function multiply(a, b) { console.log(a * b); @@ -8,7 +8,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here : The function is logging the result of the console.log statement +// but it is not returning any value. That's because we are using console.log instead of return in the function. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> 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..f9b50820b 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,17 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: The function will not return the sum of a and b because there is a syntax error. -function sum(a, b) { - return; - 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 +// =============> write your explanation here : return should be on the same line as 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..8fefd5359 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,30 @@ -// Predict and explain first... +// Predict and explain first...: the function is supposed to return the last digit of a number, +// but it won't because it's using a fixed number instead of a variable to get the last digit from. // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here : it will give the same last digits of the same number (3). -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// function getLastDigit() { +// 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)}`); +// 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)}`); // 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 your explanation here +// =============> write the output here : It gave the same last digit for all the numbers, which is 3. +// Explain why the output is the way it is : because the function is using a fixed number (103) . +// =============> write your explanation here: because the function is using a fixed number (103) . // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> 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..ceec0f314 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 (85, 1.851)); \ 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..991431a8f 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 convertToUpperSnakeCase(str){ + return str.toUpperCase().replaceAll(" ", "_"); + } + console.log(convertToUpperSnakeCase("hello there")); + console.log(convertToUpperSnakeCase("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..d6b161c9e 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,26 @@ // 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("43p")); +console.log(toPounds("399p")); +console.log(toPounds("300098p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..1725f629f 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,25 +10,25 @@ 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 // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here : 3 // 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: 01, because it the remaining of 61/60. // 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, it's the remainder of 61/60, and it is padded to 2 digits. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..9af0e86b0 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -1,25 +1,29 @@ // This is the latest solution to the problem from the prep. // Make sure to do the prep before you do the coursework // 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)); + 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`; } +console.log(formatAs12HourClock("00:00")); +console.log(formatAs12HourClock("01:00")); +console.log(formatAs12HourClock("11:00")); +console.log(formatAs12HourClock("12:00")); +console.log(formatAs12HourClock("13:00")); +console.log(formatAs12HourClock("23:00")); -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}` -);