diff --git a/Project-CLI-Treasure-Hunt b/Project-CLI-Treasure-Hunt new file mode 160000 index 000000000..7cc7f5a96 --- /dev/null +++ b/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 7cc7f5a96677e8187c1b64abf73231ac1a1820eb diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..074cbc4f8 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 +// =============> write your prediction here it will captilase the first and second letter of the string because the is calling for 0 , 1 slice. // 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)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +console.log(capitalise("summer")); +// why will an error occur when this program runs? +// because the variable str is being declared twice, once as a parameter and then again inside the function. // =============> write your explanation here // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("summer")); \ 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..07838484b 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,27 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here - it won't work because the variable decimalNumber is being declared twice, once as a parameter and then again inside 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.8)); // =============> write your explanation here - +// previously the code was not working because the variable decimalNumber was being declared twice, once as a parameter and then again inside the function. This caused a conflict and resulted in an error. // 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.8)); \ 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..0290d722c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,19 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here - +// We will get in error beacuse teh variable num is not defined. function square(3) { return num * num; } -// =============> write the error message here +// =============> write the error message here - SyntaxError: Unexpected number -// =============> explain this error message here +// =============> explain this error message here - the number should not be there, it should be a variable name. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..12270017b 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,9 +1,9 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here - the multiply will not return anything because the function does not have a return statement. function multiply(a, b) { - console.log(a * b); + console.log(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); @@ -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..ab78b4b90 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here - the function will not work because we don't have a return statement in the function. function sum(a, b) { return; @@ -8,6 +8,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here = I'vee added a return statement to the function to ensure it returns the sum of the two numbers. // 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..6ae2f6440 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,6 +1,6 @@ -// Predict and explain first... +// Predict and explain first... - const num = 103; is declared outside the function, so it will not be used in the getLastDigit function. -// Predict the output of the following code: +// Predict the output of the following code: - error. // =============> Write your prediction here const num = 103; @@ -14,11 +14,25 @@ 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 - +// 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 -// =============> write your explanation here +// =============> write your explanation here - because const cant be used with other numbers, it is only used with the first number declared. and the function is not taking any parameters, so it will always return the last digit of the first number declared. // 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 +// our function getLastDigit does not accept any parameters, so it always uses the global variable num (which is 103). +const num = 103; + +function getLastDigit(number) { + return number.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)}`); diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..5ed3a15d3 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) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + const bmi = weight / (height * height); + return parseFloat(bmi.toFixed(1)); + // return the BMI of someone based off their weight and height +} +console.log(calculateBMI(80, 1.81)); \ 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..b147f2949 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 toUpperCaseSnake(sentence){ +sentence = sentence.split(' ').join('_').toUpperCase(); +return sentence; +} +console.log(toUpperCaseSnake("Do what you can, with what you have, where you are.")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..c634acc5c 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,18 @@ // 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.slice(0, -1); + const paddedPence = penceStringWithoutTrailingP.padStart(3, "0"); + + const pounds = paddedPence.slice(0, -2); + const pence = paddedPence.slice(-2).padEnd(2, "0"); + + return `£${pounds}.${pence}`; +} + + + +console.log(toPounds("399p")); +console.log(toPounds("99p")); + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..d78f2e274 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,20 @@ 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, once for each of totalHours, remainingMinutes, and remainingSeconds. // 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 value assigned to pad 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" — because pad(0) => "0".padStart(2, "0") => "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 - 1 — because the last call to pad is for remainingSeconds, which 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" — because pad(1) => "1".padStart(2, "0") => "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..2183b5bb0 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,24 +2,28 @@ // 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)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const [hh, mm] = time.split(":"); + let hours = Number(hh); + const minutes = mm; + let suffix = "am"; + + if (hours === 0) { + hours = 12; + } else if (hours === 12) { + suffix = "pm"; + } else if (hours > 12) { + hours -= 12; + suffix = "pm"; } - return `${time} am`; -} -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); + const paddedHours = hours.toString().padStart(2, "0"); + return `${paddedHours}:${minutes} ${suffix}`; +} -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +console.assert(formatAs12HourClock("00:00") === "12:00 am", "Midnight"); +console.assert(formatAs12HourClock("01:30") === "01:30 am", "1:30 AM"); +console.assert(formatAs12HourClock("13:15") === "01:15 pm", "1:15 PM"); +console.assert(formatAs12HourClock("23:59") === "11:59 pm", "11:59 PM"); +console.assert(formatAs12HourClock("09:05") === "09:05 am", "9:05 AM");