diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..a6f91bcb5 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,23 @@ -// Predict and explain first... -// =============> write your prediction here +// =============> Prediction <============= +// I think the code will work as expected. For example, if capitalise("andora") +// is run, it should return "Andora". -// call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring +// =============> Actual Results <============= +// The code did not run successfully due to a syntax error. Since the variable +// str was already declared when in the capitalise function input, it cannot +// be declared again as was done on line 6. + +// =============> Corrected Script <============= function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + if (str === "") { + return ""; + } + + return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here +console.assert( + capitalise("andora") === "Andora", + `current output: ${capitalise("andora")}, expected output: Andora` +); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..ee5bad81a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,16 @@ -// Predict and explain first... +// =============> Prediction <============= +// The script won't run because the decimalNumber variable is being declared +// twice (first in the convertToPercentage input, and again inside the function) -// Why will an error occur when this program runs? -// =============> write your prediction here - -// Try playing computer with the example to work out what is going on +// =============> Actual Results <============= +// The script threw a syntax error due to the double variable declaration +// =============> Corrected Script <============= function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; - - return percentage; + return `${decimalNumber * 100}%`; } -console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +console.assert( + convertToPercentage(0.45) === "45%", + `current output: ${convertToPercentage(0.45)}, expected output: 45%` +); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..ded851828 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,22 @@ - -// 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 - -function square(3) { - return num * num; +// =============> Prediction <============= +// It will throw an error. This is because the variable num is being used without +// being declared previously. + +// =============> Actual Results <============= +// Error message: function square(3) { +// ^ +// SyntaxError: Unexpected number + +// =============> Explanation <============= +// An error was thrown because the because we cannot name a variable with a number +// as has been done in the function input + +// =============> Corrected Script <============= +function square(num) { + return num * num; } -// =============> write the error message here - -// =============> explain this error message here - -// Finally, correct the code to fix the problem - -// =============> write your new code here - - +console.assert( + square(3) === 9, + `actual result: ${square(3)}, expected result: 9` +); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..9e17c9f46 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,26 @@ -// Predict and explain first... +// =============> Prediction <============= +// The code will not throw an error -- it's syntactically fine. However, the +// multiply function is missing a return call, so it will return undefined +// by default. The function therefore simply prints the result of the +// multiplication instead of actually returning it as a value. -// =============> write your prediction here +// =============> Actual Results <============= +// 320 +// The result of multiplying 10 and 32 is undefined +// =============> Explanation <============= +// The 320 is printed in the terminal because of the console.log(). Because +// no return was written, the default undefined is generated. + +// =============> Corrected Script <============= function multiply(a, b) { - console.log(a * b); + return a * b; } -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - -// =============> write your explanation here +const actualResult = multiply(10, 32); +const expectedResult = 320; -// Finally, correct the code to fix the problem -// =============> write your new code here +console.assert( + actualResult === expectedResult, + `actual result: ${actualResult}, expected result: ${expectedResult}` +); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..f94233bd0 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ -// Predict and explain first... -// =============> write your prediction here +// =============> Prediction <============= +// The script will run but not as intended. The sum function has a valid +// return, though it is simply null. Since a + b calculation comes after +// the return, it isn't run as the program exits the loop once the return +// call has been made. I therefore expect the following to be printed on +// the terminal: +// The sum of 10 and 32 is undefined + +// =============> Actual Results <============= +// The sum of 10 and 32 is undefined + +// =============> Explanation <============= +// The return was made before a and b could be added. + +// =============> Corrected Script <============= function sum(a, b) { - return; - a + b; + return a + b; } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +const actualResult = sum(10, 32); +const expectedResult = 42; -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here +console.assert( + actualResult === expectedResult, + `actual result: ${actualResult}, expected result: ${expectedResult}` +); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..f6daa3d11 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,41 @@ -// Predict and explain first... +// =============> Prediction <============= +// The script will run but not as intended. Each time the getLastDigit +// function is ran, it will return "3". This is because it's using the +// global variable num -// Predict the output of the following code: -// =============> Write your prediction here +// =============> Actual Results <============= +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 -const num = 103; +// =============> Explanation <============= +// The getLastDigit is using the global num variable instead of an +// input variable -function getLastDigit() { +// =============> Corrected Script <============= +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)}`); +const actualResult_1 = getLastDigit(42); +const actualResult_2 = getLastDigit(105); +const actualResult_3 = 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 -// Finally, correct the code to fix the problem -// =============> write your new code here +const expectedResult_1 = "2"; +const expectedResult_2 = "5"; +const expectedResult_3 = "6"; -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +console.assert( + actualResult_1 === expectedResult_1, + `actual result: ${actualResult_1}, expected result: ${expectedResult_1}` +); + +console.assert( + actualResult_2 === expectedResult_2, + `actual result: ${actualResult_2}, expected result: ${expectedResult_2}` +); + +console.assert( + actualResult_3 === expectedResult_3, + `actual result: ${actualResult_3}, expected result: ${expectedResult_3}` +); diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..d66965f8a 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -1,19 +1,13 @@ -// Below are the steps for how BMI is calculated - -// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. - -// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: - -// squaring your height: 1.73 x 1.73 = 2.99 -// dividing 70 by 2.99 = 23.41 -// Your result will be displayed to 1 decimal place, for example 23.4. +function calculateBMI(weight, height) { + const bmi = parseFloat((weight / height ** 2).toFixed(1)); -// You will need to implement a function that calculates the BMI of someone based off their weight and height + return bmi; +} -// Given someone's weight in kg and height in metres -// Then when we call this function with the weight and height -// It should return their Body Mass Index to 1 decimal place +const calculatedBMI = calculateBMI(70, 1.73); +const actualBMI = 23.4; -function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file +console.assert( + calculatedBMI === actualBMI, + `calculated BMI: ${calculatedBMI}, actual BMI: ${actualBMI}` +); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..d8c2e3f2f 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -1,16 +1,21 @@ -// A set of words can be grouped together in different cases. +function upperCaseSnake(str) { + const upperCaseSnake = str.toUpperCase().replaceAll(" ", "_"); -// For example, "hello there" in snake case would be written "hello_there" -// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. + return upperCaseSnake; +} -// Implement a function that: +let returnedUpperCaseSnake = upperCaseSnake("hello_there"); +let actualUpperCaseSnake = "HELLO_THERE"; -// Given a string input like "hello there" -// When we call this function with the input string -// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" +console.assert( + returnedUpperCaseSnake === actualUpperCaseSnake, + `returned upperCaseSnake: ${returnedUpperCaseSnake}, actual upperCaseSnake: ${actualUpperCaseSnake}` +); -// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" +returnedUpperCaseSnake = upperCaseSnake("lord of the rings"); +actualUpperCaseSnake = "LORD_OF_THE_RINGS"; -// 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 +console.assert( + returnedUpperCaseSnake === actualUpperCaseSnake, + `returned upperCaseSnake: ${returnedUpperCaseSnake}, actual upperCaseSnake: ${actualUpperCaseSnake}` +); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..e37fe0e63 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,29 @@ -// In Sprint-1, there is a program written in interpret/to-pounds.js +function toPounds(str) { + const price = (parseInt(str.replace("p", ""), 10) / 100).toFixed(2); -// You will need to take this code and turn it into a reusable block of code. -// You will need to declare a function called toPounds with an appropriately named parameter. + return `£${price}`; +} -// You should call this function a number of times to check it works for different inputs +let returnedPrice = toPounds("399p"); +let actualPrice = "£3.99"; + +console.assert( + returnedPrice === actualPrice, + `returned price: ${returnedPrice}, actual price: ${actualPrice}` +); + +returnedPrice = toPounds("1p"); +actualPrice = "£0.01"; + +console.assert( + returnedPrice === actualPrice, + `returned price: ${returnedPrice}, actual price: ${actualPrice}` +); + +returnedPrice = toPounds("102342342342p"); +actualPrice = "£1023423423.42"; + +console.assert( + returnedPrice === actualPrice, + `returned price: ${returnedPrice}, actual price: ${actualPrice}` +); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..fe92f306a 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,20 @@ 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 - -// Questions +// =============> Questions <============= // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// Answer: 3 times during ${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 +// Answer: 0. The first call is at pad(totalHours) when totalHours = 0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// Answer: "00" since we add padding of "0" at the start for a string of minimum length 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 +// Answer: 1 since the last time pad is called is in pad(remainingSeconds) (and remainingSeconds = 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 +// Answer: "01" since pad(1) returns "01"