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
9 changes: 7 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here
// it will be reference error because the variable str is being declared twice, once as a parameter and once inside the function body.

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

// =============> write your explanation here
// the error is occurring because the variable str is being declared twice, once as a parameter and once inside the function body.
//removing the let keyword would fix this error, for the function to work properly.

// =============> write your new code here
let firstCapitalised = capitalise("hello");
console.log(firstCapitalised);
16 changes: 12 additions & 4 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

// Why will an error occur when this program runs?
// =============> write your prediction here

// i predict that the error will be a syntax error because the variable decimalNumber is being declared twice, once as a parameter and once inside the function body
// Try playing computer with the example to work out what is going on

/*
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +13,16 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);

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

// removing the const keyword would fix this error, for the function to work properly.
// Finally, correct the code to fix the problem
// =============> write your new code here

function convertToPercentage(decimalNumber) {
decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
let converted = convertToPercentage(0.5);
console.log(converted);
17 changes: 13 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

// Predict and explain first BEFORE you run any code...
// num is not defined at all, so when the function is called it will throw a reference error.
// and inside the function the parameter is not defined correctly, it should be num instead of 3.

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

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

// reference error because the parameter is not defined correctly, it should be num instead of 3.
/* below is the original code
function square(3) {
return num * num;
}
*/

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

// the error was syntaxError: unexpected number.
// =============> explain this error message here

// because the parameter is not defined correctly, it should be num instead of 3. so the function is not defined correctly and it will throw a syntax error.
// Finally, correct the code to fix the problem

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

function square(num) {
return num * num;

}
let result = square(3);
console.log(result);

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

// there is no return statement in the multiply function, so when we try to log the result of the function call, it will return undefined.
/* original code
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

// console.log will do the multiplication of a and b and print the result to the console, but it will not return any value.
// there is no operation done here. just for testing purpose not actual operation.
// Finally, correct the code to fix the problem
// =============> write your new code here

function multiply(a, b) {
console.log(a * b);
return a * b; // new code added to return the result of the multiplication.
}
14 changes: 12 additions & 2 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

//we have to delete the semicolon used after the return statement in the sum function, because it will terminate the function and return undefined.
/* below is the original code
function sum(a, b) {
return;
a + b;
}

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

*/
// =============> write your explanation here
// same as my prediction.
// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b; // removed the semicolon after the return statement to fix the problem.
// and also assigned the value of a + b to the return statement so that it will return the sum of a and b.
// unlike my prediction removing the semicolon only wont do the fix. because in javascript there is a feature called automatic semicolon insertion which will automatically insert a semicolon after the return statement
// when playing computer with this code it will read return with out ; as same as with it. - same error result
}

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

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

const num = 103;
// the console.log statement will print the last digit of the number 103, which is 3. for all 3 cases.
// since the num variable is declared is global scope, it will always return the last digit of 103.
// the function loses the feature of reusability because it is not taking any parameter to work with, it is always working with the global variable num.
/* original code
const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
Expand All @@ -12,13 +15,22 @@ function getLastDigit() {
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 is the same as my prediction, it will print the last digit of the number 103, which is 3. for all 3 cases.
// Explain why the output is the way it is
// =============> write your explanation here
// like i explained in my prediction, since the num variable is declared is global scope, it will always return the last digit of 103.
// 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
7 changes: 6 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,9 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);

return bmi.toFixed(1); // toFixed() method is used to format the BMI value to 1 decimal place
}
let result = calculateBMI(70, 1.73); // Example usage of the function with weight 70kg and height 1.73m
console.log(`The BMI is ${result}`); // Output the result to the console.
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 @@ -14,3 +14,12 @@
// 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(inString) {
const outPutString = inString.toUpperCase().replaceAll(" ","_");
return outPutString;
}
let output1 = toUpperSnakeCase("hello there");
let output2 = toUpperSnakeCase("lord of the rings");
console.log(`The result for output1 is : ${output1}.
The result for output2 is : ${output2}.`)
23 changes: 23 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,26 @@
// 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 penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // pad the string with leading zeros to ensure it has at least 3 characters
const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); // get the substring representing the pounds by taking all characters except the last two
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); // get the substring representing the pence by taking the last two characters and padding it with a "0" if necessary
console.log(`£${pounds}.${pence}`); //

*/

function toPounds(penceString){
const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // pad the string with leading zeros to ensure it has at least 3 characters
const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); // get the substring representing the pounds by taking all characters except the last two
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); // get the substring representing the pence by taking the last two characters and padding it with a "0" if necessary

return `£${pounds}.${pence}`
}
let output1 = toPounds("500p");
let output2 = toPounds("399p");
let output3 = toPounds("2000p");
console.log(`${output1}
${output2}
${output3}`);
11 changes: 6 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ 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

// 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 assigned to num is 0
// c) What is the return value of pad is called for the first time?
// =============> write your answer here

// the return value 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 last call to pad is with remainingSeconds, which is 1. so num =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
// pad returns "01" because it pads the single-digit "1" with a leading "0"