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: 13 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
// ===========> write your prediction here
// Prediction: There will be an error because we are trying to declare a variable 'str' using 'let', but 'str' is already declared as the function's parameter.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
capitalise("mahmoud");

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
// We fix the error by just updating the existing parameter without using 'let',
// or by creating a new variable with a DIFFERENT name. Here we use a different name.
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}

// =============> write your explanation here
// =============> write your new code here
// ===========> write your explanation here
// Explanation: The original code used 'let str = ...' which throws a SyntaxError because 'str' is already defined in the parameter list.

// ===========> write your new code here
// The fixed code is written above. I changed the variable name inside the function to 'capitalisedStr'.
console.log(capitalise("hello"));
17 changes: 8 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// Predict and explain first...

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

// Try playing computer with the example to work out what is going on
// ===========> write your prediction here
// Prediction: There are two errors. First, we are trying to redeclare the parameter 'decimalNumber' using 'const'. Second, we are trying to log 'decimalNumber' outside the function where it doesn't exist.

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
// We removed 'const decimalNumber = 0.5;' to use the parameter directly
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
// ===========> write your explanation here
// Explanation: A function parameter cannot be redeclared using 'const'. Also, parameters are local variables, meaning they cannot be accessed outside the function globally.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ===========> write your new code here
// We fix this by calling the function properly and passing 0.5 as an argument inside console.log.
console.log(convertToPercentage(0.5));
21 changes: 12 additions & 9 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@

// 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
// ===========> write your prediction of the error here
// Prediction: We will get a SyntaxError because a function parameter cannot be a literal number (like 3). It must be a valid variable name (identifier).

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

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

// =============> explain this error message here
// ===========> explain this error message here
// Explanation: When declaring a function, the parameters must be names (like 'num'), not actual values. We only pass actual values (like 3) when we CALL the function.

// Finally, correct the code to fix the problem

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


// ===========> write your new code here
// The fixed code is written above where I changed '3' to 'num'.
// Now let's call the function to test it:
console.log(square(3));
12 changes: 8 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// Predict and explain first...

// =============> write your prediction here
// ===========> write your prediction here
// Prediction: The output sentence will say "... is undefined" because the multiply function does not return a value.

function multiply(a, b) {
console.log(a * b);
// We removed console.log and added 'return' so the function gives the result back
return a * b;
}

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

// =============> write your explanation here
// ===========> write your explanation here
// Explanation: Without a 'return' statement, a function evaluates to 'undefined'. To use the result of the calculation inside the template literal string, the function MUST return it.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ===========> write your new code here
// The fixed code is written above. We changed 'console.log(a * b)' to 'return a * b'.
14 changes: 9 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Predict and explain first...
// =============> write your prediction here
// ===========> write your prediction here
// Prediction: The output will say "... is undefined" because the function returns nothing before it even calculates the sum.

function sum(a, b) {
return;
a + b;
// We fix the error by putting the expression on the SAME line as the 'return' keyword.
return a + b;
}

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

// =============> write your explanation here
// ===========> write your explanation here
// Explanation: In JavaScript, if you put a line break immediately after the 'return' keyword, it acts as 'return;' and stops the function, returning 'undefined'. The expression 'a + b' must be on the same line.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ===========> write your new code here
// The fixed code is written above. I moved 'a + b' to the same line as 'return'.
23 changes: 14 additions & 9 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// ===========> Write your prediction here
// Prediction: The output will incorrectly say the last digit is "3" for all numbers, because the function uses the global variable 'num' (103) instead of accepting a parameter.

const num = 103;
// We don't need this global variable, so I commented it out:
// const num = 103;

function getLastDigit() {
// We added 'num' as a parameter inside the parentheses
function getLastDigit(num) {
return num.toString().slice(-1);
}

Expand All @@ -14,11 +17,13 @@ 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
// ===========> write the output here
// Output after the fix: 2, 5, 6. (Before the fix it was 3, 3, 3).

// Explain why the output is the way it is
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
// ===========> write your explanation here
// Explanation: The original function had no parameters, so it used the global variable 'num = 103'. By adding 'num' as a parameter, the function now correctly uses the value passed into it when called.

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// Finally, correct the code to fix the problem
// ===========> write your new code here
// The fixed code is written above.
24 changes: 13 additions & 11 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Below are the steps for how BMI is calculated

// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.

// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:

// For example, if you weigh 70kg and are 1.73m tall, you work out your BMI by:
// squaring your height: 1.73 x 1.73 = 2.99
// dividing 70 by 2.99 = 23.41
// Your result will be displayed to 1 decimal place, for example 23.4.

// You will need to implement a function that calculates the BMI of someone based off their weight and height
function calculateBMI(weight, height) {
// 1. Calculate height squared (الطول مضروب في نفسه)
const heightSquared = height * height;

// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place
// 2. Divide weight by height squared (الوزن تقسيم الطول المربع)
const bmi = weight / heightSquared;

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// 3. Return the result to 1 decimal place (إرجاع النتيجة بخانة عشرية واحدة)
return bmi.toFixed(1);
}

// === Let's test the function to see if it works! ===
console.log(`The BMI for 70kg and 1.73m is: ${calculateBMI(70, 1.73)}`);
// It should print: 23.4
21 changes: 11 additions & 10 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// A set of words can be grouped together in different cases.

// For example, "hello there" in snake case would be written "hello_there"
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.

// Implement a function that:

// Given a string input like "hello there"
// When we call this function with the input string
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
// I named the function 'toUpperSnakeCase'
function toUpperSnakeCase(text) {
// 1. Replace all spaces " " with underscores "_"
// 2. Convert the whole text to UPPERCASE
return text.replaceAll(" ", "_").toUpperCase();
}

// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"
// === Let's test the function to see if it works! ===
console.log(toUpperSnakeCase("hello there"));
// It should print: HELLO_THERE

// 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
console.log(toUpperSnakeCase("lord of the rings"));
// It should print: LORD_OF_THE_RINGS
15 changes: 14 additions & 1 deletion Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// In Sprint-1, there is a program written in interpret/to-pounds.js

// You will need to take this code and turn it into a reusable block of code.
// You will need to declare a function called toPounds with an appropriately named parameter.

function toPounds(pence) {
// 1. Divide pence by 100 to get pounds (نقسم على 100)
const pounds = pence / 100;

// 2. Return the formatted string with a £ sign and 2 decimal places (إرجاع النص مع علامة الجنيه وخانتين عشريتين)
return `£${pounds.toFixed(2)}`;
}

// You should call this function a number of times to check it works for different inputs
// === Let's test the function ===

console.log(toPounds(150)); // Should print: £1.50
console.log(toPounds(2500)); // Should print: £25.00
console.log(toPounds(99)); // Should print: £0.99
console.log(toPounds(5)); // Should print: £0.05
25 changes: 15 additions & 10 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,29 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

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

// You will need to play computer with this example
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// ===========> write your answer here
// Answer: 3 times (once for hours, once for minutes, and once for seconds).

// Call formatTimeDisplay with an input of 61, now answer the following:
// (I am calling it here to see the result)
console.log(formatTimeDisplay(61));

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// ===========> write your answer here
// Answer: 0. Because JavaScript evaluates the string from left to right, so it calls pad(totalHours) first, and totalHours is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// ===========> write your answer here
// Answer: "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
// 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
// Answer: 1. Explanation: The last call in the return statement is pad(remainingSeconds). When the input is 61 seconds, 61 % 60 leaves 1 remaining second.

// 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
// 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
// Answer: "01". Explanation: The pad function takes the number 1, converts it to a string "1", and pads the start with a "0" to make it 2 characters long ("01").