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
17 changes: 13 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// The variable inside the function str has already declared as function parameter.

// 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;
}
//function capitalise(str) {
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
//return str;
//}

// =============> write your explanation here
// Repeatative variable declaration prediction matches with the error message. Then I changed the old variable into new inside the
// function. Then affter running the code it won't return anything cause no function call has been made yet.
// =============> write your new code here
// This is my code:
function capitalise(str) {
let newstr = `${str[0].toUpperCase()}${str.slice(1)}`;
return newstr;
}
console.log(capitalise("dipa"));
30 changes: 24 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I thought the function will work properly but couldn't print the correct value cause the value should be convertToPercentage
// but it is declared only as decimalNumber.

// 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
// When I run the code after my prediction it shows SyntaxError: Identifier 'decimalNumber' has already been declared. Which means
// decimalNumber has been declared already as function parameter and then again declared inside the function as variable. Then when I
// declare const decimalNumber = 0.5; outside the function and run the code it shows the value of variable decimalNumber which is 0.5. Then
// I replace console.log(decimalNumber); to console.log(convertToPercentage); and run the code again and it shows this output
// [Function: convertToPercentage] which means only the funtion name. Then I replace console.log(convertToPercentage); to
// console.log(convertToPercentage(decimalNumber)); again and it finally works properly that means converted the decimal number to
// percentage value which shows 50% .


// Finally, correct the code to fix the problem
// =============> write your new code here
// Corrected code:

function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
const decimalNumber = 0.5;
console.log(convertToPercentage(decimalNumber));
17 changes: 14 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,28 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// I think we cannot directly insert value which is 3 as a parameter. We have to declare it as a variable.

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

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

// =============> explain this error message here
// The error shows SyntaxError as Unexpected number which means the function syntax doesnot expect to input
// any values directly in the parameter. That's why the function doesnot work.

// Finally, correct the code to fix the problem

// =============> write your new code here
// The correct code:

function square(num) {
return num * num;
}
let num = 3;
console.log(square(num));


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

// =============> write your prediction here
// I think there is nor function call has been made. a and b only inserted as function parameters.

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

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

// =============> write your explanation here
// The result of multiplying 10 and 32 is undefined. This is the error. Now I think the reason for showing the result undefined is that
// console.log(a * b); only prints 320 but the function does not return anything.

// Finally, correct the code to fix the problem
// =============> write your new code here
// The correct code:

function multiply(a, b) {
return(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
20 changes: 15 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// I think only return; doesnot mean anything means it won't return anything. Besides, the expression a + b; won't also work
// cause after calculating a + b it won't return anything and the output will show undefined value.

function sum(a, b) {
return;
a + b;
}
// function sum(a, b) {
// return;
// a + b;
// }

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

// =============> write your explanation here
// My prediction exactly matches with the output after I run code.
// Finally, correct the code to fix the problem
// =============> write your new code here
// The correct code:

function sum(a, b) {
return (a + b);
}

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

// Predict the output of the following code:
// =============> Write your prediction here
// i think the function won't return anything for numbers 42, 105 and 806 because the variable num contains value 103 and the return will only
// work with 103 inside the function. There is no relation between const num = 103; and 42, 105 and 806.

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

function getLastDigit() {
return num.toString().slice(-1);
}
// function getLastDigit() {
// 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 42 is ${getLastDigit(42)}`);
// 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:
// 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 quite similar to my prediction. When the function tried to find out function calls for getLastDigit(42), getLastDigit(105)
// and getLastDigit(806) it doesn't find any of these because those didn't match with the variable declaration. As the function wokred only
// with 103, it returned value for 103 and printed 3 as last digit for all three values.
// Finally, correct the code to fix the problem
// =============> write your new code here
// The correct code:

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
10 changes: 7 additions & 3 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// Then when we call this function with the weight and height
// 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
}

function calculateBMI(weightkg, heightm) {
const BMI = (weightkg / (heightm * heightm));
return BMI.toFixed(1);
}
let result = calculateBMI(70, 1.73);
console.log (result);
6 changes: 6 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,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) {
let newstr = str.replaceAll(" ", "_").toUpperCase();
return newstr;
}
console.log(toUpperSnakeCase("lord of the rings"));
11 changes: 11 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,14 @@
// 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(pence) {
const remainingPence = (pence % 100);
const Pounds = (pence - remainingPence) / 100;
return `£${Pounds}.${remainingPence}`;
}

console.log (toPounds(155));
console.log (toPounds(99));
console.log (toPounds(10000));
15 changes: 10 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

const seconds = 61;
console.log(formatTimeDisplay(seconds));
// 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
// For each formatTimeDisplay is called, pad will be called 3 times. 1. pad(totalHours), 2. pad(remainingMinutes), 3. pad(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
// The value assigned to num when pad is called for the first time will be 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when it is called for the first time will be "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 when pad is called for the last time in this program will be 1. Because, pad is called for the last time
// for the value of remainingSeconds.

// 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 in this program will be "01". Because, after converting num.toString(1)
// the length of the string "1" is 1 character. So it checks in while (numString.length < 2) and founds "1" which is less than
// 2 character length. Then it will go inside numString = "0" + numString; and executes the function and will return "01".
65 changes: 61 additions & 4 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,79 @@

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

if (hours === 0) {
return `12:${time.slice(3)} am`;
}
else if (hours < 12) {
return `${time} am`;
}
else if (hours <= 12) {
return `${time} pm`;
}
else if (hours > 12) {
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
}
return `${time} am`;
else if (hours > 12) {
return `${hours - 12}:${minutes} pm`;
}
else if (hours < 12) {
return `${time}:${minutes} am`;
}
}

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

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

const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);
console.log (formatAs12HourClock("00:00"));

const currentOutput4 = formatAs12HourClock("23:59");
const targetOutput4 = "11:59 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);
console.log (formatAs12HourClock("23:59"));

const currentOutput5 = formatAs12HourClock("12:00");
const targetOutput5 = "12:00 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);
console.log (formatAs12HourClock("12:00"));

const currentOutput6 = formatAs12HourClock("13:00");
const targetOutput6 = "01:00 pm";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
);
console.log (formatAs12HourClock("13:00"));

const currentOutput7 = formatAs12HourClock("11:59");
const targetOutput7 = "11:59 am";
console.assert(
currentOutput7 === targetOutput7,
`current output: ${currentOutput7}, target output: ${targetOutput7}`
);
console.log (formatAs12HourClock("11:59"));
Loading