Skip to content

Commit 129de51

Browse files
Completed all mandatory exercises for Sprint 2
1 parent 3372770 commit 129de51

File tree

10 files changed

+117
-73
lines changed

10 files changed

+117
-73
lines changed

Sprint-2/1-key-errors/0.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ===========> write your prediction here
3+
// 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.
34

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

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

12-
// =============> write your explanation here
13-
// =============> write your new code here
16+
// ===========> write your explanation here
17+
// Explanation: The original code used 'let str = ...' which throws a SyntaxError because 'str' is already defined in the parameter list.
18+
19+
// ===========> write your new code here
20+
// The fixed code is written above. I changed the variable name inside the function to 'capitalisedStr'.
21+
console.log(capitalise("hello"));

Sprint-2/1-key-errors/1.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5-
6-
// Try playing computer with the example to work out what is going on
4+
// ===========> write your prediction here
5+
// 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.
76

87
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
8+
// We removed 'const decimalNumber = 0.5;' to use the parameter directly
109
const percentage = `${decimalNumber * 100}%`;
11-
1210
return percentage;
1311
}
1412

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
13+
// ===========> write your explanation here
14+
// Explanation: A function parameter cannot be redeclared using 'const'. Also, parameters are local variables, meaning they cannot be accessed outside the function globally.
1815

1916
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
17+
// ===========> write your new code here
18+
// We fix this by calling the function properly and passing 0.5 as an argument inside console.log.
19+
console.log(convertToPercentage(0.5));

Sprint-2/1-key-errors/2.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

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

6-
// =============> write your prediction of the error here
5+
// ===========> write your prediction of the error here
6+
// 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).
77

8-
function square(3) {
9-
return num * num;
8+
function square(num) {
9+
return num * num;
1010
}
1111

12-
// =============> write the error message here
12+
// ===========> write the error message here
13+
// Error message: SyntaxError: Unexpected number
1314

14-
// =============> explain this error message here
15+
// ===========> explain this error message here
16+
// 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.
1517

1618
// Finally, correct the code to fix the problem
1719

18-
// =============> write your new code here
19-
20-
20+
// ===========> write your new code here
21+
// The fixed code is written above where I changed '3' to 'num'.
22+
// Now let's call the function to test it:
23+
console.log(square(3));

Sprint-2/2-mandatory-debug/0.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
// Predict and explain first...
22

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

56
function multiply(a, b) {
6-
console.log(a * b);
7+
// We removed console.log and added 'return' so the function gives the result back
8+
return a * b;
79
}
810

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

11-
// =============> write your explanation here
13+
// ===========> write your explanation here
14+
// 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.
1215

1316
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
17+
// ===========> write your new code here
18+
// The fixed code is written above. We changed 'console.log(a * b)' to 'return a * b'.

Sprint-2/2-mandatory-debug/1.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ===========> write your prediction here
3+
// Prediction: The output will say "... is undefined" because the function returns nothing before it even calculates the sum.
34

45
function sum(a, b) {
5-
return;
6-
a + b;
6+
// We fix the error by putting the expression on the SAME line as the 'return' keyword.
7+
return a + b;
78
}
89

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

11-
// =============> write your explanation here
12+
// ===========> write your explanation here
13+
// 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.
14+
1215
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
16+
// ===========> write your new code here
17+
// The fixed code is written above. I moved 'a + b' to the same line as 'return'.

Sprint-2/2-mandatory-debug/2.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// ===========> Write your prediction here
5+
// 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.
56

6-
const num = 103;
7+
// We don't need this global variable, so I commented it out:
8+
// const num = 103;
79

8-
function getLastDigit() {
10+
// We added 'num' as a parameter inside the parentheses
11+
function getLastDigit(num) {
912
return num.toString().slice(-1);
1013
}
1114

@@ -14,11 +17,13 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1417
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1518

1619
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
20+
// ===========> write the output here
21+
// Output after the fix: 2, 5, 6. (Before the fix it was 3, 3, 3).
22+
1823
// Explain why the output is the way it is
19-
// =============> write your explanation here
20-
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
24+
// ===========> write your explanation here
25+
// 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.
2226

23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
27+
// Finally, correct the code to fix the problem
28+
// ===========> write your new code here
29+
// The fixed code is written above.
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
// Below are the steps for how BMI is calculated
2-
32
// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.
4-
5-
// 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:
6-
3+
// For example, if you weigh 70kg and are 1.73m tall, you work out your BMI by:
74
// squaring your height: 1.73 x 1.73 = 2.99
85
// dividing 70 by 2.99 = 23.41
96
// Your result will be displayed to 1 decimal place, for example 23.4.
107

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

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

17-
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
15+
// 3. Return the result to 1 decimal place (إرجاع النتيجة بخانة عشرية واحدة)
16+
return bmi.toFixed(1);
17+
}
18+
19+
// === Let's test the function to see if it works! ===
20+
console.log(`The BMI for 70kg and 1.73m is: ${calculateBMI(70, 1.73)}`);
21+
// It should print: 23.4
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// A set of words can be grouped together in different cases.
2-
32
// For example, "hello there" in snake case would be written "hello_there"
43
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.
54

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

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

14-
// You will need to come up with an appropriate name for the function
15-
// Use the MDN string documentation to help you find a solution
16-
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
16+
console.log(toUpperSnakeCase("lord of the rings"));
17+
// It should print: LORD_OF_THE_RINGS
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
// In Sprint-1, there is a program written in interpret/to-pounds.js
2-
32
// You will need to take this code and turn it into a reusable block of code.
43
// You will need to declare a function called toPounds with an appropriately named parameter.
54

5+
function toPounds(pence) {
6+
// 1. Divide pence by 100 to get pounds (نقسم على 100)
7+
const pounds = pence / 100;
8+
9+
// 2. Return the formatted string with a £ sign and 2 decimal places (إرجاع النص مع علامة الجنيه وخانتين عشريتين)
10+
return ${pounds.toFixed(2)}`;
11+
}
12+
613
// You should call this function a number of times to check it works for different inputs
14+
// === Let's test the function ===
15+
16+
console.log(toPounds(150)); // Should print: £1.50
17+
console.log(toPounds(2500)); // Should print: £25.00
18+
console.log(toPounds(99)); // Should print: £0.99
19+
console.log(toPounds(5)); // Should print: £0.05

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@ function formatTimeDisplay(seconds) {
1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
1313

14-
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
15-
// to help you answer these questions
16-
14+
// You will need to play computer with this example
1715
// Questions
1816

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

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

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

2729
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
30+
// ===========> write your answer here
31+
// Answer: "00"
2932

30-
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
33+
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
34+
// ===========> write your answer here
35+
// 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.
3236

33-
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
37+
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
38+
// ===========> write your answer here
39+
// 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").

0 commit comments

Comments
 (0)