diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..4b8adb940 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,32 @@ // Predict and explain first... // =============> write your prediction here +// capitalise function takes 1 str arg +// let srting will give an error if i try to pass my string. +// remove let +// str is being put in template literal +// str[0]: 1st index of str becomes uppercase +// str.slice(1): 2nd index slice until end of str +// str = "frankocean" +// return = "Frankocean" + // 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 +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// I was right, error given is because of let str within the function. +// SyntaxError: Identifier 'str' has already been declared +// We cannot have two declarations of the same variable so we remove the let inside .capitalise. // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} +console.log(capitalise("frankocean")) diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..66901d730 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,19 +2,33 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// decimalNumber declared inside the function, this is the value that must be an arg +// line 17 also fails because it tries to access a var that is declared within 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}%`; +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +// } -console.log(decimalNumber); +// console.log(decimalNumber); // =============> write your explanation here +// Remove the local var declaration +// ReferenceError: decimalNumber is not defined | console.log(decimalNumber); +// define decimalNumber in line 17 by passing the function with decimalNumber value +// remove the definition in line 11 // 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-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..1e01ecb9b 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -4,17 +4,25 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// Error because 3 is a value, functions take parameters(like num). +// It wont work 3 is a number literal and not a name/identifier. -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 +// The code was expecting the parameter name but found the number literal =3 +// This is why we get the syntax error. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(3)); \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..4307eeac4 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,26 @@ // Predict and explain first... // =============> write your prediction here +// It will print 2 lines: +// (10*32=320) line 1: 320 +// line 2: The result of multiplying 10 and 32 is undefined -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 +// There are two console.log() being called. +// The one inside the funtion prints the result of the multiplication +// The one outside the function prints the desired result of the whole string but there is no return from the function so it is undefined +// The result of multiplying 10 and 32 is undefined. // 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-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..cf860db05 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // =============> write your prediction here +// I dont think the code will run at all since the return is separated from the sum that should happen +// the console.log() oustide the function will say the sum is undefined. -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 sum of 10 and 32 is undefined +// I was wrong, I thought that the code would not run. +// The output says the sum is undefined because the return statement and the "a + b" are separated by ; +// To fix it, I just need to remove the ; next to return on line 7. // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..726549eb8 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,23 +2,44 @@ // Predict the output of the following code: // =============> Write your prediction here +// Nothing is going to run properly because the function does not have any parameters. +// I think everything will get a syntax error -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 +// I was wrong in my prediction. Code runs. +// It says the last digit is 3 for every number which is wrong +// This is because we declared the var num = 103 globally, and it is affecting all the fucntion calls +// Because of the global declaration, every function call is going to use the global num +// This is bad because the code is now taking in diferent parameters but returning a value based on one global value. +// TO fix this we remove the global var num. +// Then we add num as a parameter in the function :getLastDigit(num) + // 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-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..eded36a45 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,6 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + return (weight / (height * height)).toFixed(1) } +console.log(calculateBMI(70, 1.73)) \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..943eabf25 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,10 @@ // 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 UPPER_SNAKE_CASE(str) { + let snake_case = str.replaceAll(" ", "_"); + return snake_case.toUpperCase() +} + +console.log(UPPER_SNAKE_CASE("have you ever had a krispy kreme")) \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..a7f3178d4 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,16 @@ // 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(str) { + let penceStringWithoutTrailingP = str.substring(0, str.length - 1); + + let paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + let pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + + let pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + + console.log(`£${pounds}.${pence}`); +} + +toPounds("5045p") \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..f53bec84e 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -7,10 +7,10 @@ function pad(num) { } function formatTimeDisplay(seconds) { - const remainingSeconds = seconds % 60; - const totalMinutes = (seconds - remainingSeconds) / 60; - const remainingMinutes = totalMinutes % 60; - const totalHours = (totalMinutes - remainingMinutes) / 60; + const remainingSeconds = seconds % 60; // 1 + const totalMinutes = (seconds - remainingSeconds) / 60; // 1 + const remainingMinutes = totalMinutes % 60; // 1 + const totalHours = (totalMinutes - remainingMinutes) / 60; //0 return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } @@ -21,18 +21,18 @@ 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: - +console.log(formatTimeDisplay(61)) // 00:01:01 // 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 when it 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 // e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// "01" diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..f7609b09f 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -2,24 +2,61 @@ // 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. +// original code: +// function formatAs12HourClock(time) { +// const hours = Number(time.slice(0, 2)); +// if (hours > 12) { +// return `${hours - 12}:00 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 currentOutput2 = formatAs12HourClock("23:00"); +// const targetOutput2 = "11:00 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); + +// my code: function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours >= 12) { + return `${hours - 12}:${time.slice(3)} pm`; } - return `${time} am`; + return `${time}:${time.slice(3)} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); +const cases = [ + { input: "00:00", expected: "12:00 am" }, // midnight + { input: "01:00", expected: "01:00 am" }, + { input: "02:00", expected: "02:00 am" }, // your failing test + { input: "09:00", expected: "09:00 am" }, + { input: "11:59", expected: "11:59 am" }, + { input: "12:00", expected: "12:00 pm" }, // noon + { input: "12:30", expected: "12:30 pm" }, + { input: "13:00", expected: "01:00 pm" }, + { input: "23:00", expected: "11:00 pm" }, // your other test + { input: "23:59", expected: "11:59 pm" }, +]; -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +function runTests(cases) { + let passed = 0; + for (const { input, expected } of cases) { + const actual = formatAs12HourClock(input); + const ok = actual === expected; + if (ok) passed++; + console.log( + `${ok ? "PASS" : "FAIL"} input: ${input} expected: ${expected} actual: ${actual}` + ); + } + console.log(`\n${passed}/${cases.length} passed`); +} +runTests(cases); \ No newline at end of file