diff --git a/Project-CLI-Treasure-Hunt b/Project-CLI-Treasure-Hunt new file mode 160000 index 000000000..6668c42dc --- /dev/null +++ b/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 6668c42dc5412e54026ca0fa8cb5691017ef6350 diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..194714664 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,18 @@ // Predict and explain first... // =============> write your prediction here - +// I gives us a syntax error as str declared before. // 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; -} +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } -// =============> write your explanation here +// =============> write your explanation here : +// as the variable in side the function has the same name as the function variable. // =============> write your new code here +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} +console.log(capitalise("abcd")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..be26e66f2 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,30 @@ // Predict and explain first... - -// Why will an error occur when this program runs? +// error on line 11 +// error on line 17 +// Why will an error occur when this program runs? yes // =============> write your prediction here +// decimalNumber is declared before and can not be declared again. +// console.log(decimalNumber) should be console.log(convertToPercentage) +// Try playing computer with the example to work ot what is going on -// Try playing computer with the example to work out what is going on - -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +// } -console.log(decimalNumber); +//console.log(percentage); // =============> write your explanation here +// decimalNumber is declared before and can not be declared again. +// console.log(decimalNumber) should be console.log(convertToPercentage) // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + return `${decimalNumber * 100}%`; + +} + +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..3e633b725 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,21 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// line 9 num is not defined. -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +//} // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here - +// 3 is not a variable for the function to use. It should be a variable name like num or n. // 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..7fcd67dfe 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,20 @@ // Predict and explain first... // =============> write your prediction here +// it looks fine. -function multiply(a, b) { - console.log(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 - +// It misses the return value of multiply function. // 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..6147695e4 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 +// there is an error on line 5 and 6 +//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 +//the return statement is not returning any value. a+b mentioned after return. // 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..986d3753b 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,35 @@ // Predict the output of the following code: // =============> Write your prediction here +// the result will be 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 +//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 +// because num is defined as 103 and not taking any input from the function parameter. // 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 +// because num is defined as 103 and not taking any input from the function parameter. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/new.js b/Sprint-2/2-mandatory-debug/new.js new file mode 100644 index 000000000..e69de29bb diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..a88f4fa2e 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ 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 bmi.toFixed(1); +} +console.log(`The BMI of a person weighing 70kg and 1.73m tall is ${calculateBMI(70, 1.73)}`); \ 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..07274a51f 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,8 @@ // 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(str) { + + return str.toUpperCase().replace(/ /g, '_'); +} +console.log(toUpperSnakeCase("it's done!")); \ 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..5e2fdf1b7 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}`; +} +console.log(toPounds("399p")); +console.log(toPounds("50p")); +console.log(toPounds("5p")); +console.log(toPounds("0p")); +console.log(toPounds("12345p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..8afb1df79 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,7 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } + // 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 @@ -18,17 +19,20 @@ function formatTimeDisplay(seconds) { // 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: +console.log(formatTimeDisplay(61)); // 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 - +// 1, because remainingSeconds is 1 when pad is called for the last time. // 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 adds a leading zero to single digit numbers. \ 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..970f590fa 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,24 +2,42 @@ // 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`; + +if (!/^\d{2}:\d{2}$/.test(time)) { + return "Invalid time format"; + } + +const hours = Number(time.slice(0, 2)); +const minutes = Number(time.slice(3, 5)); +if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) { + return "Invalid time range"; } - return `${time} am`; -} + let period = "am"; + let displayHours = hours; -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); + if (hours === 0) { + displayHours = 12; // midnight + } else if (hours === 12) { + period = "pm"; // noon + } else if (hours > 12) { + displayHours = hours - 12; + period = "pm"; + } + const formattedHours = String(displayHours).padStart(2, "0"); + const formattedMinutes = String(minutes).padStart(2, "0"); + return `${formattedHours}:${formattedMinutes} ${period}`; +} +console.log(formatAs12HourClock("00:00")); -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("12:00") === "12:00 pm", "Noon"); +console.assert(formatAs12HourClock("08:30") === "08:30 am", "Morning"); +console.assert(formatAs12HourClock("15:45") === "03:45 pm", "Afternoon"); +console.assert(formatAs12HourClock("23:59") === "11:59 pm", "Late night"); +console.assert(formatAs12HourClock("24:00") === "Invalid time range", "Hour too high"); +console.assert(formatAs12HourClock("12:60") === "Invalid time range", "Minute too high"); +console.assert(formatAs12HourClock("8:7") === "Invalid time format", "Wrong format"); +console.assert(formatAs12HourClock("hello") === "Invalid time format", "Not a time"); \ No newline at end of file