Skip to content

Commit 9fd99c2

Browse files
coursework/sprint-2
1 parent 1cf9289 commit 9fd99c2

File tree

11 files changed

+90
-29
lines changed

11 files changed

+90
-29
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> Capitalises the first letter of the str
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,7 @@ function capitalise(str) {
99
return str;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
capitalise("mohsen");
13+
14+
// =============> variable str is declared again inside function
15+
// =============> str = `${str[0].toUpperCase()}${str.slice(1)}`;

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> Instead of calling the function, a variable is called
55

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

@@ -14,7 +14,7 @@ function convertToPercentage(decimalNumber) {
1414

1515
console.log(decimalNumber);
1616

17-
// =============> write your explanation here
17+
// =============> Also inside the function, decimalNumber is called again
1818

1919
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
20+
// =============> decimalNumber = 0.5;

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

Lines changed: 4 additions & 4 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 variable num is not declared
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+
// =============> There must a variable declared, not just simply passing a number as an argument
1515

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

18-
// =============> write your new code here
18+
// =============> function square(num) {
1919

2020

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

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

3-
// =============> write your prediction here
3+
// =============> the result of the multiply is printed inside function, and at the function call is will be undefined
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+
// =============> because function is not returning any value
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
// =============> return a * b;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> undefined will be printed
33

44
function sum(a, b) {
55
return;
@@ -8,6 +8,6 @@ function sum(a, b) {
88

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

11-
// =============> write your explanation here
11+
// =============> because the function returns nothing
1212
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
13+
// =============> return a + b;

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> the last digit of every function call will be 3
55

66
const num = 103;
77

8-
function getLastDigit() {
8+
function getLastDigit(num) {
99
return num.toString().slice(-1);
1010
}
1111

@@ -16,9 +16,10 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1616
// Now run the code and compare the output to your prediction
1717
// =============> write the output here
1818
// Explain why the output is the way it is
19-
// =============> write your explanation here
19+
// =============> function is using the num declared before it , not the number passed to it
2020
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
21+
// =============> function getLastDigit(num) {
2222

2323
// This program should tell the user the last digit of each number.
2424
// Explain why getLastDigit is not working properly - correct the problem
25+
// Variable num used inside the function, should be passed as an argument

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
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+
// return the BMI of someone based off their weight and height
19+
const bmi = weight / height ** 2;
20+
return bmi.toFixed(1);
21+
}
22+
console.log(calculateBMI(70, 1.73));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
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+
function makeUpperSnakeCase(str) {
18+
return str.toUpperCase().split(" ").join("_");
19+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@
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+
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ function formatTimeDisplay(seconds) {
1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
20+
// =============> 3
21+
formatTimeDisplay(61);
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?
25-
// =============> write your answer here
26+
// =============> 0
2627

2728
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
29+
// =============> It is called return value and will be '00'
2930

3031
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
32+
// =============> The function is passed 61 which means 1 minute and 1 second so the value passed to pad for the last call is 1
3233

3334
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
35+
// =============> 1 is passed to function as a number and stored in num then num is turned into a string and one '0' will be added
36+
// to the beginning of num so the returned value will be '01'

0 commit comments

Comments
 (0)