Skip to content

Commit 4d85df1

Browse files
committed
submission Sprint-2 work
1 parent b31a586 commit 4d85df1

11 files changed

Lines changed: 124 additions & 36 deletions

File tree

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

Lines changed: 5 additions & 5 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

77
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
8+
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
return capitalisedStr;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
console.log(capitalise("hello there"));
13+

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
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

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

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

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
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+
// The input to the function is a number, but the function is trying to use that number as a variable name.
77

88
function square(3) {
99
return num * num;
1010
}
1111

12-
// =============> write the error message here
12+
// =============> /SyntaxError: Unexpected number
1313

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

1616
// Finally, correct the code to fix the problem
1717

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

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
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
44

55
function multiply(a, b) {
66
console.log(a * b);
77
}
88

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

11-
// =============> write your explanation here
11+
// =============> The console.log inside the function multiply is read first & prints the result of multiplying 10 and 32 - 320, to the console.
12+
// 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
13+
//
1214

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

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
3-
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.
44
function sum(a, b) {
55
return;
66
a + b;
77
}
88

99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

11-
// =============> write your explanation here
11+
// =============> returns undefined because the empty return tells the function to return nothing, so the function returns undefined.
12+
1213
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
14+
function sum(a, b) {
15+
return a + b;
16+
}
17+
18+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// in the const num = 103; and not the parameter passed to the function getLastDigit.
55

66
const num = 103;
77

@@ -14,11 +14,21 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

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

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

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
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+
1718
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
19+
const heightSquared = height * height;
20+
const bmi = weight / heightSquared;
21+
return bmi.toFixed(1);
22+
}
23+
24+
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)