Skip to content

Commit 31de955

Browse files
committed
completed mandatory excerise for sprint 2
1 parent 3372770 commit 31de955

File tree

10 files changed

+64
-8
lines changed

10 files changed

+64
-8
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
@@ -10,4 +11,11 @@ function capitalise(str) {
1011
}
1112

1213
// =============> write your explanation here
14+
// The original error happened because we tried to declare a new variable
15+
// named `str` with `let` even though `str` was already a parameter. That's
16+
// illegal in JavaScript and produces a SyntaxError. By removing the extra
17+
// declaration (or by returning the string directly) the function works.
1318
// =============> write your new code here
19+
function capitalise(str) {
20+
return `${str[0].toUpperCase()}${str.slice(1)}`;
21+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Predict and explain first...
2-
32
// Why will an error occur when this program runs?
43
// =============> write your prediction here
4+
// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming.
55

66
// Try playing computer with the example to work out what is going on
77

@@ -18,3 +18,10 @@ console.log(decimalNumber);
1818

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

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +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+
// I predict that the error will be a SyntaxError because we are trying to declare a function with an invalid name. In JavaScript, function names cannot start with a number. By trying to declare a function named `square(3)`, we are violating this rule, which will result in a SyntaxError when the code is parsed.
8+
79

810
function square(3) {
911
return num * num;
1012
}
1113

1214
// =============> write the error message here
13-
15+
// SyntaxError due to an unexpected token or invalid function name.
1416
// =============> explain this error message here
15-
17+
// This is because `square(3)` is not a valid function declaration in JavaScript, and the parser will not be able to understand it as a function definition.
1618
// Finally, correct the code to fix the problem
1719

1820
// =============> write your new code here
21+
function square(num) {
22+
return num * num;
23+
}
1924

2025

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

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

33
// =============> write your prediction here
4+
// I predict that the error will be a ReferenceError because the function `multiply` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The result of multiplying 10 and 32 is undefined" instead of the expected product of 10 and 32.
45

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

1112
// =============> write your explanation here
12-
13+
// The error occurs because the `multiply` function does not return a value; it only logs the result to the console. When we use `multiply(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return statement in the function. To fix this, we need to add a return statement to the `multiply` function so that it returns the product of `a` and `b` instead of just logging it.
1314
// Finally, correct the code to fix the problem
1415
// =============> write your new code here
16+
function multiply(a, b) {
17+
return a * b;
18+
}

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

Lines changed: 5 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+
// I predict that the error will be a ReferenceError because the function `sum` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The sum of 10 and 32 is undefined" instead of the expected sum of 10 and 32.
44
function sum(a, b) {
55
return;
66
a + b;
@@ -9,5 +9,9 @@ function sum(a, b) {
99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
// The error occurs because the `sum` function does not return a value; it only has a return statement without any expression. When we use `sum(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return value from the function. To fix this, we need to return the result of `a + b` instead of just having a return statement with no value.
1213
// Finally, correct the code to fix the problem
1314
// =============> write your new code here
15+
function sum(a, b) {
16+
return a + b;
17+
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Predict and explain first...
2-
2+
// this function should square any number but instead we're going to get an error
33
// Predict the output of the following code:
44
// =============> Write your prediction here
5-
5+
// I predict that the output will be:
6+
// The last digit of 42 is 3
7+
// The last digit of 105 is 3
8+
// The last digit of 806 is 3
69
const num = 103;
710

811
function getLastDigit() {
@@ -15,10 +18,19 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1518

1619
// Now run the code and compare the output to your prediction
1720
// =============> write the output here
21+
// The output will be:
22+
// The last digit of 42 is 3
23+
// The last digit of 105 is 3
24+
// The last digit of 806 is 3
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
// The function `getLastDigit` is not taking any parameters, so it always returns the last digit of the global variable `num` (which is 103). This means that regardless of what number is passed to the function, it will always return 3.
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
30+
function getLastDigit(num) {
31+
return num.toString().slice(-1);
32+
}
2233

2334
// This program should tell the user the last digit of each number.
2435
// Explain why getLastDigit is not working properly - correct the problem
36+
// the function `getLastDigit` is not working properly because it does not take any parameters, and it always returns the last digit of the global variable `num`. To fix this, we need to modify the function to accept a parameter (the number we want to find the last digit of) and use that parameter instead of the global variable. This way, we can get the correct last digit for each number passed to the function.

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

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

1717
function calculateBMI(weight, height) {
18+
const bmi = weight / (height * height);
19+
return Math.round(bmi * 10) / 10;
1820
// return the BMI of someone based off their weight and height
1921
}

Sprint-2/3-mandatory-implement/2-cases.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
// You will need to come up with an appropriate name for the function
1515
// Use the MDN string documentation to help you find a solution
1616
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
17+
18+
function toUpperSnakeCase(str) {
19+
return str.toUpperCase().replace(/ /g, '_');
20+
// return the string in UPPER_SNAKE_CASE
21+
}

Sprint-2/3-mandatory-implement/3-to-pounds.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7+
8+
function toPounds(kilograms) {
9+
return kilograms * 2.20462;
10+
// return the weight in pounds
11+
}

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@ function formatTimeDisplay(seconds) {
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
2020
// =============> write your answer here
21+
// The `pad` function will be called three times when `formatTimeDisplay` is called, once for each of the time components: hours, minutes, and seconds. Each component is passed to the `pad` function to ensure it is displayed with at least two digits, adding a leading zero if necessary.
2122

2223
// Call formatTimeDisplay with an input of 61, now answer the following:
2324

2425
// b) What is the value assigned to num when pad is called for the first time?
2526
// =============> write your answer here
27+
// When `formatTimeDisplay(61)` is called, the first time `pad` is called, `num` is assigned the value of `totalHours`, which is 0.
2628

2729
// c) What is the return value of pad is called for the first time?
2830
// =============> write your answer here
31+
// The return value of `pad(0)` is "00".
2932

3033
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3134
// =============> write your answer here
32-
35+
// When `formatTimeDisplay(61)` is called, the last time `pad` is called, `num` is assigned the value of `remainingSeconds`, which is 1.
3336
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3437
// =============> write your answer here
38+
// The return value of `pad(1)` is "01".

0 commit comments

Comments
 (0)