diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..037f714c23 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,20 @@ // Predict and explain first... -// =============> write your prediction here +// =============> This will not run because the variable str has already been used +// as a parameter. This will cause a syntax error + // 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 str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; + } -// =============> write your explanation here -// =============> write your new code here +// =============> The first mistake this renders is indeed in line 14 that +// "Identifier 'str' has already been declared" +// =============> +// +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..9e4974ba04 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,15 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// An error will occur because the variable decimalNumber is being declared as both +// a parameter and as a local variable inside the function. This will cause a +// syntax error. Additionally, the variable decimalNumber is not defined +// outside the function, so it will throw a ReferenceError when trying to log it +// to the console. +// The percentage sign is not seen as a text character but instead as code for +// the function to calculate the remainder after a division operation: the modulo. +// It will therefore not return a percentage as desired + // Try playing computer with the example to work out what is going on @@ -15,6 +24,24 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// The first error is that the variable decimalNumber is being declared twice, +// first as a parameter and then as a local variable inside the function. This +// will cause a syntax error. +// The second error is that the variable decimalNumber is not defined outside the +// function. It will not log to the console but throw a ReferenceError because it +// is not accessible in that scope. + +// Also, on testing the code I saw that I had misjudged the modulo issue - it is not +// an issue at all because the percentage sign is being used as a text character in a +// template literal, so it will be treated as such and not as code for the modulo operator. // 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)); \ 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 aad57f7cfe..69094eddcd 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,26 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// "num" has not been defined and the parameter is not used in the function; +// it has a number instead + function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Unexpected number + // =============> explain this error message here +// The error message is saying that it was not expecting a number in the function +// declaration. This is because the parameter should be a variable name, not a number // 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 b27511b417..6e60323912 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,9 @@ // Predict and explain first... // =============> write your prediction here +// The function multiply does not return anything, it only logs the result to the console. +// Therefore the outcome of the final console.log will be undefined + function multiply(a, b) { console.log(a * b); @@ -9,6 +12,15 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The function multiply does not return a value, it only logs the result to the console. +// Therefore, on using the template literal to write the result of the function, it doesn't +// have this result and renders "undefined" instead + // 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 37cedfbcfd..b407b8793d 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,9 @@ // Predict and explain first... // =============> write your prediction here +// the semicolon after "return" will hamper the return statement because this +// character will end the statement before receiving a definition of what it +// needs to return. + function sum(a, b) { return; @@ -9,5 +13,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// as above + // 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 57d3f5dc35..4ff10d9c6d 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,14 @@ +// This program should tell the user the last digit of each number. +// Explain why getLastDigit is not working properly - correct the problem // Predict and explain first... // Predict the output of the following code: // =============> Write your prediction here +// The "const" is hampering the function getLastDigit because the return +// function will always use the value of the const for its calculations and +// ignore any other input + + const num = 103; @@ -15,10 +22,24 @@ 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 +// The function getLastDigit is not working properly because it is using the variable +// num, which is defined outside the function with the value of 103. It will always +// return the last digit of 103 instead of the given parameter in the template literal + // 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 +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)}`); + diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..69b49a319a 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/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 + const bmi = weight / (height * height); + return parseFloat(bmi.toFixed(1)); } \ 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 5b0ef77ad9..b250f7caca 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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) { + const upperSnakeCase = str.toUpperCase().replace(/ /g, '_'); + return upperSnakeCase; +} + +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 6265a1a703..86230583ee 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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("123p")); +console.log(toPounds("5p")); +console.log(toPounds("0p")); +console.log(toPounds("98237986367887293p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..3df95d1513 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,7 @@ 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 @@ -17,18 +18,30 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> Three times: +// pad(totalHours) +// pad(remainingMinutes) +// pad(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 +// =============> 0 for totalHours // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> 0 for totalHours, which is then converted to "00" by the padStart +// method because the string length of "0" is less than 2 // 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 for remainingSeconds; it is the num parameter, not the string // 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 +// =============> The string "01" because the string length of "1" is less than 2 + +// By the way - the Python visualiser didn't want to work with this. It said: +// File "", line 1 +// function pad(num) { +// ^^^ +// SyntaxError: invalid syntax + +// I asked Claude.ai to help instead \ No newline at end of file diff --git a/Sprint-2/Project-CLI-Treasure-Hunt b/Sprint-2/Project-CLI-Treasure-Hunt new file mode 160000 index 0000000000..4374e4e6b8 --- /dev/null +++ b/Sprint-2/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 4374e4e6b862b0a110e806b6115f3f6b3c22b26a diff --git a/Sprint-2/prep.js b/Sprint-2/prep.js new file mode 100644 index 0000000000..403df50f49 --- /dev/null +++ b/Sprint-2/prep.js @@ -0,0 +1,17 @@ +function formatAs12HourClock(time) { + return `${time} am`; +} + +const currentOutput = formatAs12HourClock("08:00"); +const targetOutput = "08:00 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +const currentOutput = formatAs12HourClock("23:00"); +const targetOutput = "11:00 pm"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); \ No newline at end of file