Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 12 additions & 2 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
// 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;
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// The function parameter declares the variable string in the first line and then the variable is declared again using let. This will not run because you cannot declare a variable with the same name twice.
// The return returns the same variable making the funtion useless.
// The is no console.log() to print anything.
// =============> write your new code here

function capitalise(str) {
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}
let result = capitalise("str")
console.log(result);
23 changes: 17 additions & 6 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@

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

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

return percentage;
}
// return percentage;
//}

console.log(decimalNumber);
//console.log(decimalNumber);

// =============> write your explanation here
//The function has already declared the decimalNumber variable. The same variable cannot be used or declared twice.
//The decimalNumber variable is only existing inside the the function. Therefore the console.log() will not have anything to print.
//The decimalNumber must be called outside first.


// 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.5));
10 changes: 9 additions & 1 deletion Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// The will be no output printed on the console because there is no code to instructing printing.
// The num variable is not defined and 3 is used instead of num

function square(3) {
return num * num;
}

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

// =============> explain this error message here
// This error illustrates that a number was used instead of a variable name.

// Finally, correct the code to fix the problem

// =============> write your new code here


function square(num) {
squareNum = num * num;
return squareNum;
}
console.log(square(3));
10 changes: 9 additions & 1 deletion Sprint-3/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 run and print the outcoome of (a * b), however it will have 2 outputs.
// The first output is the console.log(a * b) and the second output will execute the console in line 11.

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

// console.log is use to print the instead osf return such that the return value can be called at the end.
// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

9 changes: 8 additions & 1 deletion Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// =============> write your prediction here

// The return and a + b are on 2 different lines leading to a syntax error. The sum fuctions does not work like this.
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +9,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

//The code runs but gives undefined in the console when we call the sum(10, 32). The return function is not defined. We must define it a + b so that when we call it, it give the sum of the 2 values

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Found a typo and should be "... The return value is not defined..." - because precisely speaking, the sum function itself is defined (in line 4) and the return itself is a built-in function (i.e. defined in JavaScript).

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
17 changes: 13 additions & 4 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Predict the output of the following code:
// =============> Write your prediction here

// num is declared as a const variable of 103, this will give the same outcome because num is suppose to a placeholder not a number.
const num = 103;

function getLastDigit() {
Expand All @@ -14,11 +14,20 @@ 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
// The output gives the same answer that is 3

// Explain why the output is the way it is
// =============> write your explanation here
// The variable num was declared to a const number (103) instead of a placeholder therefore it cannot be changed
// Finally, correct the code to fix the problem
// =============> write your new code here
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)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// The const variable must be removed from the code. The computer reads that first and gives the same output for all console.log statements.
// A placeholder variable for num must be declared in the function parameter.
Comment on lines +32 to +33

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think line 33 would be a more accurate explanation. For line 32, even the const num variable is kept in the fixed code, the num.toString() in your function would still use the num in function parameter (when they are the same name). This is the concept of "scope of variable" in JavaScript (and other programming languages as well). So the key issue is "whether the function parameter exist or not", but not quite "whether the global constant variable removed or not".

8 changes: 7 additions & 1 deletion Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
// Then when we call this function with the weight and height
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
//function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
//}
function calculateBMI(weight, height) {
squareHieght = height * height;
calculateBMI = Math.round(((weight / squareHieght)) *10 )/10;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While your program produce the correct result, the ((weight / squareHieght)) used double brackets. This is not quite recommended - especially in socket programming, single pairs of bracket and double pairs of bracket may yield different results.

return calculateBMI
}
console.log(calculateBMI(70, 1.73))
6 changes: 6 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
// 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(str) {
return str.replaceAll(" ", "_").toUpperCase();
}
console.log(toUpperSnakeCase("hello world"))
console.log(toUpperSnakeCase("lord of the rings"))
12 changes: 12 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@
// 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 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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just to clarify a bit, why .padEnd(2, "0") is needed here?

return `${pounds}.${pence}`;
}
console.log(toPounds("399p"))
console.log(toPounds("10p"))
console.log(toPounds("1023p"))
console.log(toPounds("5p"))
Comment on lines +15 to +18

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your program looks generally ok to me. However, have you think of console.log(toPounds("0000000p")); and would it be the output you wanted? Just to give you a thought first and you don't need to fix the issue at this point, because you will learn more about designing test cases in the later module.

9 changes: 7 additions & 2 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 3 times

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

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here

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

// remainingSeconds is 1, the last pad will execute so the function will divide 61 by 60 and then return the modulus or remainder which is 1.
// 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", The pad function recieves the number 1 and converts it to a number string.
// The while loop checks the string length to get 1. 1 is less than 2 therefore the loop executes and adds a 0 to the string giving "01".
// The loop will finishes because 2 is not less than 2. The function returns "01"
38 changes: 21 additions & 17 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
// 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.


function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const timeString = String(time);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just wondering why this line is needed?

let [hours, minutes] = timeString.split(":").map(Number);
if (isNaN(minutes)){
minutes = 0;
}
let period = "am"
if (hours >= 12) {
period = "pm";
if (hours > 12) {
hours = hours - 12;
}
} else if (hours === 0) {
hours = 12;
}
return `${time} am`;
const paddedHours = String(hours).padStart(2, "0");
const paddedMinutes = String(minutes).padStart(2, "0");
return `${paddedHours}:${paddedMinutes}${period}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Based on the original given targetedOutput (which has been deleted in your code), there should be a space before period (e.g. 08:00 am but not 08:00am).

}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.log(formatAs12HourClock("00:30"))
console.log(formatAs12HourClock("23:32"))
console.log(formatAs12HourClock("08:50"))
console.log(formatAs12HourClock("13:00"))
Comment on lines +26 to +29

@hackertainment hackertainment Oct 4, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your test cases have covered am and pm. However, there are still 3 boundary cases which have not been covered yet. Can you think of them and add them too? Thank you.

Loading