Skip to content

Commit 5dcb8b5

Browse files
completed the key-errors tasks
1 parent f5b9e81 commit 5dcb8b5

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// the str is already declared as a parameter of the function, so we cannot declare it again inside the function. This will throw a syntax error as we can't declare a variable with the same name as a parameter in the same scope.
44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

@@ -10,4 +10,10 @@ function capitalise(str) {
1010
}
1111

1212
// =============> write your explanation here
13+
// the error is occurring because we are trying to declare a variable with the same name as a parameter in the same scope. This is not allowed in JavaScript and will throw a syntax error. To fix this, we can simply remove the let keyword and just reassign the value to the str parameter directly, like this:
14+
1315
// =============> write your new code here
16+
function capitalise(str) {
17+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
18+
return str;
19+
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// the error will occur because we are trying to declare a variable with the same name as a parameter in the same scope. Another error is that we are trying to log a variable that is not defined in outer scope, but only in the function scope.
56

67
// Try playing computer with the example to work out what is going on
78

@@ -15,6 +16,16 @@ function convertToPercentage(decimalNumber) {
1516
console.log(decimalNumber);
1617

1718
// =============> write your explanation here
18-
19+
// the first step is the function is defined and reads the function as it appears.
20+
// the second step is to run the function if it is called, but it is not called in this case, only the console.log is called, which is trying to log a variable that is not defined in the global scope, and hence this will throw a reference error first. But because the function is not called, the syntax error of declaring a variable with the same name as a parameter will not be thrown until the function is called.
1921
// Finally, correct the code to fix the problem
2022
// =============> write your new code here
23+
/* function convertToPercentage(decimalNumber) {
24+
decimalNumber = 0.5;
25+
const percentage = `${decimalNumber * 100}%`;
26+
27+
return percentage;
28+
}
29+
30+
console.log(convertToPercentage(0.5));
31+
*/

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11

22
// Predict and explain first BEFORE you run any code...
3-
3+
// it will throw a syntax error because we are trying to put a number in the parameter of the function where it should be only a variable name.
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-
7+
// this error will occur because a number is being used as a parameter name in the function definition, which is not allowed in JavaScript. Parameter names must be valid variable names, and numbers cannot be used as variable names. Therefore, the code will throw a syntax error when it is run. Another error is that the variable num is not defined in the function, so it will throw a reference error when trying to return num * num.
88
function square(3) {
99
return num * num;
1010
}
1111

1212
// =============> write the error message here
13-
13+
// Uncaught SyntaxError: Illegal return statement
14+
// Uncaught SyntaxError: Unexpected number
1415
// =============> explain this error message here
15-
16+
// This error message indicates that there is a syntax error in the code that entails both an illegal return statement and an unexpected number. The illegal return statement error occurs because the function is trying to return a value from a function that is not properly defined. The unexpected number error occurs because the parameter name of the function is a number, which is not allowed in JavaScript.
1617
// Finally, correct the code to fix the problem
1718

1819
// =============> write your new code here
20+
/*
21+
function square(num) {
22+
return num * num;
23+
}
24+
*/
1925

2026

0 commit comments

Comments
 (0)