diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..65ad3030d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-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? \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..d660966b7 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 +// I changed for let -const age = 33; +let age = 33; age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..6d4dc4ae3 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,7 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? +// I have ti change the order -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..e95738ad1 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,4 +1,4 @@ -const cardNumber = 4533787178994213; +/*const cardNumber = 4533787178994213; const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber @@ -7,3 +7,9 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value +/*Answer> The code will not works because slice is strigs and array and +cardNumber is number */ +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.slice(-4); + + diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..2ce09132e 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,7 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +/*const 12HourClockTime = "20:53"; +const 24hourClockTime = "08:53"; +*/ + +//Answer: +const Hour12ClockTime = "20:53"; +const hour24ClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..bdae7723d 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,4 +1,4 @@ -let carPrice = "10,000"; +/*let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); @@ -12,11 +12,29 @@ 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 +/* carPrice.replaceAll(",", "") línea 4 +Number(carPrice.replaceAll(",", "")) línea 4 +priceAfterOneYear.replaceAll("," "") línea 5 +Number(priceAfterOneYear.replaceAll("," "")) línea 5 +console.log(...) línea 9 +Total> 5 calls // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? +is missing coma +console.log(...) in line 9 + // c) Identify all the lines that are variable reassignment statements +carPrice = Number(carPrice.replaceAll(",", "")); in line 4 +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); in line 5 // d) Identify all the lines that are variable declarations +let carPrice = "10,000"; línea 1 +let priceAfterOneYear = "8,543"; línea 2 +const priceDifference = carPrice - priceAfterOneYear; línea 6 +const percentageChange = (priceDifference / carPrice) * 100; línea 7 + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +become a price that is string with comas fixed ready to run and show the % +*/ diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..c58262195 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -11,15 +11,37 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions + // a) How many variable declarations are there in this program? +/* 6 variables +movieLength +remainingSeconds +totalMinutes +remainingMinutes +totalHours +result // b) How many function calls are there? +1 call +console.log(result) // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +% is the modulo operator. +It returns the remainder of dividing movieLength by 60. +In this case: it calculates how many seconds are left over after counting the full minutes. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +First, subtract the remaining seconds to get only the whole seconds in minutes. Then, divide by 60 to convert seconds to whole minutes. + // e) What do you think the variable result represents? Can you think of a better name for this variable? +The result contains a string in the format: hours:minutes:seconds +movieDurationString + + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +It works for positive numbers of seconds. */ diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..010dcad89 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,4 +1,4 @@ -const penceString = "399p"; +/*const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, @@ -25,3 +25,20 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +/* 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); +Use .substring(start, end) to remove the last character (p) from the string. +penceString.length - 1 index of the last character excluding p +3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +Use .padStart(targetLength, "0") to ensure the chain has at least 3 +4. `const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);` + +`Extracts the digits from the pounds.` `paddedPenceNumberString.length - 2` → leaves the last two digits for the pence.` +5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + +.substring(start) takes the last two characters, which represent the penny + +.padEnd(2, "0") ensures there are always two digits, padding with 0 if necessary +6. console.log(£${pounds}.${pence}); + +Builds a string in the standard price format: £pounds.pence + diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js index d61254bd7..005b9d6bf 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js @@ -1,8 +1,15 @@ -function getAngleType(angle) { +/* function getAngleType(angle) { if (angle === 90) return "Right angle"; // replace with your completed function from key-implement -} +} */ + + function getAngleType(angle) { + if (angle === 90) return "Right angle"; + 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"; diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js index b62827b7c..100630c9f 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js @@ -1,8 +1,16 @@ -const getAngleType = require("./1-get-angle-type"); +/*const getAngleType = require("./1-get-angle-type"); test("should identify right angle (90°)", () => { expect(getAngleType(90)).toEqual("Right angle"); -}); +});*/ + + +function getAngleType(angle) { + if (angle === 90) return "Right angle"; + 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"; // REPLACE the comments with the tests // make your test descriptions as clear and readable as possible diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js index 9836fe398..0cefd9622 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js @@ -3,4 +3,16 @@ function isProperFraction(numerator, denominator) { // add your completed function from key-implement here } +module.exports = isProperFraction; + +/* ANSWER*/ +function isProperFraction(numerator, denominator) { + if (numerator < denominator) return true; + if (numerator > denominator) return false; + if (numerator < denominator) return true; + if (numerator === denominator) return false; + if (numerator < denominator) return true; + if (numerator = denominator) return true; +} + module.exports = isProperFraction; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js index ff1cc8173..6323f8492 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js @@ -9,3 +9,26 @@ test("should return true for a proper fraction", () => { // Case 3: Identify Negative Fractions: // Case 4: Identify Equal Numerator and Denominator: + +/*ANSWER*/ +const isProperFraction = require("./2-is-proper-fraction"); + +test("should return true for a proper fraction", () => { + expect(isProperFraction(2, 3)).toEqual(true); +}); + +// Case 2: Identify Improper Fractions: +test("should return false for improper fractions", () => { + expect(isProperFraction(3, 2)).toEqual(false); +}); + +// Case 3: Identify Negative Fractions: +test("should handle negative fractions correctly", () => { + expect(isProperFraction(-2, -3)).toEqual(false); +}); + +// Case 4: Identify Equal Numerator and Denominator: +test("should return false when numerator equals denominator", () => { + expect(isProperFraction(3, 3)).toEqual(false); +}); + diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js index 0d95d3736..43145bacd 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js @@ -2,4 +2,19 @@ function getCardValue(card) { // replace with your code from key-implement return 11; } +module.exports = getCardValue; + +//ANSWER +function getCardValue(card) { + const rank = card.slice(0, -1); // remove the last character (suit emoji) + + if (rank === "A") return 11; + if (["J", "Q", "K", "10"].includes(rank)) return 10; + + const num = parseInt(rank); + if (num >= 2 && num <= 9) return num; + + throw new Error("Invalid card rank."); +} + module.exports = getCardValue; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js index 03a8e2f34..6a48bd7ad 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js @@ -9,3 +9,34 @@ test("should return 11 for Ace of Spades", () => { // Case 3: Handle Face Cards (J, Q, K): // Case 4: Handle Ace (A): // Case 5: Handle Invalid Cards: + +/*answer*/ +const getCardValue = require("./3-get-card-value"); + +test("should return 11 for Ace of Spades", () => { + const aceOfSpades = getCardValue("A♠"); + expect(aceOfSpades).toEqual(11); +}); + +// Case 2: Handle Number Cards (2-10): +test("should return correct value for number cards", () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("10♠")).toEqual(10); +}); + +// Case 3: Handle Face Cards (J, Q, K): +test("should return 10 for face cards", () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♣")).toEqual(10); +}); + +// Case 4: Handle Ace (A): +test("should return 11 for any Ace", () => { + expect(getAngleType("A♥")).toEqual(11); +}); + +// Case 5: Handle Invalid Cards: +test("should throw error for invalid cards", () => { + expect(() => getCardValue("1♠")).toThrow('Invalid card rank.'); +});