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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.DS_Store
.vscode
testing.js --- IGNORE ---
**/.DS_Store
19 changes: 15 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here
// The code has a function that is supposed to capitalise the string passed to it. (only the first letter)
// the string will be stored in the variable str and the function will return its value.
// it takes the first letter (str[0]) and uses toUpperCase() method to capitalise it.
// the str.slice(1) method is used to show the letter.
//

// 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
// an error occured because the variable str was already declared as a parameter of the function so
// there was no need to use let again.


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

console.log(capitalise("hello"));
16 changes: 16 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Predict and explain first...
// the code is supposed to convert a decimal number to percentage by multiplying
// the decimal Number by 100 then add the sign % to show that it is a percentage

// Why will an error occur when this program runs?
// =============> write your prediction here
// an error will occur because the variable decimalNumber is redeclared inside the function.
// it's already declared as a parameter of the function, so redeclaring it with const causes a syntax error.

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

Expand All @@ -15,6 +19,18 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// the fucntion convertToPercentage has a parameter named decimalNumber,
// but inside the function, there is a line that tries to declare a new constant with the same name decimalNumber.
// there is no need to redeclare the variable or edit it since it is a user input.
// also the variable decimalNumber is called from outside the function which menas from another scope.
// that is wrong because the variable can only be used inside the fucntion. instead of that we need to call the funtion, not hte variable.

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

// =============> write your prediction of the error here
// the error will occur because the parameter name is a number (3) which is not allowed.
// it should be a variable or empty.

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

// =============> write the error message here

// SyntaxError: Unexpected number
// at internalCompileFunction (node:internal/vm:73:18)
// at wrapSafe (node:internal/modules/cjs/loader:1274:20)
// at Module._compile (node:internal/modules/cjs/loader:1320:27)
// at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
// at Module.load (node:internal/modules/cjs/loader:1197:32)
// at Module._load (node:internal/modules/cjs/loader:1013:12)
// at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
// at node:internal/main/run_main_module:28:49

// Node.js v18.19.1
// =============> explain this error message here

// ther error message says that a number can not put as an input when declaring the function
// Finally, correct the code to fix the problem

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


console.log(square(5));
10 changes: 9 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Predict and explain first...

// =============> write your prediction here
// this code will use the declared function multiplay (a, b) to multiply two numbers, 10 and 32, and print the result to the console.
// However, the function can not be used to get the result because it does not return any value. Instead, it only logs the result to the console.

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

// =============> write your explanation here
// the function does not return any value, it only prints the result to the console, so it can't be used to return anything, it will return "undefined".

// 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)}`);
10 changes: 10 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 will throw an error because it does not return any value, the use of return without passing any value will cause an error.
// also the last line of the function is unreachable because it comes after the return statement.

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

// =============> write your explanation here
// The function will not return the expected sum because the return statement is not followed by any value.

// 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)}`);
26 changes: 26 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// Predict and explain first...
// the code should display the last digit of any number passed to the function.
// but there is an error which is the variable num is globally declared and has a value of 103.
// the function does not take any parameters, and it takes the global variable num which is always 103.
// that causes the code to always return 3 as 3 is the last digit of 103 which is the value of the global variable num.

// Predict the output of the following code:
// =============> Write your prediction here
// since 3 is the last digit of 103, I think the output will be:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

const num = 103;

Expand All @@ -14,11 +22,29 @@ 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
// The last digit of 42 is 3 ... the output is as I predicted.

// =============> 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
// because the function getLastDigit does not take any parameters, and it uses the global variable num which is always 103.
// Finally, correct the code to fix the problem
// =============> write your new code here

// no need to declare the global variable num here. instead, we will pass it as a parameter to the function.
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
// because the function does not take the parameter so it will always use the variable num which is always 103.
9 changes: 8 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
let squaredHeight = height * height;
let BMI = weight/squaredHeight;
BMI = BMI.toFixed(1);
return Number(BMI);
}

console.log(calculateBMI(70, 1.73));
//console.log(calculateBMI(70, 1.73) + 5);
17 changes: 17 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,20 @@
// 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(string)
{
let output = '';
for(let i = 0; i < string.length; i++)
{
let char = string[i];
if(char === " ")
{
char = "_";
}
output += char;
}
return output.toUpperCase();
}

console.log(toUpperSnakeCase("Hello there, How are you doing guys?"));
26 changes: 26 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,29 @@
// 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");

return (`£${pounds}.${pence}`);
}

console.log(toPounds("5764p"));
console.log(toPounds("813p"));
console.log(toPounds("90897867564p"));
console.log(toPounds("71p"));
console.log(toPounds("312987p"));
7 changes: 7 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61));
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// The function pad will be called 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
// the value is 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// # The Python visualizer stops working when it runs pad because (padStart is not a function), maybe because it uses ES6 version of javascript!
// The return value of pad when it's called for the first time 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
//the value assigned to num is 1 . the value 1 is the number of the remaining seconds, pad() converted it to 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
// the return value is 01 . the function pad() converted num (which values 1) to 01 so the seconds can be readable easily.
48 changes: 37 additions & 11 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,49 @@
// 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`;
let hours = time.slice(0, 2);
let minutes= time.slice(3, 5);

if (Number(hours) > 12) {
hours = String(hours - 12);
hours = hours.padStart(2, "0");

return `${hours}:${minutes} pm`;
}
return `${time} am`;
return `${hours}:${minutes} am`;
}

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

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
//==========================
currentOutput = formatAs12HourClock("23:00");
targetOutput = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
//==========================
currentOutput = formatAs12HourClock("07:51");
targetOutput = "07:51 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
//==========================
currentOutput = formatAs12HourClock("00:37");
targetOutput = "00:37 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
//==========================
currentOutput = formatAs12HourClock("20:59");
targetOutput = "08:59 pm";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
23 changes: 23 additions & 0 deletions testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function formatAs12HourClock(time) {
if (Number(time.slice(0, 2)) > 12)
{
return `${Number(time.slice(0, 2) - 12)}:00 pm`;
}
return `${time} am`;
}

let targetOutPut = "2:00 pm";
let currentOutPut= formatAs12HourClock("14:00");

console.assert(
targetOutPut === currentOutPut,
`current output: ${currentOutPut}, target output: ${targetOutPut}`
);
// =========
targetOutPut = "11:00 am";
currentOutPut= formatAs12HourClock("11:00");

console.assert(
currentOutPut === targetOutPut,
`Error,, current is ${currentOutPut} and target is ${targetOutPut}`
);