Skip to content
Open
21 changes: 18 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> I think that this is a function in order to capitalize the first letter of any string defined by the variable str

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +9,20 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
// =============> I get the following error message:
// SyntaxError: Identifier 'str' has already been declared
// at wrapSafe (node:internal/modules/cjs/loader:1691:18)
// at Module._compile (node:internal/modules/cjs/loader:1734:20)
// at Object..js (node:internal/modules/cjs/loader:1893:10)
// at Module.load (node:internal/modules/cjs/loader:1480:32)
// at Module._load (node:internal/modules/cjs/loader:1299:12)
// at TracingChannel.traceSync (node:diagnostics_channel:328:14)
// at wrapModuleLoad (node:internal/modules/cjs/loader:244:24)
// at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
// at node:internal/main/run_main_module:33:47
//I realize that by adding a parameter in the function capitalize IS actually declaring the variable str, so we can't redeclare it below with let. I will remove the let and check again.

function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
13 changes: 9 additions & 4 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> We have similar case as before, decimalNumber has been redeclared. If we remove the const in row 9 the console.log will show 0.5

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

Expand All @@ -14,7 +14,12 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============> I was wrong, if we just remove the const it gives an error that decimalNumber is not defined. So I took it out of the function and declare it before the function.
//And I loged the function in the console instead.

// Finally, correct the code to fix the problem
// =============> write your new code here
const decimalNumber = 0.5;
function convertToPercentage(decimalNumber){
const percentage = `${decimalNumber*100}%`;
return percentage;
}
console.log(convertToPercentage(decimalNumber));
24 changes: 18 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@

// this function should square any number but instead we're going to get an error

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

// =============> The num is not defined.
function square(3) {
return num * num;
}

// =============> write the error message here
// =============>SyntaxError: Unexpected number
// at wrapSafe (node:internal/modules/cjs/loader:1691:18)
// at Module._compile (node:internal/modules/cjs/loader:1734:20)
// at Object..js (node:internal/modules/cjs/loader:1893:10)
// at Module.load (node:internal/modules/cjs/loader:1480:32)
// at Module._load (node:internal/modules/cjs/loader:1299:12)
// at TracingChannel.traceSync (node:diagnostics_channel:328:14)
// at wrapModuleLoad (node:internal/modules/cjs/loader:244:24)
// at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
// at node:internal/main/run_main_module:33:47

// =============> explain this error message here
//Node.js v24.10.0

// Finally, correct the code to fix the problem
// =============> Number 3 was unexpected number where it supposed to be a parameter num

// =============> write your new code here
// Finally, correct the code to fix the problem
function square(num) {
return num * num;
}
console.log(square(5)); //It returned 25 so it works


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

// =============> write your prediction here
// =============>I think that instead of console.log(a*b) we should ask for return(a*b). I think we will get an undifined error for a and b (but I am not completely sure because a and b are parameters.)

function multiply(a, b) {
console.log(a * b);
}

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

// =============> write your explanation here
// =============> Well my prediction was right partially. There should have been a return (a*b) in the function instead of console.log.
//What I couldn't understand is that I did receive a result of 320 and then "The result of multiplying 10 and 32 is undefined".
//So, it took the numbers 10 and 32 to define the parameters from out of the function but then because there is no return it showed undefined? I would like to analyze this in class.
//I will try more to play with the code.

// 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)}`);
11 changes: 8 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// =============> I think we will have an undefined error because return is empty and under it is a+b without definition. Idk if again it will take the definition from after the function
//logging and return a summary, I will test it.

function sum(a, b) {
return;
Expand All @@ -8,6 +9,10 @@ function sum(a, b) {

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

// =============> write your explanation here
// =============> So as per the prediction a+b are not defined neither returned. And logging them out of the function gave us an error of undefined.
// 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)}`);
13 changes: 9 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> I think the below will give an error of predefined as num is already defined with const therefore it can't be redifined with other values.

const num = 103;

Expand All @@ -14,11 +14,16 @@ 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
// =============> I was wrong it does work but always gives as a result the last digit of 103 which is 3, as 103 is the only value of the num variable.
// 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
//I removed the definition of the variable out of the function and declare it as a parameter within () in the function.
7 changes: 5 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,8 @@
// 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
}
let bmi = weight/(height*height);
return Math.round(bmi*10)/10;
}

console.log(calculateBMI(70, 1.73));
9 changes: 9 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@
// 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
//I will name my function upperSnake.
//I will first replace all spaces with underscores and then return everything in upper case letters.
function upperSnake(str) {
str = str.replaceAll(" ", "_");
return str.toUpperCase();
}

console.log(upperSnake("Hello World"));

// 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
18 changes: 18 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,21 @@
// 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) { //I made penceString a parameter rather than a const so I can reuse with the function.
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}`;
} //Kept all the rest variables as const since they can apply in all cases. Used return instead of loging in the console, to have a result in my function

console.log(toPounds("40p")); //Tested my function with different values of panceString
10 changes: 5 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 times, one for hour one for minutes one for seconds.

// 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 as totalHours value is 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 00 (represents hours)

// 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
// =============> 1 because the remaining seconds of 61 is 1.

// 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 num when pad is called for the last time is actually the remaining seconds padded)
25 changes: 17 additions & 8 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// 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.
//My thought is to treat everything as a string except treat hours as a number when i need comparisons in the if statements. Then return everything as a string again.

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
const colonIndex = time.indexOf(":");
const hours = time.slice(0, colonIndex);
const minutes = time.slice(colonIndex + 1);
//I will only use numbers for the comparisons
const hoursNum = Number(hours);

if (hoursNum === 0) {
return `12:${minutes} am`;
} else if (hours === 12) {
return `${hours}:${minutes} pm`;
} else if (hoursNum < 12) {
return `${hours.padStart(2, "0")}:${minutes} am`; //Covering the possibility that hours in the time are given as a single digit ie 8:23. So pad is making this 08 instead of 8
} else return `${String(hoursNum - 12).padStart(2, "0")}:${minutes} pm`; // Same here pad is used to ensure the two digits outcome
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
const currentOutput = formatAs12HourClock("8:23"); //I tested it with 0:00 and 00:00 also
const targetOutput = "08:23 am";
console.assert(
currentOutput === targetOutput,
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

Expand Down