Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bafaee8
answered sprint-2 0.js
iteddy16 Jun 13, 2025
ea11210
answered 1-key-errors 1.js
iteddy16 Jun 13, 2025
9b714c8
answered 1-key-errors 2.js
iteddy16 Jun 13, 2025
9b5c5d4
answered 2-mandatory-debug 0.js
iteddy16 Jun 13, 2025
1640e2c
update 0.js
iteddy16 Jun 13, 2025
1537baf
answered 2-mandatory-debug 1.js
iteddy16 Jun 13, 2025
d79a1ca
answered 2-mandatory-debug 2.js
iteddy16 Jun 13, 2025
7a35859
answered 3-mandatory-implement / 1-bmi.js
iteddy16 Jun 13, 2025
6b2be6c
answered 3-mandatory-implement/2-cases.js
iteddy16 Jun 13, 2025
dc3b4e8
update 3-mandatory-implement/3-to-pounds.js
iteddy16 Jun 13, 2025
aa1f8ee
answered 4-mandatory-interpret/time-format.js
iteddy16 Jun 13, 2025
62bba09
update 5-stretch-extend
iteddy16 Jun 13, 2025
95332ed
Updated Sprint-2/ 1.js
iteddy16 Jun 24, 2025
01079c2
Update Sprint-3\2\2-is-proper-fraction.js
iteddy16 Jun 24, 2025
e4425d9
Update 2-is-proper-fraction.test.js
iteddy16 Jun 24, 2025
de2a5de
updated Sprint-3\2\3-get-card-value and test.js
iteddy16 Jun 24, 2025
e3ebe30
update sprint-3\3\count.js and count.test.js
iteddy16 Jun 24, 2025
6e88202
update sprint-3\3\get-ordinal-number.js
iteddy16 Jun 24, 2025
376d990
Add additional test cases for getOrdinalNumber function
iteddy16 Jun 24, 2025
1593416
update Sprint-3\3\repeat.js and repeat.test.js
iteddy16 Jun 24, 2025
ba26a99
Revert "Update Sprint-3\2\2-is-proper-fraction.js"
iteddy16 Jun 24, 2025
9a7b904
Update for review mandatory debug 2.js
iteddy16 Jul 3, 2025
09838ac
updated for review mandatory implement 2-cases.js
iteddy16 Jul 3, 2025
e7ceb40
updates for review mandatory implement 3-to-pound.js
iteddy16 Jul 3, 2025
46a7807
updated for review mandatory-interpret time-format.js
iteddy16 Jul 3, 2025
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
16 changes: 14 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@

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

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

*/
// =============> write your explanation here
// =============>
// The function capitalise is trying to declare a variable `str` using `let` inside the function,
// but `str` is already defined as a parameter of the function. This causes a syntax error because you cannot redeclare a parameter as a variable within the same scope.
// To fix this, you can simply remove the `let` keyword and the redeclaration of `str` inside the function.

// =============> write your new code here
// Here is the corrected function:
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing the same variable name solves this issue. Looks good.

Bear in mind for a more complicated program it could be confusing to reuse the same variables.

return str;
}
// call the function capitalise with a string input
console.log(capitalise("hello world")); // Output: "Hello world"
12 changes: 10 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// =============> write your prediction here

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

/*
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +13,16 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);

*/
// =============> write your explanation here
// The error will occur because the variable `decimalNumber` is declared twice, once as a parameter and once inside the function.
// The second declaration will cause a syntax error because you cannot redeclare a parameter in the same scope.
// The variable `decimalNumber` is declared twice, once as a parameter and once inside the function.

// 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.2));
19 changes: 18 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@

// =============> write your prediction of the error here

// # Prediction: The code will throw a ReferenceError because 'num' is not defined in the function scope.
// # This is because we're trying to use the variable 'num' before it's declared.
// # To fix this, we need to pass the number to be squared as an argument to the function
// # or declare 'num' before using it.
/*
function square(3) {
return num * num;
}

*/
// =============> write the error message here
// # SyntaxError: Unexpected number

// =============> explain this error message here
// # Explanation: The error occurs because the function parameter is incorrectly defined.
// # Instead of using a variable name, a number is used directly in the function definition.
// # This is not a valid way to define a function parameter in JavaScript.
// # To fix this, we need to replace '3' with a valid parameter name, such as 'num'.


// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the parameter is good

return num * num;
}
// Test the corrected function
console.log(square(3)); // Output: 9



8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...

// =============> write your prediction here
// # The code will log the result of multiplying 10 and 32, but it will not return a value from the multiply function.
// # The function multiply will log the product of a and b, but it will not return any value.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +11,12 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// # The function `multiply` logs the product of `a` and `b`, but it does not return a value.
// # This means that when we try to log the result of `multiply(10, 32)`, it will log `undefined` because the function does not return anything.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b; // Change console.log to return
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // This will now log the correct result
9 changes: 9 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// # The function `sum` is expected to return the sum of `a` and `b`, but it will not work as intended.
// # The return statement is placed before the expression, causing the function to return `undefined`.

function sum(a, b) {
return;
Expand All @@ -9,5 +11,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// # The function `sum` has a return statement that is placed before the expression `a + b`, which means it will return `undefined`.
// # As a result, when we try to log the result of `sum(10, 32)`, it will log `undefined` instead of the expected sum.

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b; // Move the return statement to return the sum of a and b
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // This will now log the correct result
20 changes: 19 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

// Predict the output of the following code:
// =============> Write your prediction here
// # The last digit of 42 is 3
// # The last digit of 105 is 3
// # The last digit of 806 is 3

const num = 103;
//const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
Expand All @@ -15,10 +18,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 output is incorrect because the function getLastDigit does not take any parameters.
// # It always returns the last digit of the number 103, which is 3, regardless of the input.
// # The function should take a number as an argument and return its last digit.

// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(num) {
return num.toString().slice(-1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recognising that we need a parameter here was the main exercise, good job.

It would be nice for you to remove the const num = 103 on line 9.
You could test getLastDigit with the numbers from above?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I removed it.
code2

}

// This program should tell the user the last digit of each number.

// Explain why getLastDigit is not working properly - correct the problem
// # The function getLastDigit was not designed to accept an argument, so it always returned the last digit of the hardcoded number 103.
// # The function should accept a number as an argument and return its last digit.
9 changes: 7 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
// 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
}
const bmi = weight / (height * height);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good.

return bmi.toFixed(1); // Round to 1 decimal place
// # Calculate BMI by dividing weight by height squared
// return the BMI of someone based off their weight and height
}
// Example usage:
console.log(`Your BMI is ${calculateBMI(70, 1.73)}`); // Should log: Your BMI is 23.4
10 changes: 10 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@
// 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

function toUpperSnakeCase(input) {
return input
.trim() // Remove leading/trailing whitespace
.replace(/\s+/g, "_") // Replace whitespace sequences with '_'
.toUpperCase(); // Convert to uppercase
}

// Example usage:
console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS"
27 changes: 27 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,30 @@
// 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 to_pound(penceString) {

//const penceString = PenceNumber;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good that you tested this function and also used the return statement.

Perhaps we could use penceString directly as the parameter for this function?
Also be mindful of getting snake case and camel case naming mixed up.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, assigning PenceString as a parameter directly works, and I fixed it in that way.
code3

}
console.log(to_pound("399p")); // Output: £3.99
console.log(to_pound("100p")); // Output: £1.00
console.log(to_pound("50p")); // Output: £0.50
console.log(to_pound("1p")); // Output: £0.01
console.log(to_pound("999p")); // Output: £9.99
console.log(to_pound("0p")); // Output: £0.00
8 changes: 8 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// # 3 times, once for each of totalHours, remainingMinutes, and remainingSeconds

// 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
// 0, which is the value of totalHours when seconds is 61
// (remainingSeconds = 61 % 60 = 1), -> (totalMinutes = (61 - 1) / 60 = 60 / 60 = 1), -> (remainingMinutes = 1 % 60 = 1), -> (totalHours = (1 - 1) / 60 = 0)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// in the function pad(num) if num = 0, 0.toString is "0", "0".padStart(2, "0") is "00" , So the return value of pad(0) is "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
// 01, which is the value of remainingSeconds when seconds is 61. The totalHours is 00, remainingMinutes is 01, and remainingSeconds is 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
// "01", because pad converts the number 1 to a string and pads it with a leading zero to make it two characters long
console.log(formatTimeDisplay(61)); // "00:01:01"
16 changes: 16 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// This is the latest solution to the problem from the prep.
// 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.
// The function should take a time in 24-hour format (HH:MM) and return it in 12-hour format with am/pm.
// For example, "08:00" should return "08:00 am" and "23:00" should return "11:00 pm".
"use strict";
/**
* Converts a time string from 24-hour format (HH:MM) to 12-hour format with am/pm.
* @param {string} time - The time in 24-hour format (e.g., "08:00", "23:00").
* @returns {string} The time in 12-hour format with am/pm.
*/

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
Expand All @@ -23,3 +31,11 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// Additional test cases
const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "00:00 am"; // Midnight should be 12:00 am
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done for completing the extension here.
This solution works for the test cases that you have defined.

A point to consider for this program:

Inputting 12:00 gives 12:00 am as the output, 12:00 in 24 hour time is noon.

Only for reference, here is a tested example that also handles minutes:

function pad(num) {
  //adds leading 0s to string until it reaches length 2
  return num.toString().padStart(2, "0");
}

function formatAs12HourClock(time) {
  if (!/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/.test(time)) { //if string DOESN'T look like HH:MM, min 00:00/max 23:59
    console.log(`You entered "${time}". formatAs12HourClock only works for times written HH:MM. min 00:00, max 23:59.`);
    return;
  }

  let suffix = ""
  let hours = Number(time.slice(0,2)); //convert to number to enable comparisons in if statement
  const mins = time.slice(3);

  if (hours==0) { // if time entered was 00:MM, then it's midnight and the time has just flipped to am
    hours = 12;  // in 12 hours clock, 00 is not allowed, so add 12 => 12:00 am for midnight
    suffix = "am"
  }
  else if (hours<12) { // hours 1 through 11 are am hours
    suffix = "am"
  }
  else if (hours==12) { // at midday, the time flips to pm
    suffix = "pm"
  }
  else {  // hours > 12 => time is in the afternoon/night
    hours=hours-12; 
    suffix = "pm"
  }

  return `${pad(hours)}:${mins} ${suffix}`
}

const testStrings = {
  "08:00":"08:00 am", 
  "23:00":"11:00 pm", 
  "12:00": "12:00 pm",
  "13:15": "01:15 pm",
  "11:11": "11:11 am",
  "03:15": "03:15 am",
  "00:05": "12:05 am",
  "00:00": "12:00 am",
}

for (const [input, output] of Object.entries(testStrings)){
  const currentOutput = formatAs12HourClock(input);
  console.assert(
    output === currentOutput,
    `input: ${input} desired output: ${output}, actual output: ${currentOutput}`
  )
}

11 changes: 11 additions & 0 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@ test("should return true for a proper fraction", () => {
// Case 2: Identify Improper Fractions:

// Case 3: Identify Negative Fractions:
<<<<<<< HEAD
test("should return false for a negative fraction", () => {
expect(isProperFraction(-2, 3)).toEqual(false);
expect(isProperFraction(2, -3)).toEqual(false);
expect(isProperFraction(-2, -3)).toEqual(false); // consider your spec on this one
});



=======
>>>>>>> parent of 01079c2 (Update Sprint-3\2\2-is-proper-fraction.js)

// Case 4: Identify Equal Numerator and Denominator:
16 changes: 14 additions & 2 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
// return 11;

if (card === 'A') {
return 11; // Ace can be 11 or 1, but we'll
}
else if (card === 'K' || card === 'Q' || card === 'J') {
return 10; // Face cards are worth 10
}
else {
return parseInt(card); // Number cards are worth their face value
}

}
module.exports = getCardValue;
console.log(getCardValue('K')); // Should return 11
module.exports = getCardValue
40 changes: 35 additions & 5 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
const getCardValue = require("./3-get-card-value");
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 5 for 5 of Diamonds", () => {
const fiveOfDiamonds = getCardValue("5♦");
expect(fiveOfDiamonds).toEqual(5);
});
test("should return 10 for 10 of Diamonds", () => {
const tenOfDiamonds = getCardValue("10♦");
expect(tenOfDiamonds).toEqual(10);
});

// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for Face Cards", () => {
const faceCards = getCardValue("K♣");
expect(faceCards).toEqual(10);
});

// Case 4: Handle Ace (A):
test("should return 11 for Ace of Spades", () => {
const aceOfSpades = getCardValue("A♠");
expect(aceOfSpades).toEqual(11);
});

// Case 5: Handle Invalid Cards:
test("should return 0 for invalid card '1♠'", () => {
const invalidCard = getCardValue("1♠");
expect(invalidCard).toEqual(0);
});

test("should return 0 for invalid input 'Z♠'", () => {
const invalidInput = getCardValue("Z♠");
expect(invalidInput).toEqual(0);
});

test("should return 0 for empty string", () => {
const emptyString = getCardValue("");
expect(emptyString).toEqual(0);
});
12 changes: 11 additions & 1 deletion Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;
}

console.log(countChar("hello world", "l")); // ➜ 3
console.log(countChar("Mississippi", "s")); // ➜ 4
console.log(countChar("banana", "n"));

module.exports = countChar;
Loading