Skip to content

Commit 874f890

Browse files
committed
feat: complete sprint 2 coursework cleanly
1 parent 0823433 commit 874f890

11 files changed

Lines changed: 135 additions & 38 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// A) Error message - str has already been declared
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

7+
// =============> write your new code here
8+
79
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10+
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
11+
return capitalisedStr;
1012
}
11-
12-
// =============> write your explanation here
13-
// =============> write your new code here
13+
console.log(capitalise("hello there"));

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> Decimal number is declared twice in the same scope
55

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

8-
function convertToPercentage(decimalNumber) {
8+
/*function convertToPercentage(decimalNumber) {
99
const decimalNumber = 0.5;
1010
const percentage = `${decimalNumber * 100}%`;
1111
1212
return percentage;
1313
}
1414
15-
console.log(decimalNumber);
15+
console.log(decimalNumber);*/
1616

1717
// =============> write your explanation here
18+
// Decimal number is declared twice & also set as a parameter which causes the syntaxError.
1819

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

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

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

22
// Predict and explain first BEFORE you run any code...
33

4+
//The input to the function is a number, but the function is trying to use that number as a variable name.
45
// this function should square any number but instead we're going to get an error
56

67
// =============> write your prediction of the error here
78

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

1213
// =============> write the error message here
14+
//SyntaxError: Unexpected number
1315

1416
// =============> explain this error message here
17+
// Function parameter '3' is a literal value, not a parameter. Javascript expects a text name for the parameter.
1518

16-
// Finally, correct the code to fix the problem
1719

18-
// =============> write your new code here
20+
// Finally, correct the code to fix the problem
1921

22+
function square(num) {
23+
return num * num;
24+
}
2025

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

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

3-
// =============> write your prediction here
3+
// =============> Error because console.log is within the multiply function does not return a value
4+
45

56
function multiply(a, b) {
67
console.log(a * b);
78
}
89

910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

11-
// =============> write your explanation here
12+
// =============> The console.log inside the function multiply is read first & prints the result of multiplying 10 and 32 - 320, to the console.
13+
// But the aim of the function which was tryin to return it in the template literals is left as undefined because the multiply function does not return a value
14+
//
1215

1316
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
17+
18+
19+
function multiply(a, b) {
20+
return a * b;
21+
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> The function won't return the sum of a and b because the return is between the
3+
// return statement and the expression a + b.
34

4-
function sum(a, b) {
5+
/*function sum(a, b) {
56
return;
67
a + b;
78
}
89
9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/
11+
12+
// =============> returns undefined because the empty return tells the function to return nothing, so the function returns undefined.
1013

11-
// =============> write your explanation here
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
function sum(a, b) {
17+
return a + b;
18+
}
19+
20+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// It will return the last digit of the number 103 which is 3 because the function getLastDigit is using the variable num which is set
5+
// in the const num = 103; and not the parameter passed to the function getLastDigit.
56

6-
const num = 103;
7+
/*const num = 103;
78
89
function getLastDigit() {
910
return num.toString().slice(-1);
1011
}
1112
1213
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1314
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);*/
1516

1617
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18+
// =============> The last digit of 42 is 3
19+
//The last digit of 105 is 3
20+
// The last digit of 806 is 3
21+
1822
// Explain why the output is the way it is
19-
// =============> write your explanation here
23+
// =============> as explained above, the const num = 103; is being used in the function getLastDigit instead of the parameter passed to the function.
2024
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
2225

23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
26+
function getLastDigit(num) {
27+
return num.toString().slice(-1);
28+
}
29+
30+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
31+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
32+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
33+
34+

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

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

1717
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
18+
const heightSquared = height * height;
19+
const bmi = weight / heightSquared;
20+
return bmi.toFixed(1);
21+
}
22+
23+
console.log(calculateBMI(70, 1.73));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@
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(inputString) {
19+
const upperCaseString = inputString.toUpperCase();
20+
const upperSnakeCaseString = upperCaseString.replace(/ /g, "_");
21+
return upperSnakeCaseString;
22+
}
23+
24+
console.log(toUpperSnakeCase("lord of the rings"));

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,27 @@
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(penceString) {
9+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
14+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
15+
const pounds = paddedPenceNumberString.substring(
16+
0,
17+
paddedPenceNumberString.length - 2
18+
);
19+
20+
const pence = paddedPenceNumberString
21+
.substring(paddedPenceNumberString.length - 2)
22+
.padEnd(2, "0");
23+
24+
return ${pounds}.${pence}`;
25+
}
26+
27+
console.log(toPounds("1p"));
28+
console.log(toPounds("90p"));
29+
console.log(toPounds("303p"));
30+
console.log(toPounds("23456p"));

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
2121
// Questions
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// =============> 3
2525

2626
// Call formatTimeDisplay with an input of 61, now answer the following:
2727

2828
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
29+
// =============> 0
3030

3131
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
32+
// =============> 00
3333

3434
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
35+
// =============> 1, the modulo % operator means that 61 gives a remainder of 1
3636

3737
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
38+
// =============> 01, this is so the 1 is padded with a leading 0 to make it 2 characters long.

0 commit comments

Comments
 (0)