Skip to content

Commit e4d1c9e

Browse files
committed
Changes made as per the coursework
1 parent 3372770 commit e4d1c9e

File tree

10 files changed

+128
-41
lines changed

10 files changed

+128
-41
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// It will give a syntax error on using let str > write your prediction here
33

4-
// call the function capitalise with a string input
4+
// call the function capitalise with a string input - did it with the console.log
55
// interpret the error message and figure out why an error is occurring
66

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
7+
//function capitalise(str) {
8+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
// return str;
10+
// }
11+
// console.log(capitalise("newbell"));
1112

1213
// =============> write your explanation here
14+
// str is a parameter which again is getting declared in line 2 of the code
1315
// =============> write your new code here
16+
function capitalise(str) {
17+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
18+
return str;
19+
}
20+
console.log(capitalise("newbell"));

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> write your prediction here - It will throw a syntax error
55

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

8+
//function convertToPercentage(decimalNumber) {
9+
// const decimalNumber = 0.5;
10+
// const percentage = `${decimalNumber * 100}%`;
11+
//
12+
// return percentage;
13+
//}
14+
//
15+
//console.log(decimalNumber);
16+
17+
// =============> write your explanation here - We cannot redeclare decimalNumber in the function since it is already a parameter
18+
19+
// Finally, correct the code to fix the problem
20+
// =============> write your new code here
821
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1022
const percentage = `${decimalNumber * 100}%`;
1123

1224
return percentage;
1325
}
1426

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
18-
19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
27+
console.log(convertToPercentage (0.5));

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
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+
// =============> write your prediction of the error here - Here the syntax error will be thrown
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 - SyntaxError: Unexpected number
1313

14-
// =============> explain this error message here
14+
// =============> explain this error message here : Here we cannot put a number in the function. It should be the variable name
1515

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

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

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

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

3-
// =============> write your prediction here
3+
// =============> write your prediction here - The return in the function is missing.
44

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
5+
//function multiply(a, b) {
6+
//console.log(a * b);
7+
//}
88

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

11-
// =============> write your explanation here
11+
// =============> write your explanation here - Here we did not put the return statement since it prints the answer.
1212

1313
// Finally, correct the code to fix the problem
1414
// =============> write your new code here
15+
function multiply(a, b) {
16+
return a * b;
17+
}
18+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> write your prediction here - there is semicolon after return which should not be there
33

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

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

11-
// =============> write your explanation here
11+
// =============> write your explanation here - Since there is a semi colon after return, it will throw an error which needs to be modified
1212
// Finally, correct the code to fix the problem
1313
// =============> write your new code here
14+
function sum(a, b) {
15+
return a + b;
16+
}
17+

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> Write your prediction here - There is no num parameter defined in the getLastDigit(). The parenthesis is blank
55

6-
const num = 103;
6+
//const num = 103;
77

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
8+
//function getLastDigit() {
9+
// return num.toString().slice(-1);
10+
//}
1111

1212
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1313
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+
// =============> write the output here - The output shows 3 for all
1818
// Explain why the output is the way it is
19-
// =============> write your explanation here
19+
// =============> write your explanation here - Since the parameter num is not defined, it will always pick up the const number and hence ignore all other numbers
2020
// Finally, correct the code to fix the problem
2121
// =============> write your new code here
22+
const num = 103;
23+
24+
function getLastDigit(num) {
25+
return num.toString().slice(-1);
26+
}
27+
28+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
29+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
30+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2231

2332
// This program should tell the user the last digit of each number.
2433
// Explain why getLastDigit is not working properly - correct the problem

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 Number(bmi.toFixed(1));
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+
function toUpperSnakeCase(str) {
18+
return str
19+
.toUpperCase()
20+
.replaceAll(" ","_");
21+
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,45 @@
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+
//const penceString = "399p";
8+
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+
//console.log(`£${pounds}.${pence}`);
25+
26+
function toPounds(penceString) {
27+
const penceStringWithoutTrailingP = penceString.substring(
28+
0,
29+
penceString.length - 1
30+
);
31+
32+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
33+
34+
const pounds = paddedPenceNumberString.substring(
35+
0,
36+
paddedPenceNumberString.length - 2
37+
);
38+
39+
const pence = paddedPenceNumberString
40+
.substring(paddedPenceNumberString.length - 2)
41+
.padEnd(2, "0");
42+
43+
return ${pounds}.${pence}`;
44+
}
45+
console.log(toPounds("399p")); // £3.99
46+
console.log(toPounds("50p")); // £0.50
47+
console.log(toPounds("5p")); // £0.05
48+
console.log(toPounds("12345p")); // £123.45

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ 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+
// =============> write your answer here`
21+
// It will be called 3 times
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+
//0
2628

2729
// c) What is the return value of pad is called for the first time?
2830
// =============> write your answer here
31+
//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
35+
//When pad is called for the last time, the value assigned is 1. The remainder is 1 after dividine by 60
3236

3337
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3438
// =============> write your answer here
39+
// 01 - since padStart(2, "0") adds 0 to the front because the string length is

0 commit comments

Comments
 (0)