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
18 changes: 14 additions & 4 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

// =============> I presume the str is repeatedly declared, first as a parameter and again as a variable.
// 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
// =============> write your new code here

// =============> str is already declared in the parameter and shouldn't be declared inside the function. The error says it's been declared already for this reason. Therefore we need to rename the str inside the function.
/* ==========> function capitalise(str) {
let result = `${str[0].toUpperCase()}${str.slice(1)};
return result;
}
OR
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
*/

22 changes: 18 additions & 4 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> The error will appear because we are trying to log the variable decimalNumber in the function. We have to call the function at this point. Besides to that, the decimalNumber is again declared inside the function where it shouldn't be.


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


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


return percentage;
}


console.log(decimalNumber);
// =============> it says decimalNumber has aleady been declared for the above reason.

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

// Finally, correct the code to fix the problem
// =============> write your new code here
/* ===========> function convertToPercentage(decimalNumber) {
let decimalNum = 0.5;
const percentage = `${decimalNum * 100}%`;
return percentage;
}
const result = convertToPercentage();
console.log(result);
*/


20 changes: 15 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@

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

// =============> It is going to throw an error message as 3 is written in incorrect position. That position is set only for parameters, not arguments.


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

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

// =============> explain this error message here
// =============> in the node repl, it says unexpected number, as the function always expects a parameter to be declared in its definition.


// =============> The error is already described above.


// Finally, correct the code to fix the problem

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

/* ===========> function square(num) {
return num * num;
}
*/



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


// =============> write your prediction here


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

// =============> The function is only printing values on the console but not returning the value to the caller. Henceforth, it outputs both 320 and undefined on the console. 320 is the result of the console.log(a * b) inside the function, whereas undefined comes from the outer console.log(...) as it tries to bring forth and print the result of the function multiply(a, b). But the function is not returning any value and hence undefined appears on the console.


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

16 changes: 11 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// =============> As the function is not retuning anything, we're going to see undefined on the console.


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

// =============> The function uses parameters a and b. It calculates the addition of a and b. Supposedly it should return the result and then we can use the value of the function, but it is returning nothing at all as return is closed without being specified.
// 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 ${(10, 32)}`);
*/
25 changes: 20 additions & 5 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
// Predict and explain first...


// Predict the output of the following code:
// =============> Write your prediction here
// =============> the function is going to return error as the /1 is not valid expression or syntax


const num = 103;

function getLastDigit() {

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)}`);
console.log(`The last digit of 7247 is ${getLastDigit(7274)}`);


// Now run the code and compare the output to your prediction
// =============> write the output here
/* ===========> function getLastDigit() {
return num.toString().slice(-1);
*/
// Explain why the output is the way it is
// =============> write your explanation here
// =============> this is because the /1 is not defined and not valid in the system of javaScript languague, so it throws an error. Morever because the parameter for the function is not defined, the function takes the global variable scope to run the code inside it. So obviously, we need to put down a parameter which is num to have the function work for any number other than the global variable scope.
// Finally, correct the code to fix the problem
// =============> write your new code here
/* ============> function getLastDigit(num) {
return num.toString().slice(-1);
}
*/


// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem



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
}
const bmi = weight / (height * height);
return Math.round(bmi * 10) / 10;
}

console.log(`The BMI of Yonatan is ${calculateBMI(65, 1.65)}`);
4 changes: 4 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,7 @@
// 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 snakeCaseString(str) {
return str.split(" ").join("_").toUpperCase();
}
console.log(snakeCaseString("lord of the rings"));
24 changes: 24 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,27 @@
// 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
const penceString = "399p";

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("39p"));
console.log(toPounds("100p"));
console.log(toPounds("4000p"));
console.log(toPounds("9p"));
14 changes: 8 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@ function pad(num) {
}
return numString;
}
console.log(pad());

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

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
// =============> three times in terms of hours, minutes and 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

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 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 will be 1, and because the remaining seconds after 61 % 60 will be 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
// =============> the return value of pad when it is called for the last time is "01". Because inside pad(), 1 is less than 2, and will be padded to "0" and will become "01".
70 changes: 63 additions & 7 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,72 @@ function formatAs12HourClock(time) {
return `${time} am`;
}

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

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
const currentOutput2 = formatAs12HourClock("00:30");
const targetOutput2 = "12:30 am";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
"00:30 should be 12:30 am"
);

const currentOutput3 = formatAs12HourClock("01:30");
const targetOutput3 = "01:30 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
"01:30 should be 01:30 am"
);
const currentOutput4 = formatAs12HourClock("11:59");
const targetOutput4 = "11:59 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
"11:59 should be 11:59 am"
);
const currentOutput5 = formatAs12HourClock("12:30");
const targetOutput5 = "12:30 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`,
"12:30 should be 12:30 pm"
);
const currentOutput6 = formatAs12HourClock("15:45");
const targetOutput6 = "03:45 pm";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`,
"15:45 should be 03:45 pm"
);
const currentOutput7 = formatAs12HourClock("23:30");
const targetOutput7 = "11:30 pm";
console.assert(
currentOutput7 === targetOutput7,
`current output: ${currentOutput7}, target output: ${targetOutput7}`,
"23:30 should be 11:30 pm"
);
// fixing the bugs found:
function formatAs12HourClock(time) {
const hours24 = Number(time.slice(0, 2)); // 23:00 ---> "23"
const minutes = time.slice(-2); // 23:00 ---> "00"

let period = "am";
let hours12 = hours24;

if (hours24 === 0) {
hours12 = 12; // midnight
} else if (hours24 === 12) {
period = "pm"; // noon
} else if (hours24 > 12) {
hours12 = hours24 - 12;
period = "pm";
}

return `${String(hours12).padStart(2, "0")}:${minutes} ${period}`; // converts the return output to string type and padded to 2 length.
}