diff --git a/week-1/errors/0.js b/week-1/errors/0.js index cf6c5039..e9c7b03f 100644 --- a/week-1/errors/0.js +++ b/week-1/errors/0.js @@ -1,2 +1,2 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +/*This is just an instruction for the first activity - but it is just for human consumption +We don't want the computer to run these 2 lines - how can we solve this problem?*/ diff --git a/week-1/errors/1.js b/week-1/errors/1.js index 7a43cbea..e57b2e97 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +console.log(age); diff --git a/week-1/errors/2.js b/week-1/errors/2.js index e09b8983..2865eb8a 100644 --- a/week-1/errors/2.js +++ b/week-1/errors/2.js @@ -1,5 +1,5 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/week-1/errors/3.js b/week-1/errors/3.js index ffa72ca4..4c5c3271 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,5 +1,6 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/week-1/errors/4.js b/week-1/errors/4.js index 21dad8c5..65bbbdd9 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,2 +1,4 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 117bcb2b..c16718b2 100644 --- a/week-1/exercises/count.js +++ b/week-1/exercises/count.js @@ -4,3 +4,5 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing +// ans ; it will reassign the count variable +console.log(count); diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index bd4a4740..31ffae57 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -1,4 +1,3 @@ - const num = 56.5467; // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math @@ -8,3 +7,10 @@ const num = 56.5467; // Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) // Log your variables to the console to check your answers +const wholeNumberPart = Math.floor(num); +const decimalPart = num - wholeNumberPart; +const roundedNum = Math.round(num); + +console.log(wholeNumberPart); +console.log(decimalPart); +console.log(roundedNum); diff --git a/week-1/exercises/initials.js b/week-1/exercises/initials.js index 50b62103..3791bc5d 100644 --- a/week-1/exercises/initials.js +++ b/week-1/exercises/initials.js @@ -4,3 +4,5 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string in upper case to form the user's initials // Log the variable in each case +let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); +console.log(initials); diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index c91cd2ab..bf238eb0 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -15,4 +15,8 @@ const base = filePath.slice(lastSlashIndex + 1); console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable +const dir = filePath.slice(filePath.indexOf("/"), filePath.lastIndexOf("/")); +console.log(dir); // Create a variable to store the ext part of the variable +const ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(ext); diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 79a4a4d5..1d89c685 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -7,3 +7,4 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num several times to build an idea of what the program is doing +console.log(num); diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 49b0ac15..9a5c55fb 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -12,9 +12,13 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made +There are 2 function calls + Number + replaceAll // b) Identify all the lines that are variable reassignment statements + // c) Identify all the lines that are variable declarations // d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 196be3b2..928cc3d1 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -11,9 +11,7 @@ const pounds = paddedPenceNumberString.substring( paddedPenceNumberString.length - 2 ); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); +const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); console.log(`£${pounds}.${pence}`); diff --git a/week-2/debug/0.js b/week-2/debug/0.js index b46d471a..28ccc51f 100644 --- a/week-2/debug/0.js +++ b/week-2/debug/0.js @@ -5,3 +5,7 @@ function multiply(a, b) { } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +//The first line of the function logs the product of the two arguments to the console. The second line of the code calls the multiply() function with the arguments 10 and 32, and then logs the result to the console. The expected output is: + +//320 \ No newline at end of file diff --git a/week-2/debug/1.js b/week-2/debug/1.js index df4020ca..9c08a61f 100644 --- a/week-2/debug/1.js +++ b/week-2/debug/1.js @@ -1,8 +1,9 @@ // Predict and explain first... function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +//the error here that we should place the a + b expression directly after the return keyword. \ No newline at end of file diff --git a/week-2/debug/2.js b/week-2/debug/2.js index bae9652a..9f31b8fe 100644 --- a/week-2/debug/2.js +++ b/week-2/debug/2.js @@ -1,14 +1,12 @@ -// Predict and explain first... - +// Predict and explain first.. +// This program should tell the user the last digit of each number. +// Explain why getLastDigit is not working properly - correct the problem const num = 103; -function getLastDigit() { - return num.toString().slice(-1); +function getLastDigit(num) { + return num % 10; } 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/week-2/errors/0.js b/week-2/errors/0.js index 58b1349d..076a6a00 100644 --- a/week-2/errors/0.js +++ b/week-2/errors/0.js @@ -4,6 +4,7 @@ // interpret the error message and figure out why it's happening, if your prediction was wrong function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +console.log(capitalise("hello")); diff --git a/week-2/errors/1.js b/week-2/errors/1.js index 14b5b511..1731afad 100644 --- a/week-2/errors/1.js +++ b/week-2/errors/1.js @@ -2,12 +2,14 @@ // Write down the error you predict will be raised // Why will an error occur when this program runs? // Play computer with the example to work out what is going on +//In this code, there is an error that the variable decimalNumber, declared twice. once as a parameter of the convertToPercentage function and again outside the function, because in JS variables can't be declared twice within the same scope +// to fix this error, we need to delete the parameter in line 8, so we can call the function without any argument. -function convertToPercentage(decimalNumber) { +function convertToPercentage() { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage()); diff --git a/week-2/errors/2.js b/week-2/errors/2.js index b0454133..efbeeb1f 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -1,10 +1,11 @@ - // Predict and explain first... // this function should square any number but instead we're going to get an error // what is happening? How can we fix it? +// the error here is the (num) variable is not defined within the square function. +// to fix this error, we need to define the (num) variable within the square function, like this: -function square(3) { - return num * num; +function square(num) { + return num * num; } - +console.log(square(3)); diff --git a/week-2/implement/bmi.js b/week-2/implement/bmi.js index 172c7c9a..ecac9983 100644 --- a/week-2/implement/bmi.js +++ b/week-2/implement/bmi.js @@ -13,3 +13,14 @@ // Given someone's weight in kg and height in metres // When we call this function with the weight and height // Then it returns their Body Mass Index to 1 decimal place + +function calculateBMI(weight, height) { + const BMI = weight / (height * height); + return BMI.toFixed(1); + +} +const weight = 70; //weight in Kg +const height = 1.73; // height in m + +const BMIResult = calculateBMI(weight, height); +console.log("BMI:", BMIResult); \ No newline at end of file diff --git a/week-2/implement/cases.js b/week-2/implement/cases.js index 56b0d8d0..c6931fa7 100644 --- a/week-2/implement/cases.js +++ b/week-2/implement/cases.js @@ -10,8 +10,17 @@ // Then it returns the string in UPPER_CAMEL_CASE, so "HELLO_THERE" // Test case: we expect "lord of the rings" to be "LORD_OF_THE_RINGS" -// Test case: we expect "the great gatsby" to be "THE_GREAT_GATSBY" -// Test case: we expect "the da vinci code" to be "THE_DA_VINCI_CODE" - // Come up with a clear, simple name for the function // Use the string documentation to help you plan your solution + +function camelCaseToWords(text) { + const result = text.replaceAll(/\s/g, "_"); + return result.toUpperCase(); +} +const text1 = "lord of the rings"; +const text2 = "the great gatsby"; +const text3 = "the great gatsby"; + +console.log(camelCaseToWords(text1)); +console.log(camelCaseToWords(text2)); +console.log(camelCaseToWords(text3)); diff --git a/week-2/implement/to-pounds.js b/week-2/implement/to-pounds.js index 7add3d05..1679109d 100644 --- a/week-2/implement/to-pounds.js +++ b/week-2/implement/to-pounds.js @@ -3,3 +3,14 @@ // Take this code and turn it into a reusable block of code. // Declare a function called toPounds with an appropriately named parameter. // 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("499p")); // we expect the output: £4.99 +console.log(toPounds("123p")); // we expect the output: £1.23 +console.log(toPounds("5p")); // we expect the output: £0.05 \ No newline at end of file diff --git a/week-2/implement/vat.js b/week-2/implement/vat.js index 44c38d74..0dc2ce5f 100644 --- a/week-2/implement/vat.js +++ b/week-2/implement/vat.js @@ -8,3 +8,11 @@ // Given a number, // When I call this function with a number // Then it returns the new price with VAT added on + +function calculatePriceWithVAT(price) { + price = price * 1.2; + + return price; +} + +console.log(calculatePriceWithVAT(50)); diff --git a/week-2/interpret/time-format.js b/week-2/interpret/time-format.js index 15793e20..9187e783 100644 --- a/week-2/interpret/time-format.js +++ b/week-2/interpret/time-format.js @@ -26,18 +26,29 @@ console.log(formatTimeDisplay(143)); // Questions // a) When formatTimeDisplay is called how many times will pad be called? +// Here The pad function is called three times. // Call formatTimeDisplay with an input of 143, now answer the following: // b) What value is assigned to the parameter num when pad is called for the first time? +// When formatTimeDisplay is called with an input of 143, the value will be assigned to the parameter num when pad is called for the first time is 2. // c) What is the return value of pad when it is called for the first time? +//The return value of pad when it is called for the first time is 02 // d) What is the value assigned to the parameter num when pad // is called for the last time in this program? Explain your answer +//The value assigned to the parameter num when pad is called for the last time in this program is 00. +// because the remaining seconds is the remainder of the seconds after dividing by 60 // e) What is the return value when pad is called // for the last time in this program? Explain your answer +//The return value when pad is called for the last time in this program is 23. +//because the assigned value will return the num value itself // f) Research an alternative way of padding the numbers in this code. // Look up the string functions on mdn +//we can use padStart(2, '0') to pad the numbers to a length of two characters with the padding character '0'. like this: +//function pad(num) { + //return num.toString().padStart(2, '0'); +//} \ No newline at end of file diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..1c3aacc1 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -1,10 +1,31 @@ +// formatAs12HourClock currently has a 🐛 + +// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" +// b) Check the assertion output and explain what the bug is +// c) Now fix the bug and re-run all your assertions + function formatAs12HourClock(time) { if (Number(time.slice(0, 2)) > 12) { - return `${Number(time.slice(0, 2)) - 12}:00 pm`; + let a = `${Number(time.slice(0, 2)) - 12}`; + if (a < 9) { + a = ("0" + a).slice(-2); + } + let b = `${Number(time.slice(-2))}`; + if (b < 9) { + b = "0" + b; + } + return a + ":" + b + " " + "pm"; + } + let a = `${time.slice(0, 2)}`; + if (a < 9) { + a = ("0" + a).slice(-2); } - return `${time} am`; + let b = `${Number(time.slice(-2))}`; + if (b < 9) { + b = "0" + b; + } + return a + ":" + b + " " + "am"; } - const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( @@ -23,8 +44,4 @@ console.assert( targetOutput2 ); -// formatAs12HourClock currently has a 🐛 - -// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" -// b) Check the assertion output and explain what the bug is -// c) Now fix the bug and re-run all your assertions +console.log(formatAs12HourClock("11:00")); diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 9dd3a210..38be0d8b 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -21,3 +21,25 @@ // Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" + +function getAngleType(angle) { + if (angle < 90) { + return "Acute angle"; + } else if (angle == 90) { + return "Right angle"; + } else if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } else if (angle == 180) { + return "Straight angle"; + } else if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + return "invalid angle"; +} + +console.log(getAngleType(80)); +console.log(getAngleType(90)); +console.log(getAngleType(120)); +console.log(getAngleType(180)); +console.log(getAngleType(200)); +console.log(getAngleType(540)); diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 0dd74fbc..abe57016 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -29,3 +29,28 @@ // 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." + +function getCardValue(cardString) { + const [rank, suit] = cardString.split("♠"); + switch (rank) { + case "2": + case "3": + case "4": + case "5": + case "6": + case "7": + case "8": + case "9": + case "10": + return parseInt(rank); + case "J": + case "Q": + case "K": + return 10; + case "A": + return 11; + default: + throw new Error("Invalid card rank."); + } +} +console.log(getCardValue(10)); diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js index 31da32b5..9487be3d 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -33,3 +33,26 @@ // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. // These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator. + +function isProperFraction(numerator, denominator) { + if (denominator === 0) { + throw new Error("Denominator cannot be zero"); + } + + return Math.abs(numerator) < Math.abs(denominator); +} + +console.assert(isProperFraction(2, 3) === true, "Test Case 1 Failed"); // Proper Fraction check +console.assert(isProperFraction(5, 2) === false, "Test Case 2 Failed"); // Improper Fraction check +console.assert(() => { + try { + isProperFraction(3, 0); // Zero Denominator check + } catch (error) { + return error.message === "Denominator cannot be zero"; + } + return false; +}, "Test Case 3 Failed"); +console.assert(isProperFraction(-4, 7) === true, "Test Case 4 Failed"); // Negative Fraction check +console.assert(isProperFraction(3, 3) === false, "Test Case 5 Failed"); // Equal Numerator and Denominator check + +console.log("All assertions passed!"); diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.js index 7b22836b..94058828 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -38,3 +38,12 @@ // Then it should return true because the input forms a valid triangle. // This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem. + +function isValidTriangle(a, b, c) { + if (a + b <= c || a + c <= b || b + c <= a) { + return false; // Invalid triangle + } + return true; // Valid triangle +} +const isTriangleValid = isValidTriangle(3, 4, 5); +console.log(isTriangleValid); // Output: true (valid triangle)