diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..b8580c502 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -3,4 +3,4 @@ let count = 0; 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 +// Describe what line 3 is doing, in particular focus on what = is doing \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..673e79791 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -8,4 +8,3 @@ let lastName = "Johnson"; let initials = ``; // https://www.google.com/search?q=get+first+character+of+string+mdn - diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..3dff4769d 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -15,9 +15,13 @@ 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(0, lastSlashIndex); + // Create a variable to store the ext part of the variable +const extIndex = base.lastIndexOf("."); +const ext = base.slice(extIndex + 1); -const dir = ; -const ext = ; +console.log(`The directory part is ${dir}`); +console.log(`The extension part is ${ext}`); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..91406cd27 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -6,4 +6,4 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // In this exercise, you will need to work out what num represents? // 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 and running the program several times to build an idea of what the program is doing +// Try logging the value of num and running the program several times to build an idea of what the program is doing \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..69da8d68a 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 +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..43dac0020 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,4 @@ // trying to create an age variable and then reassign the value by 1 const age = 33; -age = age + 1; +age = age + 1; \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..b446a4233 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -2,4 +2,4 @@ // what's the error ? console.log(`I was born in ${cityOfBirth}`); -const cityOfBirth = "Bolton"; +const cityOfBirth = "Bolton"; \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..2278c96a9 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -6,4 +6,4 @@ const last4Digits = cardNumber.slice(-4); // Before running the code, make and explain a prediction about why the code won't work // 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 +// Then try updating the expression last4Digits is assigned to, in order to get the correct value \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..683baac4f 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,2 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const hour12ClockTime = "20:53"; // Changed to start with a letter +const hour24ClockTime = "08:53"; // Also changed for consistency diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..32ea7fc1b 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,22 +1,2 @@ -let carPrice = "10,000"; -let priceAfterOneYear = "8,543"; - -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); - -const priceDifference = carPrice - priceAfterOneYear; -const percentageChange = (priceDifference / carPrice) * 100; - -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 - -// 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? - -// c) Identify all the lines that are variable reassignment statements - -// d) Identify all the lines that are variable declarations - -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// 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 \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..85efdba75 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -22,4 +22,4 @@ console.log(result); // e) What do you think the variable result represents? Can you think of a better name for this variable? -// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..43b8f6448 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,18 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +// 1. const penceString = "399p": initialize a string variable with the value "399p" + +// 3. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length -1); +// cleaning the trailing P character. it uses the string´s length minus 1 as the end index for substring(), this leave only the numeric value while drop the last character. + +// 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStar(3, "0"); Padding- ensure the numeric string is at least three characters long by prepending zeros. +// this is important for separating pounds from pence especially when values is less than 100p + +// 9. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); Extracting pounds component, this uses substring() +// to take all character from the start (index 0) up to, but not including the last two characters, they are always pence. + +// 14. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); Extracting and formatting price- +// Extracts the pence component (the last two digits) and ensures they are always two digits long by padding the end with a zero if needed (though not needed for "99"). + +// 18. console.log(\£${pounds}.${pence}`);` Output: Format the extracted components into the standard currency display £3.99. \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..b0682aecd 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -15,4 +15,4 @@ What effect does calling the `alert` function have? Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. What effect does calling the `prompt` function have? -What is the return value of `prompt`? +What is the return value of `prompt`? \ No newline at end of file diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..c0f324774 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -13,4 +13,4 @@ Try also entering `typeof console` Answer the following questions: What does `console` store? -What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..2445d911f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -10,4 +10,4 @@ function capitalise(str) { } // =============> write your explanation here -// =============> write your new code here +// =============> write your new code here \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..987186f20 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -17,4 +17,4 @@ console.log(decimalNumber); // =============> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here \ 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 aad57f7cf..8db2f38ce 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,14 @@ +// Predict and explain first... -// Predict and explain first BEFORE you run any code... +// =============> write your prediction here -// 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; +function multiply(a, b) { + console.log(a * b); } -// =============> write the error message here +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> explain this error message here +// =============> write your explanation here // Finally, correct the code to fix the problem - -// =============> write your new code here - - +// =============> write your new code here \ 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 37cedfbcf..718468166 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -10,4 +10,4 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here \ 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 57d3f5dc3..fdcc23862 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -21,4 +21,4 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`); // =============> 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 +// Explain why getLastDigit is not working properly - correct the problem \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..d2b705b55 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -6,7 +6,7 @@ // 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. +// Your result will be displayed to 1 decimal place, for example 23.4 // You will need to implement a function that calculates the BMI of someone based off their weight and height @@ -15,5 +15,5 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height +// return the BMI of someone based off their weight and height } \ 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..9327e98c4 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -13,4 +13,4 @@ // 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 +// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/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..99ce20539 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -3,4 +3,4 @@ // 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. -// You should call this function a number of times to check it works for different inputs +// You should call this function a number of times to check it works for different inputs \ 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..600040bfc 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,7 +11,7 @@ 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 +// You will need to play computer with this example - use the Python Visualizer https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions // Questions @@ -31,4 +31,4 @@ function formatTimeDisplay(seconds) { // =============> write your answer here // 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 +// =============> write your answer here \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..2cfcba511 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -22,4 +22,4 @@ const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +); \ No newline at end of file diff --git a/Sprint-3/1-key-implement/1-get-angle-type.js b/Sprint-3/1-key-implement/1-get-angle-type.js index 08d1f0cba..388573697 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-key-implement/1-get-angle-type.js @@ -8,8 +8,8 @@ // Then, write the next test! :) Go through this process until all the cases are implemented function getAngleType(angle) { - if (angle === 90) return "Right angle"; - // read to the end, complete line 36, then pass your test here +if (angle === 90) return "Right angle"; +// read to the end, complete line 36, then pass your test here } // we're going to use this helper function to make our assertions easier to read @@ -53,4 +53,4 @@ const obtuse = getAngleType(120); // 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 diff --git a/Sprint-3/1-key-implement/2-is-proper-fraction.js b/Sprint-3/1-key-implement/2-is-proper-fraction.js index 91583e941..0ec3272df 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-key-implement/2-is-proper-fraction.js @@ -51,3 +51,5 @@ const equalFraction = isProperFraction(3, 3); // Stretch: // What other scenarios could you test for? + + diff --git a/Sprint-3/1-key-implement/3-get-card-value.js b/Sprint-3/1-key-implement/3-get-card-value.js index aa1cc9f90..fb70af0b9 100644 --- a/Sprint-3/1-key-implement/3-get-card-value.js +++ b/Sprint-3/1-key-implement/3-get-card-value.js @@ -49,3 +49,5 @@ const fiveofHearts = getCardValue("5♥"); // 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." + + 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..d6db07a95 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js @@ -1,13 +1,20 @@ function getAngleType(angle) { if (angle === 90) return "Right angle"; - // replace with your completed function from key-implement - + 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"; } +// Don't get bogged down in this detail +// Jest uses Common JS module syntax by default as it's quite old +// We will upgrade our approach to ES6 modules in the next course module, so for now +// we have just written the CommonJS module.exports syntax for you +module.exports = getAngleType; + // replace with your completed function from key-implement - - +} @@ -15,4 +22,5 @@ function getAngleType(angle) { // Jest uses CommonJS module syntax by default as it's quite old // We will upgrade our approach to ES6 modules in the next course module, so for now // we have just written the CommonJS module.exports syntax for you -module.exports = getAngleType; \ No newline at end of file +module.exports = getAngleType; + 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..61335b5b2 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 @@ -10,15 +10,33 @@ test("should identify right angle (90°)", () => { // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" +test("should identify acute angle (<90°)", () => { + expect(getAngleType(45)).toEqual("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" +test("should identify obtuse angle (>90° and <180°)", () => { + expect(getAngleType(120)).toEqual("Obtuse angle"); +}); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" +test("should identify straight angle (180°)", () => { + expect(getAngleType(180)).toEqual("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" +test("should identify reflex angle (>180° and <360°)", () => { + expect(getAngleType(270)).toEqual("Reflex angle"); +}); + + + + + 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..46ddce752 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js @@ -1,6 +1,11 @@ function isProperFraction(numerator, denominator) { if (numerator < denominator) return true; // add your completed function from key-implement here + if (numerator > denominator) return false; + if (numerator <= 0 || denominator <= 0) return false; + if (numerator === denominator) return false; + return false; + } -module.exports = isProperFraction; \ No newline at end of file +module.exports = isProperFraction; 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..64aa001c4 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 @@ -4,8 +4,20 @@ test("should return true for a proper fraction", () => { expect(isProperFraction(2, 3)).toEqual(true); }); -// Case 2: Identify Improper Fractions: +// Case 2: Identify Improper Fractions: +test("should return false for an improper fraction", () => { + expect(isProperFraction(5, 4)).toEqual(false); +}); + + // Case 3: Identify Negative Fractions: +test("should return false for a negative fraction", () => { + expect(isProperFraction(-2, 3)).toEqual(true); +}); + // Case 4: Identify Equal Numerator and Denominator: +test("should return false when numerator equals denominator", () => { + expect(isProperFraction(4, 4)).toEqual(false); +}); \ No newline at end of file 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..d9647ff2a 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js @@ -1,5 +1,23 @@ -function getCardValue(card) { - // replace with your code from key-implement - return 11; -} -module.exports = getCardValue; \ No newline at end of file +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 an improper fraction", () => { + expect(isProperFraction(5, 4)).toEqual(false); +}); + + + +// Case 3: Identify Negative Fractions: +test("should return false for a negative fraction", () => { + expect(isProperFraction(-2, 3)).toEqual(true); +}); + + +// Case 4: Identify Equal Numerator and Denominator: +test("should return false when numerator equals denominator", () => { + expect(isProperFraction(4, 4)).toEqual(false); +}); 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..7f1d382e0 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 @@ -6,6 +6,24 @@ test("should return 11 for Ace of Spades", () => { }); // Case 2: Handle Number Cards (2-10): +test("should return 7 for Seven of Hearts", () => { + const sevenOfHearts = getCardValue("7♥"); + expect(sevenOfHearts).toEqual(7); + }); // Case 3: Handle Face Cards (J, Q, K): +test("should return 10 for King of Diamonds", () => { + const kingOfDiamonds = getCardValue("K♦"); + expect(kingOfDiamonds).toEqual(10); + }); // Case 4: Handle Ace (A): -// Case 5: Handle Invalid Cards: +test("should return 11 for Ace of Clubs", () => { + const aceOfClubs = getCardValue("A♣"); + expect(aceOfClubs).toEqual(11); + }); + +// Case 5: Handle Invalid Cards: +test("should throw an error for invalid card rank", () => { + expect(() => { + getCardValue("1♠"); + }).toThrow("Invalid card rank."); + }); \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..b65bca97f 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/count.js @@ -1,5 +1,22 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 -} + let count = 0; + let arrOfChars = stringOfCharacters.split(""); + console.log(arrOfChars) + + arrOfChars.forEach(char => { + if (char === findCharacter) count++ + + }) + + return count; + + + }; + + + console.log( + countChar("abcdefg", "c")); + + module.exports = countChar; \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/count.test.js b/Sprint-3/3-mandatory-practice/implement/count.test.js index 42baf4b4b..78ddb840f 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.test.js +++ b/Sprint-3/3-mandatory-practice/implement/count.test.js @@ -17,8 +17,24 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); + + + // Scenario: No Occurrences // Given the input string str, // And a character char that does not exist within the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. +test("should return 0 when character does not exist in string", () => { + const str = "Joseph"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + + +module.exports = countChar; + + + + diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js index 24f528b0d..980adbcf5 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js +++ b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js @@ -1,5 +1,17 @@ function getOrdinalNumber(num) { - return "1st"; + const j = num % 10; + const k = num % 100; + if (j === 1 && k !== 11) { + return num + "st"; + } + if (j === 2 && k !== 12) { + return num + "nd"; + } + if (j === 3 && k !== 13) { + return num + "rd"; + } + return num + "th"; } -module.exports = getOrdinalNumber; \ No newline at end of file +module.exports = getOrdinalNumber; + diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js index 6d55dfbb4..a4056202a 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js +++ b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js @@ -11,3 +11,22 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + }); + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +test("should return '11th', '12th', and '13th' for teens", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}); + diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js index 621f9bd35..1a563ba1a 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -1,5 +1,12 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (count < 0) throw new Error("Count must be non-negative"); + + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } -module.exports = repeat; \ No newline at end of file +module.exports = repeat; + diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.test.js b/Sprint-3/3-mandatory-practice/implement/repeat.test.js index 8a4ab42ef..4bfaefa19 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -21,12 +21,28 @@ test("should repeat the string count times", () => { // When the repeat function is called with these inputs, // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. +test("should return the original string when count is 1", () => { + expect(repeat("hello", 1)).toEqual("hello"); +}); + + // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat function is called with these inputs, // Then it should return an empty string, ensuring that a count of 0 results in an empty output. +test("should return an empty string when count is 0", () => { + expect(repeat("hello", 0)).toEqual(""); +}); + // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. + +test("should throw an error when count is negative", () => { + expect(() => repeat("hello", -1)).toThrow("Count must be non-negative"); +}); + + + diff --git a/Sprint-3/4-stretch-investigate/card-validator.md b/Sprint-3/4-stretch-investigate/card-validator.md index e39c6ace6..42be5e453 100644 --- a/Sprint-3/4-stretch-investigate/card-validator.md +++ b/Sprint-3/4-stretch-investigate/card-validator.md @@ -30,6 +30,7 @@ These are the requirements your project needs to fulfill: - Make a JavaScript file with a name that describes its contents. - Create a function with a descriptive name which makes it clear what the function does. The function should take one argument, the credit card number to validate. - Write at least 2 comments that explain to others what a line of code is meant to do. -- Return a boolean from the function to indicate whether the credit card number is valid. +- Return a boolean from the function to indicate whether the credit card number is valid; + + -Good luck! diff --git a/Sprint-3/4-stretch-investigate/find.js b/Sprint-3/4-stretch-investigate/find.js index c7e79a2f2..9aa1242b9 100644 --- a/Sprint-3/4-stretch-investigate/find.js +++ b/Sprint-3/4-stretch-investigate/find.js @@ -10,9 +10,12 @@ function find(str, char) { return -1; } + console.log(find("code your future", "u")); console.log(find("code your future", "z")); + + // The while loop statement allows us to do iteration - the repetition of a certain number of tasks according to some condition // See the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while @@ -20,6 +23,18 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +//// The index variable starts at 0 (the beginning of the string). After every unsuccessful check in the if statement, index++ adds 1 to the value. +// This allows the function to move sequentially from left to right through the string (0, 1, 2, 3...). + // b) What is the if statement used to check +//The if statement checks for equality. It looks at the character currently stored at str[index] and compares it to the char argument. +// If they are an exact match, the function stops immediately and returns the current position. + // c) Why is index++ being used? + //This is the "iterator." Without index++, the loop would be stuck on index 0 forever because the condition index < str.length would always stay true. +// This would cause an infinite loop, likely crashing your browser or terminal. + // d) What is the condition index < str.length used for? +// This is the boundary condition. It ensures the loop only runs while there are still characters left to check. +// Once index reaches the length of the string, it means we have checked every character and should stop to avoid looking for "ghost" characters that don't exist. + diff --git a/Sprint-3/4-stretch-investigate/password-validator.js b/Sprint-3/4-stretch-investigate/password-validator.js index b55d527db..cb7381e2f 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.js +++ b/Sprint-3/4-stretch-investigate/password-validator.js @@ -2,5 +2,16 @@ function passwordValidator(password) { return password.length < 5 ? false : true } +function isValidPassword(password, previousPasswords = []) { + if (password.length < 5) return false; + if (!/[A-Z]/.test(password)) return false; + if (!/[a-z]/.test(password)) return false; + if (!/[0-9]/.test(password)) return false; + if (!/[^A-Za-z0-9]/.test(password)) return false; + if (previousPasswords.includes(password)) return false; + return true; +} + + +module.exports = passwordValidator; -module.exports = passwordValidator; \ No newline at end of file diff --git a/Sprint-3/4-stretch-investigate/password-validator.test.js b/Sprint-3/4-stretch-investigate/password-validator.test.js index 8fa3089d6..3108b3473 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.test.js +++ b/Sprint-3/4-stretch-investigate/password-validator.test.js @@ -14,13 +14,65 @@ To be valid, a password must: You must breakdown this problem in order to solve it. Find one test case first and get that working */ + const isValidPassword = require("./password-validator"); test("password has at least 5 characters", () => { - // Arrange const password = "12345"; - // Act const result = isValidPassword(password); - // Assert expect(result).toEqual(true); } -); \ No newline at end of file +); + +test("password has at least one uppercase letter", () => { + const password = "abcD1!"; + const result = isValidPassword(password); + expect(result).toEqual(true); +} +); + +test("password has at least one lowercase letter", () => { + const password = "ABCd1!"; + const result = isValidPassword(password); + expect(result).toEqual(true); +} +); + +test("password has at least one number", () => { + const password = "Abcde!2"; + const result = isValidPassword(password); + expect(result).toEqual(true); +} +); + +test("password has at least one non-alphanumeric symbol", () => { + const password = "Abcde1"; + const result = isValidPassword(password); + expect(result).toEqual(true); +} +); + +test("password is not in the previous passwords array", () => { + const password = "Password1!"; + const previousPasswords = ["Password1!", "Password2@", "Password3#"]; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +} +); + +test("valid password meets all criteria", () => { + const password = "Valid1!"; + const previousPasswords = ["Password1!", "Password2@", "Password3#"]; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +} +); +test("invalid password fails multiple criteria", () => { + const password = "inv"; + const previousPasswords = ["inv", "Password2@", "Password3#"]; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(false); +} +); + + +