diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..6bfb4d935 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 +// =============> I predict that we will not need to write let in line 8 as str is already declared +// as an argument within the function captialise. // 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; +// } + +// capitalise ('hello'); +//==============> A syntaxError occurred as 'str' is being cleared. + + function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - -// =============> write your explanation here -// =============> write your new code here +console.log (capitalise ('hello')); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..f2380bdaa 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,33 @@ + // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> I believe that the function convertToPercentage is not mentioned in the console.log statement. +// Declaring the const decimalNumber is unnecessary as it is already declared as a parameter within the function convertToPercentage. // 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 +// =============> A syntaxError occurred saying decimalNumber is already declared. // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(num) { + + const percentage = `${num * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(0.1)); + + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..b3cffa4c9 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,26 @@ + // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> The num parameter should be mentioned inside the function square instead of the number 3. +// then it should be called as square (3) in the console.log statement. -function square(3) { - return num * num; -} +//function square(3) { +//return num * num; +//} + +// =============> function square(3) syntaxError: Unexpected number. +// =============> 3 wasn't expected... instead a declaration like 'num' was expected. -// =============> write the error message here -// =============> 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..a1ff337ba 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,21 @@ + // Predict and explain first... -// =============> write your prediction here +// =============> In line 6, instead of console.log printing the result of multiply(10, 32), return a*b would be better +//because the console.log would only print a*b. -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 +// =============> After running the code, we got 'the result of multiplying 10 and 32 is undefined' +// as the multiply parameters weren't considered as parameters inside the function multiply. // 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..626b920e7 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,13 @@ // Predict and explain first... -// =============> write your prediction here +// =============> SyntaxError will occur due to putting a semicolon after return. function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> unlike the expectation that the code runs without error as 'the sum of 10 and 32 is undefined'. // Finally, correct the code to fix the problem // =============> write your new code here +// we needed just to remove the semicolon after return statement and to bring up the a+b expression side by side with return. \ 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..e45371383 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,34 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> The code will return 3 as a string first, but after calling the getLastDigit function, +// it will not return anything because every number based will be converted to string. -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 +// =============> The last digit of 42 is 3 three times. +// the prediction was close but not accurate, the case is that the parameter num needs to be +//the function instead of const num = 103. +// =============> // 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 +// parameter num needs to be with the getLastDigit function instead of const num=103. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..c1c1915cb 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + + const bmi = weight / (weight*height); + return bmi.toFixed(1); // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + +console.log(`the BMI is ${calculateBMI(80, 1.8)}`); +//I expect the function will return a number and it does return a number. \ 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..9fd896449 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -1,3 +1,4 @@ + // A set of words can be grouped together in different cases. // For example, "hello there" in snake case would be written "hello_there" @@ -14,3 +15,15 @@ // 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 capSnakeCase(str) { + const upper = str.toUpperCase(); + const snake = upper.replace(/ /g, "_"); + return snake; +} +console.log(capSnakeCase("lord of the rings")); + +// step 1: get a string input +//step 2: change the upper case +//step 3: replace spaces with underscores +// step 4: return capSnakeCase diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..e4e4cfde8 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,15 @@ // 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("5p")) +console.log(toPounds("5678p")) \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..dfc96bda3 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -1,3 +1,4 @@ + function pad(num) { return num.toString().padStart(2, "0"); } @@ -10,25 +11,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 +// =============> It will be called 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 +// =============> The leftover second is assigned to num as remainingSEconds 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 +// =============> 01 the remainingSeconds from passed from pad to num is changed to string and padded as 01 diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..9744e6179 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -1,3 +1,4 @@ + // 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. @@ -23,3 +24,52 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("13:00"); +const targetOutput5 = "01:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + + + +// modified code: + +function formatAs12HourClock(time) { + let hours = Number(time.slice(0, 2)); + const minutes = time.slice(3); + + let suffix; + if (hours >= 12) { + suffix = "pm"; + + } else { + suffix = "am"; + } + hours = hours % 12 || 12; // Convert 0 to 12, 13 to 1 + const formattedHours = hours.toString().padStart(2, '0'); + + return `${formattedHours}:${minutes} ${suffix}`; +} + +const currentOutput6 = formatAs12HourClock("23:00"); +const targetOutput6 = "11:00 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); diff --git a/median.js b/median.js new file mode 100644 index 000000000..e69de29bb diff --git a/median.test.js b/median.test.js new file mode 100644 index 000000000..e69de29bb