Skip to content

Commit ac7e78b

Browse files
Complete Sprint-3 errors exercise (0.js, 1.js, 2.js)
1 parent a6050ec commit ac7e78b

3 files changed

Lines changed: 58 additions & 23 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ==============> write your prediction here
3+
// I predict this will throw a SyntaxError, because the parameter 'str' is
4+
// being redeclared with 'let' inside the function.
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
68

79
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10+
let result = `${str[0].toUpperCase()}${str.slice(1)}`;
11+
return result;
1012
}
1113

12-
// =============> write your explanation here
13-
// =============> write your new code here
14+
console.log(capitalise("hello"));
15+
16+
// ==============> write your explanation here
17+
// The parameter 'str' already existed in the function's scope. The original
18+
// code tried to declare another variable with 'let str', which caused a
19+
// naming collision — JavaScript doesn't allow redeclaring a variable with
20+
// 'let' in the same scope. This threw a SyntaxError before the function
21+
// could even run. Renaming the new variable to 'result' fixes the collision.
22+
23+
// ==============> write your new code here
24+
// (the fixed function above already satisfies this)

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
// Predict and explain first...
2-
32
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
3+
// ==============> write your prediction here
4+
// I predict this will throw a SyntaxError, because 'decimalNumber' is being
5+
// redeclared with 'const' inside the function, even though it's already the
6+
// function's parameter.
57

68
// Try playing computer with the example to work out what is going on
79

810
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
1112

1213
return percentage;
1314
}
1415

15-
console.log(decimalNumber);
16+
console.log(convertToPercentage(0.5));
1617

17-
// =============> write your explanation here
18+
// ==============> write your explanation here
19+
// 'decimalNumber' was already declared as the function's parameter. The
20+
// original code tried to declare another variable with the same name using
21+
// 'const decimalNumber = 0.5;', which caused a naming collision — JavaScript
22+
// doesn't allow redeclaring a variable in the same scope. This threw a
23+
// SyntaxError before the function could run.
24+
//
25+
// There was also a second issue: the original 'console.log(decimalNumber);'
26+
// was outside the function, so 'decimalNumber' didn't exist there (it's
27+
// scoped only inside 'convertToPercentage'), and the function was never
28+
// actually called. The fix removes the duplicate declaration and calls
29+
// convertToPercentage(0.5) directly, logging its return value instead.
1830

1931
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
32+
// ==============> write your new code here
33+
// (the fixed function above already satisfies this)

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

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

4-
// this function should square any number but instead we're going to get an error
5-
6-
// =============> write your prediction of the error here
3+
// this function should square any number but instead we're getting an error
4+
// ==============> write your prediction of the error here
5+
// I predict this will throw a SyntaxError, because '3' is used as the
6+
// parameter name in 'function square(3)', and a number can't be used as a
7+
// parameter name — parameter names must be valid identifiers (like variable
8+
// names).
79

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

12-
// =============> write the error message here
13-
14-
// =============> explain this error message here
14+
console.log(square(3));
1515

16-
// Finally, correct the code to fix the problem
17-
18-
// =============> write your new code here
16+
// ==============> write the error message here
17+
// SyntaxError: Unexpected number
1918

19+
// ==============> explain this error message here
20+
// Function parameters must be valid identifiers — the same rules as variable
21+
// names (they must start with a letter, '$', or '_', never a digit). The
22+
// original code used '3' as the parameter name, which isn't a valid
23+
// identifier, so JavaScript couldn't even parse the function definition.
24+
// This error happens before any code runs. There was also a second bug: the
25+
// function body referred to 'num', which didn't match the (invalid)
26+
// parameter name at all — fixing the parameter name to 'num' resolves both
27+
// issues at once.
2028

29+
// Finally, correct the code to fix the problem
30+
// ==============> write your new code here
31+
// (the fixed function above already satisfies this)

0 commit comments

Comments
 (0)