Skip to content

Commit 857e12a

Browse files
committed
sprint 3 ex:1-2
1 parent d28242a commit 857e12a

5 files changed

Lines changed: 44 additions & 6 deletions

File tree

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

Lines changed: 11 additions & 3 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:this will throw an error, because the function has a parameter called str and then tries to declare another variable also called str inside the function body using let.
33

4-
// call the function capitalise with a string input
4+
// call the function capitalise with a string input: SyntaxError: Identifier 'str' has already been declared
55
// interpret the error message and figure out why an error is occurring
66

77
function capitalise(str) {
88
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
99
return str;
1010
}
1111

12-
// =============> write your explanation here
12+
// =============> write your explanation here
13+
:in JavaScript, a function's parameters are treated like variables that already exist within that function's scope the moment the function starts. So writing function capitalise(str) { ... }, str is already declared as soon as you're inside the function. Trying to declare another variable with the same name using let str = ... inside that same scope is not allowed — JavaScript won't let you redeclare an existing let/const/parameter name in the same scope, since it would be ambiguous which "str" you mean.
14+
1315
// =============> write your new code here
16+
:function capitalise(str) {
17+
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
18+
return capitalised;
19+
}
20+
21+
console.log(capitalise("hello")); // "Hello"

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

Lines changed: 8 additions & 0 deletions
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+
: inside the function, decimalNumber is already the parameter name, but then it's redeclared with const decimalNumber = 0.5; inside the function body. console.log(decimalNumber); tries to access decimalNumber completely outside the function. But decimalNumber only exists inside convertToPercentage — it was never declared anywhere in the outer/global scope.
56

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

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

1718
// =============> write your explanation here
19+
: the redundant inner redeclaration is removed entirely — the function just uses the parameter decimalNumber it's given. And instead of trying to log a variable that doesn't exist outside the function, the code calls the function itself, passing in 0.5, and logs whatever it returns — which will correctly print "50%".
1820

1921
// Finally, correct the code to fix the problem
2022
// =============> write your new code here
23+
:function convertToPercentage(decimalNumber) {
24+
const percentage = `${decimalNumber * 100}%`;
25+
return percentage;
26+
}
27+
28+
console.log(convertToPercentage(0.5));

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33

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

6-
// =============> write your prediction of the error here
6+
// =============> write your prediction of the error here
7+
: This should fail before it even runs, because 3 is being used as a parameter name in the function definition — function square(3) { ... }. Parameter names have to follow the same rules as variable names, and just like a variable, they can't just be a plain number. A parameter name is meant to be a placeholder/label for whatever value gets passed in later, not a literal value itself.
78

89
function square(3) {
910
return num * num;
1011
}
1112

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

1416
// =============> explain this error message here
17+
:hen you write a function like function square(num) { ... }, num is a name that acts as a stand-in for whatever value someone passes in when they call square(5) or square(10).Writing 3 there doesn't make sense to the parser; it's like trying to say "let 3 be the placeholder," which conflicts with how the language works. the function body refers to num, but num was never actually defined as the parameter name, so even fixing the syntax error alone wouldn't be enough.
1518

1619
// Finally, correct the code to fix the problem
1720

1821
// =============> write your new code here
22+
function square(num) {
23+
return num * num;
24+
}
25+
26+
console.log(square(3)); // 9
1927

2028

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

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

33
// =============> write your prediction here
4+
:The function multiply uses console.log to print the result inside itself, but it doesn't have a return statement
45

56
function multiply(a, b) {
67
console.log(a * b);
@@ -9,6 +10,12 @@ function multiply(a, b) {
910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
: console.log() and return do two completely different jobs. console.log() just prints something to the console for you to see — it doesn't hand any value back to the code that called the function. return is what actually sends a value back out of the function so it can be used elsewhere, like inside another expression or template literal.
1214

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

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
:the return statement sits on its own line, completely separate from a + b below it.JavaScript will treat return; as returning nothing at all, and a + b will just never actually run, since the function exits right at the return line.
34

45
function sum(a, b) {
56
return;
@@ -8,6 +9,12 @@ function sum(a, b) {
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 JavaScript automatically inserts a semicolon after return if there's a line break immediately following it, effectively treating it as return; — returning undefined — and then completely ignoring whatever comes after on the next line, since the function has already exited by that point. So a + b becomes dead code that never executes; the function returns before it even gets there.
1213
// Finally, correct the code to fix the problem
1314
// =============> write your new code here
15+
16+
function sum(a, b) {
17+
return a + b;
18+
}
19+
20+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // "The sum of 10 and 32 is 42"

0 commit comments

Comments
 (0)