Skip to content

Commit bb65455

Browse files
completed the key-errors requirements file
1 parent 4338fc8 commit bb65455

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
/*
4+
I predict the function will throw a SyntaxError as the variable str has already been declared in the function parameter
5+
function capitalise will capitalise the first letter with index 0
6+
and will append the sliced string from index 1 which is the second letter
7+
*/
38

49
// call the function capitalise with a string input
510
// interpret the error message and figure out why an error is occurring
611

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
12+
/* syntax Error :identifier the variabel has already been declared.
13+
As we can see the str variable has already been declared in the prameter of the function instead we just return the value */
14+
15+
// function capitalise(str) {
16+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
17+
// return str;
18+
// }
1119

1220
// =============> write your explanation here
21+
/* syntax Error :identifier the variabel has already been declared.
22+
As we can see the str variable has already been declared in the prameter of the function instead we just return the value */
23+
1324
// =============> write your new code here
25+
function capitalise(str) {
26+
return str[0].toUpperCase()+str.slice(1);
27+
28+
}
29+
console.log(capitalise("ebrahim"));
30+
console.log(capitalise('salomi'));

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
55

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

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
9+
// Try playing computer with the example to work out what is going on
1110

12-
return percentage;
13-
}
11+
// function convertToPercentage(decimalNumber) {
12+
// const decimalNumber = 0.5;
13+
// const percentage = `${decimalNumber * 100}%`;
1414

15-
console.log(decimalNumber);
15+
// return percentage;
16+
// }
1617

1718
// =============> write your explanation here
19+
/* The variable decimal Number has already been declared in the parameter
20+
and already is a local variable which can't be declared again causing a SyntaxError
21+
As the decimalNumber inside the function we can't print it using the console.log() as it has no power to the local scope
22+
instead we can delete the second declaration and just leave the parameter as an input for the user
23+
Finally, correct the code to fix the problem */
1824

19-
// Finally, correct the code to fix the problem
2025
// =============> write your new code here
26+
function convertToPercentage(decimalNumber) {
27+
const percentage = `${decimalNumber * 100}%`;
28+
return percentage;
29+
}
30+
31+
console.log(convertToPercentage(0.5));

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
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

65
// =============> write your prediction of the error here
6+
//I predict the funciton will throw a SyntaxEror as the perametters sould be valid variables like names or identifiers with upholding to the variable naming convention and in this case a number found
77

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

1212
// =============> write the error message here
13+
//the Error is a syntax error: Unexpected number
1314

1415
// =============> explain this error message here
16+
/* The function was givin a number as its perameter where in JS only allows varaible names and usnig a number causes a SyntaxError
17+
Also, the function was returning num*num where num is undefined causing a syntax error
18+
*/
1519

1620
// Finally, correct the code to fix the problem
1721

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

24+
function square(num) {
25+
return num * num;
26+
}
2027

28+
console.log(square(25));

0 commit comments

Comments
 (0)