Skip to content

Commit be5c268

Browse files
committed
Completed all task required for sprint 2
1 parent 3372770 commit be5c268

File tree

11 files changed

+311
-77
lines changed

11 files changed

+311
-77
lines changed

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// I predict when this code runs, there will be a SyntaxError before the function executes.
3+
// This is because the identifier 'str' has already been declared.
34

45
// call the function capitalise with a string input
6+
// capitalise("hello");
7+
8+
59
// interpret the error message and figure out why an error is occurring
10+
// Uncaught SyntaxError: Unexpected identifier 'string' - this is the error message.
11+
// This error is occuring because the variable 'str' is being redeclared within the
12+
// function, which is not allowed in JavaScript.
13+
// The duplicate use if 'str' is causing the syntax error.
14+
15+
16+
//function capitalise(str) {
17+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
18+
//return str;
19+
//}
20+
21+
// The issue is variable redeclaration.
22+
// str is the function parameter.
23+
// let str tries to create a new variable with the same name.
24+
// JavaScript does not allow redeclaring a variable in the same scope.
25+
// As a result, the code throws a SyntaxError when it encounters the second 'str' declaration.
26+
27+
// New code without the error:
628

29+
// 0.js — fully fixed version
730
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
31+
if (!str) return str; // handles empty string
32+
return str[0].toUpperCase() + str.slice(1);
1033
}
1134

12-
// =============> write your explanation here
13-
// =============> write your new code here
35+
// Test it
36+
console.log(capitalise("hello")); // should print "Hello"
37+
console.log(capitalise("world")); // should print "World"
38+
console.log(capitalise("")); // should print ""

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
// Predict and explain first...
2-
32
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
3+
// I predict an error will occur when this program runs because the variable 'decimalNumber' is already declared inside the function 'convertToPercentage' and cannot be redeclared.
54

65
// Try playing computer with the example to work out what is going on
6+
// When the 'function convertToPercentage(decimalNumber)' is created,
7+
// Javascript already creates a variable called decimalNumber. then, inside the function,
8+
// 'const decimalNumber = 0.5' tries to create another variable with the same name. This
9+
// casues a syntax error because you cannot declare two variables with the same name in the same scope.
710

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
11+
// function convertToPercentage(decimalNumber) {
12+
// const decimalNumber = 0.5;
13+
// const percentage = `${decimalNumber * 100}%`;
1114

12-
return percentage;
13-
}
15+
// return percentage;
16+
// }
1417

15-
console.log(decimalNumber);
18+
// console.log(decimalNumber);
1619

17-
// =============> write your explanation here
20+
// Write your explanation here
21+
// I redelcared the parameter decimalNumber using const.
22+
// I tried to log decimalNumber outside its scope.
23+
// The variables declared inside the function are not available outside unless they are returned.
1824

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

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
21
// Predict and explain first BEFORE you run any code...
2+
// This function should square any number but instead we're going to get an error
33

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
4+
// Write your prediction of the error here:
5+
// I predict that the error will be a syntax error because the parameter '3' is not a valid variable name.
6+
// When you define a function,, the thing inside the parentahese must be a parameter name, NOT a number.
77

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

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

14-
// =============> explain this error message here
15+
// Explain the error messge here:
16+
// When defining a function, inside the parentheses, you need to put a parameter name, which is a variable.
17+
// In this case, '3' is not a valid parameter name because it is a number, not a variable.
18+
// This causes a syntax error because Javascript expects a name, not a number.
1519

1620
// Finally, correct the code to fix the problem
21+
// Write your new code here
1722

18-
// =============> write your new code here
19-
23+
function square(num) {
24+
return num * num;
25+
}
2026

27+
console.log(square(3)); // 9
28+
console.log(square(5)); // 25

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
// Predict and explain first...
1+
// Predict and explain first...write your prediction here
2+
// I predict, when the code runs, it will print 320, but it will also print "The result of multiplying 10 and 32 is undefined".
23

3-
// =============> write your prediction here
4+
//function multiply(a, b) {
5+
// console.log(a * b);
6+
// }
7+
8+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
9+
10+
// Write your explanation here
11+
// The function multiply(a, b) logs the result of a * b to the console but does not return a value.
12+
// When multiply(10, 32) is called within the template literal, it logs 320 to the console but returns undefined.
13+
// Therefore, inside the template literal, the value is undefined, which is why the final output
14+
// becomes "The result of multiplying 10 and 32 is undefined".
15+
16+
// Finally, correct the code to fix the problem
17+
// Write your new code here
18+
// To fix the problem, the function should return the result instead of logging it.
419

520
function multiply(a, b) {
6-
console.log(a * b);
21+
return a * b;
722
}
823

924
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10-
11-
// =============> write your explanation here
12-
13-
// Finally, correct the code to fix the problem
14-
// =============> write your new code here

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
// Predict and explain first...
2-
// =============> write your prediction here
1+
// Predict and explain first...write your prediction here
2+
// I think the code will return undefined.
33

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
4+
//function sum(a, b) {
5+
// return;
6+
// a + b;
7+
//}
88

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
9+
//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10+
11+
// Write your explanation here
12+
// When JavaScript sees the 'return' statement, it immediately stopd the function.
13+
// It does NOT continue to the next line.
14+
// So this part, 'a+b;' never runs. To return the sum of a and b, we need to put it
15+
// on the same line as the return.
16+
// Like the: 'return a + b;'
1017

11-
// =============> write your explanation here
1218
// Finally, correct the code to fix the problem
1319
// =============> write your new code here
20+
21+
function sum(a, b) {
22+
return a + b;
23+
}
24+
25+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// Write your prediction here
5+
// I think the output will be 'The last digit of 42 is 2', 'The last digit of 105 is 5', and 'The last digit of 806 is 6'.
6+
// I think this will happen because the getLastDigit function is supposed to take a number, convert it to a string, and
7+
// then return the last charecter of that string, which should be the last digit.
58

6-
const num = 103;
9+
//const num = 103;
710

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
11+
//function getLastDigit() {
12+
// return num.toString().slice(-1);
13+
//}
14+
15+
//console.log(`The last digit of 42 is ${getLastDigit(42)}`);
16+
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
17+
//console.log(`The last digit of 806 is ${getLastDigit(806)}`);
18+
19+
// Now run the code and compare the output to your prediction
20+
// Write the output here
21+
// The output is:
22+
// The last digit of 42 is 3
23+
// The last digit of 105 is 3
24+
// The last digit of 806 is 3
25+
26+
// Explain why the output is the way it is
27+
// write your explanation here
28+
// The output is 3 for all numbers because the function getLastDigit is not taking any parameters. It always uses the
29+
// global variable `num` which is set to 103. So it always returns the last digit of 103, which is 3.
30+
31+
// Finally, correct the code to fix the problem
32+
// Write your new code here
33+
34+
function getLastDigit(number) {
35+
return number.toString().slice(-1);
1036
}
1137

1238
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1339
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1440
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1541

16-
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18-
// Explain why the output is the way it is
19-
// =============> write your explanation here
20-
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
2242

2343
// This program should tell the user the last digit of each number.
2444
// Explain why getLastDigit is not working properly - correct the problem
45+
// The function did not accept a parameter. It was a fixed variable (num = 103), so it always returned the last digit of 103.

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@
1414
// Then when we call this function with the weight and height
1515
// It should return their Body Mass Index to 1 decimal place
1616

17-
function calculateBMI(weight, height) {
17+
//function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19-
}
19+
// }
20+
21+
// Notes:
22+
// BMI = weight ÷ (height x height)
23+
// eg. BMI = 70kg ÷ (1.73m x 1.73)
24+
// BMI = 70kg ÷ 2.99
25+
// BMI = 23.41
26+
// BMI = 23.4 (to 1 decimal place)
27+
28+
// I need to:
29+
// 1. Square the height
30+
// 2. Divide weight by squared height
31+
// 3. Round the result to 1 decimal place
32+
// 4. Return the result
33+
34+
function calculateBMI(weight, height) {
35+
const bmi = weight / (height * height);
36+
return bmi.toFixed(1);
37+
}
38+
39+
console.log(calculateBMI(70, 1.73));
40+
41+
42+
43+
//toFixed(1) tells JavaScript to round the number to 1 decimal place.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@
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+
// NOTES:
19+
// "hello there" should become "HELLO_THERE"
20+
// I need to turn all letters into CAPITALS and replace the space " " with underscore "-"
21+
22+
// From the MDN documentation:
23+
// I can use the toUpperCase() to turn the tring into all CAPS.
24+
// I can use the replaceAll() to replace all the spaces with underscores.
25+
26+
// Good function name:
27+
// toUpperSnakeCase - this name is decriptive and indicates that the function will convert a string to upper snake case.
28+
29+
function toUpperSnakeCase(str) {
30+
return str.toUpperCase().replaceAll(" ", "_");
31+
}
32+
33+
console.log(toUpperSnakeCase("hello there"));
34+
// HELLO_THERE
35+
36+
console.log(toUpperSnakeCase("lord of the rings"));
37+
// LORD_OF_THE_RINGS
Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
11
// In Sprint-1, there is a program written in interpret/to-pounds.js
22

3+
// ORIGINAL CODE:
4+
//const penceString = "399p";
5+
6+
//const penceStringWithoutTrailingP = penceString.substring(
7+
// 0,
8+
// penceString.length - 1
9+
//);
10+
11+
//const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
12+
//const pounds = paddedPenceNumberString.substring(
13+
// 0,
14+
// paddedPenceNumberString.length - 2
15+
//);
16+
17+
//const pence = paddedPenceNumberString
18+
//.substring(paddedPenceNumberString.length - 2)
19+
//.padEnd(2, "0");
20+
21+
//console.log(`£${pounds}.${pence}`);
22+
23+
324
// You will need to take this code and turn it into a reusable block of code.
425
// You will need to declare a function called toPounds with an appropriately named parameter.
526

6-
// You should call this function a number of times to check it works for different inputs
27+
// You should call this function a number of times to check it works for different inputs.
28+
// For example:
29+
// toPounds("399p") should return "£3.99"
30+
// toPounds("45p") should return "£0.45"
31+
// toPounds("9p") should return "£0.09"
32+
// toPounds("1200p") should return "£12.00"
33+
34+
// The current code removes the "p", makes sur ethe number has at least 3 digits, splits pounds and
35+
// pences and prints it in money format.
36+
// Right now, it only works for "399p". I need it to work for "5p" and "1234p".
37+
// I need to turn it into a function. eg. 'function toPounds(penceString)'. penceString clearly
38+
// decribes what the input is.
39+
40+
function toPounds(penceString) {
41+
const penceStringWithoutTrailingP = penceString.substring(
42+
0,
43+
penceString.length - 1
44+
);
45+
46+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
47+
48+
const pounds = paddedPenceNumberString.substring(
49+
0,
50+
paddedPenceNumberString.length - 2
51+
);
52+
53+
const pence = paddedPenceNumberString
54+
.substring(paddedPenceNumberString.length - 2)
55+
.padEnd(2, "0");
56+
57+
return ${pounds}.${pence}`;
58+
}
59+
60+
console.log(toPounds("399p")); // £3.99
61+
console.log(toPounds("45p")); // £0.45
62+
console.log(toPounds("9p")); // £0.09
63+
console.log(toPounds("1200p")); // £12.00

0 commit comments

Comments
 (0)