Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
d7431d7
Complete task for Sprint-2 cleanly
KhotKeys Jul 5, 2026
dd09c04
Restore original questions alongside answers in Sprint-2 files; fix c…
KhotKeys Jul 20, 2026
7e816c1
Implement getAngleType function with all angle cases
KhotKeys Jul 20, 2026
b719055
Implement isProperFraction function with all cases
KhotKeys Jul 20, 2026
e3b82d7
Implement getCardValue function with all rank and suit cases
KhotKeys Jul 20, 2026
80f67ba
Add Jest tests for getAngleType covering all angle types and invalid …
KhotKeys Jul 20, 2026
8f5030b
Add Jest tests for isProperFraction covering positive, negative and z…
KhotKeys Jul 20, 2026
8f3546d
Add Jest tests for getCardValue covering ace, number cards, face card…
KhotKeys Jul 20, 2026
6e742e7
Implement countChar and add tests for multiple and zero occurrences
KhotKeys Jul 20, 2026
8f83aef
Implement repeatStr and add tests for repeat, count 1, count 0 and ne…
KhotKeys Jul 20, 2026
fe2c450
Implement getOrdinalNumber and add tests for st, nd, rd, th including…
KhotKeys Jul 20, 2026
285cdb3
Remove dead code from exercise-1: unused variable and unreachable con…
KhotKeys Jul 20, 2026
202642d
Remove dead code from exercise-2: unused capitalisedPets array and lo…
KhotKeys Jul 20, 2026
85e4ce4
Answer all questions about the find while loop in find.js
KhotKeys Jul 20, 2026
4279ba8
Implement passwordValidator with all 6 rules and add tests for each rule
KhotKeys Jul 20, 2026
cd2697a
Create card validator with all 4 rules and tests for valid and invali…
KhotKeys Jul 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: SyntaxError because `str` is re-declared with `let` inside the function
// where it already exists as a parameter. You cannot use `let` to re-declare a variable
// that already exists in the same scope.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

// Original broken code:
// function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }

// Explanation: The parameter `str` already exists in the function scope.
// Using `let str` tries to re-declare it, which is a SyntaxError.
// Instead of reassigning `str` (which represents the original input),
// we use a new const `capitalisedStr` to make it clear this is a different value.

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
const capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}

// =============> write your explanation here
// =============> write your new code here
26 changes: 17 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// Prediction: Two errors — (1) re-declaring decimalNumber with const inside the function
// when it is already a parameter causes a SyntaxError.
// (2) console.log(decimalNumber) outside the function will throw ReferenceError because
// decimalNumber is not defined in the outer scope.

// Try playing computer with the example to work out what is going on

// Original broken code:
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;
// return percentage;
// }
// console.log(decimalNumber);

// Explanation: The parameter decimalNumber already exists in the function scope;
// const re-declaration is illegal. Also, decimalNumber is not accessible outside the function.
// Fix: remove the const re-declaration inside the function, and pass a value when calling it.

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here

// Finally, correct the code to fix the problem
// =============> write your new code here
console.log(convertToPercentage(0.5));
25 changes: 12 additions & 13 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

// 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;
}

// =============> write the error message here
// Prediction: SyntaxError — a function parameter must be a valid identifier, not a number literal.

// =============> explain this error message here

// Finally, correct the code to fix the problem

// =============> write your new code here
// Original broken code:
// function square(3) {
// return num * num;
// }

// Error message: SyntaxError: Unexpected number
// Explanation: `3` is a number literal and cannot be used as a parameter name.
// Parameters must be valid identifiers (variable names).
// Fix: use a named parameter like `num`.

function square(num) {
return num * num;
}
19 changes: 12 additions & 7 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...
// Prediction: The template literal will print "undefined" for the function result because
// multiply uses console.log internally but does not return a value (returns undefined).

// =============> write your prediction here
// Original broken code:
// function multiply(a, b) {
// console.log(a * b);
// }
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// Explanation: Functions without a return statement return undefined.
// The outer console.log then interpolates undefined into the string.
// Fix: replace console.log inside multiply with a return statement.

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

// Finally, correct the code to fix the problem
// =============> write your new code here
21 changes: 14 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: The sum will print "undefined" because return; exits the function
// immediately without a value, so a + b is never evaluated.

// Original broken code:
// function sum(a, b) {
// return;
// a + b;
// }
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// Explanation: return; with no expression returns undefined.
// The a + b on the next line is unreachable dead code and is never executed.
// Fix: put a + b on the same line as return.

function sum(a, b) {
return;
a + b;
return a + b;
}

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
31 changes: 18 additions & 13 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// Prediction: All three logs will print "3" (the last digit of the module-level const num = 103)
// because getLastDigit ignores its parameter and always uses the outer num.
// Expected output:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

const num = 103;
// Original broken code:
// const num = 103;
// function getLastDigit() {
// return num.toString().slice(-1);
// }

function getLastDigit() {
// Actual output matched prediction — all printed "3".

// Explanation: The function has no parameter, so any argument passed in is discarded.
// It always reads the outer `num` variable which is 103.
// Fix: add a parameter to getLastDigit and use it instead of the outer variable.

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)}`);

// 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

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
2 changes: 1 addition & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 Number((weight / (height * height)).toFixed(1));
}
20 changes: 5 additions & 15 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
// A set of words can be grouped together in different cases.
function toUpperSnakeCase(str) {
return str.replaceAll(" ", "_").toUpperCase();
}

// 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.

// Implement a function that:

// 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"

// Another example: "lord of the rings" should be "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.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
16 changes: 11 additions & 5 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// In Sprint-1, there is a program written in interpret/to-pounds.js
function toPounds(penceString) {
const withoutP = penceString.substring(0, penceString.length - 1);
const padded = withoutP.padStart(3, "0");
const pounds = padded.substring(0, padded.length - 2);
const pence = padded.substring(padded.length - 2).padEnd(2, "0");
return `£${pounds}.${pence}`;
}

// 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
console.log(toPounds("399p")); // £3.99
console.log(toPounds("9p")); // £0.09
console.log(toPounds("50p")); // £0.50
console.log(toPounds("1000p")); // £10.00
13 changes: 7 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> pad is called 3 times per call to formatTimeDisplay (once for totalHours, once for remainingMinutes, once for remainingSeconds).

// Call formatTimeDisplay with an input of 61, now answer the following:
// (remainingSeconds = 1, totalMinutes = 1, remainingMinutes = 1, totalHours = 0)

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// =============> num = 0 (totalHours), because totalHours is the first argument in the template literal.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// c) What is the return value of pad when it is called for the first time?
// =============> "00" — "0".length < 2, so "0" is prepended, giving "00".

// 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
// =============> num = 1 (remainingSeconds). It is the last argument passed in the template literal, so pad is called with it last.

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> "01" — "1".length < 2, so "0" is prepended, giving "01".
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) return "Invalid angle";
if (angle === 90) return "Right angle";
if (angle === 180) return "Straight angle";
if (angle < 90) return "Acute angle";
if (angle < 180) return "Obtuse angle";
return "Reflex angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -31,7 +36,25 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
// Tests covering all cases
const right = getAngleType(90);
assertEquals(right, "Right angle");

assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(89), "Acute angle");

assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(135), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");

assertEquals(getAngleType(180), "Straight angle");

assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(270), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");

assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(400), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@

// Assumption: The parameters are valid numbers (not NaN or Infinity).

// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
// Note: A proper fraction is one where the absolute value of the numerator
// is strictly less than the absolute value of the denominator.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) return false;
return Math.abs(numerator) < Math.abs(denominator);
}

// The line below allows us to load the isProperFraction function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = isProperFraction;

// Here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
// Tests covering all cases
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 1), false); // numerator > denominator
assertEquals(isProperFraction(3, 3), false); // equal -> not proper
assertEquals(isProperFraction(-1, 2), true); // negative numerator
assertEquals(isProperFraction(1, -2), true); // negative denominator
assertEquals(isProperFraction(1, 0), false); // zero denominator
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const suit = card.slice(-1);
const rank = card.slice(0, -1);
const validSuits = ["\u2660", "\u2665", "\u2666", "\u2663"];
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error(`Invalid card: ${card}`);
}
if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;
return Number(rank);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,6 +50,13 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("2♦"), 2);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♦"), 10);

// Handling invalid cards
try {
Expand Down
Loading
Loading