Skip to content

Commit 14fee9a

Browse files
answered the exercises of the key errors and debug the mandatory file
1 parent e829acf commit 14fee9a

File tree

7 files changed

+110
-33
lines changed

7 files changed

+110
-33
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
//function capitalize will capitalise the first letter with index 0
4+
// and will append the sliced string from index 1 which is the second letter
35

46
// call the function capitalise with a string input
7+
58
// interpret the error message and figure out why an error is occurring
9+
//syntax Error :identifier the variabel has already been declared.
10+
// As we can see the str variable has already been declared in the prameter of the function instead we just return the value
11+
// The Error message is
612

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
13+
// function capitalise(str) {
14+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
15+
// console.log(str);
16+
// return str;
17+
// }
18+
19+
20+
// =============> write your explanation here
1121

12-
// =============> write your explanation here
1322
// =============> write your new code here
23+
24+
function capitalise(str) {
25+
return `${str[0].toUpperCase()}${str.slice(1)}`;
26+
27+
}
28+
capitalise("ebrahim");
29+
capitalise('salomi');

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

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

3+
34
// Why will an error occur when this program runs?
45
// =============> write your prediction here
56

7+
//1- The function is trying to declare the decimalNumber twice in the perameter and later as a new variable
8+
//2-console.log() is printing a variable in the local scope where it can't be accessed as it is inside the function
9+
610
// Try playing computer with the example to work out what is going on
711

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
12+
// function convertToPercentage(decimalNumber) {
13+
// const decimalNumber = 0.5;
14+
// const percentage = `${decimalNumber * 100}%`;
15+
16+
// return percentage;
17+
// }
1118

12-
return percentage;
13-
}
1419

15-
console.log(decimalNumber);
1620

17-
// =============> write your explanation here
1821

22+
// =============> write your explanation here
23+
//similar to the previous exercise where the variable decimal Number has already been declared in the parameter
24+
// and already is a local variable which can't be declared again
25+
//As the decimalNumber inside the function we can't print it using the console.log() as it has no power to the local scope
26+
//instead we can delete the second declaration and just leave the parameter as an input for the user which would be nicely work
1927
// Finally, correct the code to fix the problem
28+
2029
// =============> write your new code here
30+
function convertToPercentage(decimalNumber) {
31+
const percentage = `${decimalNumber * 100}%`;
32+
return percentage;
33+
}
34+
35+
console.log(convertToPercentage(0.5));

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
//The error might be accuring because the parameter can't be a number and should be a variable
78

8-
function square(3) {
9-
return num * num;
10-
}
9+
// function square(3) {
10+
// return num * num;
11+
// }
1112

1213
// =============> write the error message here
14+
//the Error is a syntax error: Unexpected number
1315

1416
// =============> explain this error message here
17+
// we can put a number or any argument when we call the number not when we shape it
1518

1619
// Finally, correct the code to fix the problem
20+
function square(num) {
21+
return num * num;
22+
}
1723

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

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

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

33
// =============> write your prediction here
4+
// This function would multiply the two arguments a and b whatever that is
5+
// when we log the output we will get the string and the output of the function but because we are not returning an output we might run into an error
46

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
7+
// function multiply(a, b) {
8+
// console.log(a * b);
9+
// }
810

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

1113
// =============> write your explanation here
14+
//The console.log() inside the funtion will print a*b but when we call the function will return() undefined means will return nothing)for the two variables a and b
1215

1316
// Finally, correct the code to fix the problem
1417
// =============> write your new code here
18+
function multiply(a, b) {
19+
return a * b;
20+
}
21+
22+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
23+

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// The function suppose to return the sum of the two variables but we will encounter an error probably a syntax Error
34

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
8-
9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
105

6+
// function sum(a, b) {
7+
// return;
8+
// a + b;
9+
// }
10+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
11+
// As we return nothing by closing the line with a semicolon hence a + b is not returned where we tell the computer to exit and go back to global scope and the computer will not be able to read the lines after that
1112
// =============> write your explanation here
13+
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
17+
function sum(a, b) {
18+
return a + b;
19+
}
20+
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,41 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//getLastDigit will convert a number to a string and reverse it
6+
// However, since there is no parameter or local variable we might run into an Error
57

6-
const num = 103;
8+
// const num = 103;
79

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
10+
// function getLastDigit() {
11+
// return num.toString().slice(-1);
12+
// }
1113

12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
// As expected, the function has no parameters, so it relies on the global variable "num" as its only variable.
26+
// Since there are no parameters defined, the function permanently uses the global variable instead.
27+
// This means even if we pass arguments when calling the function, they will be ignored because there are no parameters to receive them.
28+
2029
// Finally, correct the code to fix the problem
2130
// =============> write your new code here
2231

32+
function getLastDigit(num) {
33+
return num.toString().slice(-1);
34+
}
35+
36+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
37+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
38+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
39+
40+
2341
// This program should tell the user the last digit of each number.
2442
// Explain why getLastDigit is not working properly - correct the problem

Sprint-2/3-mandatory-implement/1-bmi.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
// It should return their Body Mass Index to 1 decimal place
1616

1717
function calculateBMI(weight, height) {
18+
let result = weight / (height*height);
19+
return result.toFixed(1);
20+
1821
// return the BMI of someone based off their weight and height
19-
}
22+
}
23+
24+
console.log(calculateBMI(70,168))

0 commit comments

Comments
 (0)