diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..90ebd6b4f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// It will show a error // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -8,6 +9,13 @@ function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +capitalise("Hello"); // =============> write your explanation here +// SyntaxError: Identifier 'str' has already been declared - This error means that the variable 'str' was declared more than once in the same scope. +// JavaScrip doesn't allow that - once a variable name is declared with let or const, you can't declare it again. // =============> write your new code here +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 f2d56151f..31cbd537c 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// I predict that this program will throw a error message - due to the variable decimalNumber being redeclared // Try playing computer with the example to work out what is going on @@ -15,6 +16,22 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// Function covertToPercentage(decimalNumber) - it has a parameter named decimalNumber +// Inside the function: const decimalNumber = 0.5; - this redeclares the same variable name that's already used for the parameter. +// That causes a error: SyntaxError: Identifier 'decimalNumber' has already been declared - because you can't declare a const (or let) +// with the same name as a parameter inside the same function scope. +// JavaScript doesn't allow us to declare a new variable with the same name in the same scope, so it caused a error // 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)); + +// Function decimalNumber = 0.5 +// It calculates 0.5 * 100 = 50 +// It returns "50%" +// console logs 50% diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..aac154655 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -10,11 +10,16 @@ function square(3) { } // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here - +// The "Unexpected number" // Finally, correct the code to fix the problem - +// The "Unexpected number" error arises when a number is improperly positioned or used within JvaScript code // =============> write your new code here + function square(num){ + return num * num; +} + +console.log(square); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..4b11f2a88 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 will show a undefined error 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 - +// The function printed the answer but did't return it, so when it was used in the template string, it came out as 'undefined.' // Finally, correct the code to fix the problem // =============> write your new code here + function multiply(a, b) { + return a * b; +} + +console.log(` The sum of 10 and 32 is ${sum(10, 32)}`); + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..89fa511e0 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// I predict it will show a undefined error function sum(a, b) { return; @@ -9,5 +10,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The problem was the semicolon after return, which made the function stop before adding the numbers // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b){ + return a + b; // Once I put a + b on the same line as return, it worked +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..4ee83bf4d 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here - +// Since getLastDigit() does not declare any parameters, the arguments passed when calling it are going to be ignored const num = 103; function getLastDigit() { @@ -15,10 +15,25 @@ 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() does not have any parameters, but it is being called with arguments: +// getLastDigit(42), getLastDigit(105), and getLastDigit(806). Inside the function, it always uses the global const num, which is set to 103. +// The means every call ignores the last digit of 103. Which the '3' + + // 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-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..43614ac74 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,11 @@ 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(`Your BMI is ${calculateBMI(68, 1.75)}`); +// How it works: function calculateBMI(weight, height) +// Parameters: weight - number, person's weight in kilograms height - number, person's height in meters +// Returns: the body mass index (BMI), rounded to one decimal place \ 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..c6eeec8dd 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,6 @@ // 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 +const greeting = "Hello there." + +console.log(greeting.toUpperCase()); \ 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..eaa598a66 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,13 @@ // 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 formaPenceToPounds(penceString){ + const penceStringWithoutTrailingP = penceString.substring(0, penceString.length -1); + const pounds = paddedPenceNumberString.slice(0, -2); + const pence = paddedPenceNumberString.slice(-2); + return `£${pounds}.${pence}`; +} + +console.log(formatPenceToPounds("9p")); +console.log(formatPenceToPounds("39p")); +console.log(formatPenceToPounds("399")); \ 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..f8df8e934 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,27 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// Three 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 - +/* The first time pad() run, its used for hours part of time. + Because 61 seconds equals 0 hours, the value given to num is 0. */ // c) What is the return value of pad is called for the first time? // =============> write your answer here +/* The first time pad() runs, it's formatting the hours part of time. +Since the hours value is 0, it turns that into the string '00' by adding a leading zero before returning it. */ // 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 +/* When the function runs with 61 seconds, the last time pad() is called is for the second part of the time. +Since 61 seconds has 1 second left over after making a full minute, the value passed into pad() is 1. +Inside the function, num equals 1, and it gets turned into '01' so the final time shows as 00:01:01. */ // 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 +/* When pad() is called for the last time, num is 1 because there's 1 second left over. + The function turns that number into the string '01' by adding a leading zero. + That '01' is the value that gets returned and used as the seconds part of the final time display (00:01:01). */ diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..9c1ec09b0 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,7 +2,7 @@ // 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) { + /*function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { return `${hours - 12}:00 pm`; @@ -22,4 +22,55 @@ const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` +); */ + +// The function ignores the minutes and did'not handle midnight (00:00) or noon (12:00) properly. + // Better version: + function formatAs12HourClock(time) { + const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + if (hours == 0) return `12:${minutes} am`; + if (hours < 12) return `${time} am`; + if (hours === 12) return `${time} pm`; + + return `${hours - 12}:${minutes} pm`; +} +const currentOutput1 = formatAs12HourClock("08:00"); +const targetOutput1 = "08:00 am"; +console.assert( + currentOutput1 === targetOutput1, + `current output: ${currentOutput1}, target output: ${targetOutput1}` ); +// Test failed: due to a typo +const currentOutput2 = formatAs12HourClock("23:00"); +const targetOutput2 = "11:00 pm"; +console.assert( + currentOutput2 === targetOutput2, + `current output: ${currentOutput2}, target output: ${targetOutput2}` + +); + +const currentOutput3 = formatAs12HourClock("13:00"); +const targetOutput3 = "1:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3} target output: {targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("15:00"); +const targetOutput4 = "3:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4} target output: {targetOutput4}` +); + +console.log(formatAs12HourClock("08:00")); +console.log(formatAs12HourClock("23:00")); +console.log(formatAs12HourClock("15:00")); +console.assert(formatAs12HourClock("00:00") === "12:00 am"); +console.assert(formatAs12HourClock("12:00") === "12:00 pm"); + +// The test passed, so my function gave the expected results. +// All the console.assert() tests passed. +// formatAs12HourClock() function works correctly for the inputs I tested. \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index ca1dfe7f2..3aff65d14 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -13,8 +13,26 @@ function getAngleType(angle) { } // Run the tests, work out what Case 2 is testing, and implement the required code here. // Then keep going for the other cases, one at a time. + + if (angle < 90) { + return "Acute angle"; + } + + if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } + + if (angle === 180) { + return "Straight angle"; + } + + if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + } + // The line below allows us to load the getAngleType function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = getAngleType; @@ -49,15 +67,20 @@ assertEquals(acute, "Acute angle"); // Case 3: Identify Obtuse Angles: // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" -const obtuse = getAngleType(120); // ====> write your test here, and then add a line to pass the test in the function above +const obtuse = getAngleType(120); +assertEquals(obtuse, "Obtuse angle"); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" // ====> write your test here, and then add a line to pass the test in the function above +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" -// ====> write your test here, and then add a line to pass the test in the function above \ No newline at end of file +// ====> write your test here, and then add a line to pass the test in the function above +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index a4739af77..06ce9e577 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -8,9 +8,7 @@ // write one test at a time, and make it pass, build your solution up methodically function isProperFraction(numerator, denominator) { - if (numerator < denominator) { - return true; - } + return numerator < denominator; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -46,14 +44,15 @@ assertEquals(improperFraction, false); // target output: true // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. const negativeFraction = isProperFraction(-4, 7); -// ====> complete with your assertion +assertEquals(negativeFraction, true); + // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 // target output: false // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. const equalFraction = isProperFraction(3, 3); -// ====> complete with your assertion +assertEquals(equalFraction, false); // Stretch: // What other scenarios could you test for? diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index 266525d1b..140f63254 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -7,12 +7,20 @@ // complete the rest of the tests and cases // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers -function getCardValue(card) { +function getCardValue(card){ + const rank = card.slice(0, -1); if (rank === "A") { return 11; } + if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + +throw new Error ("Invalid card rank"); { + } + // The line below allows us to load the getCardValue function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = getCardValue; @@ -39,19 +47,25 @@ assertEquals(aceofSpades, 11); // When the function is called with such a card, // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). const fiveofHearts = getCardValue("5♥"); +assertEquals(fiveofHearts, 5); // ====> write your test here, and then add a line to pass the test in the function above // Handle Face Cards (J, Q, K): // Given a card with a rank of "10," "J," "Q," or "K", // When the function is called with such a card, // Then it should return the value 10, as these cards are worth 10 points each in blackjack. +const king = getCardValue("K♣"); +assertEquals(king, 10); // Handle Ace (A): // Given a card with a rank of "A", // When the function is called with an Ace, // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. - -// Handle Invalid Cards: -// Given a card with an invalid rank (neither a number nor a recognized face card), -// When the function is called with such a card, -// Then it should throw an error indicating "Invalid card rank." +const ace = getCardValue("A♠"); +assertEquals(ace, 11); +try { + getCardValue("Z♠"); +} catch (error) { + assertEquals(error.message, "Invaild card rank"); +} +} \ No newline at end of file