Skip to content

Commit 5ce055d

Browse files
committed
Complete Sprint 3 coursework
1 parent f93c281 commit 5ce055d

11 files changed

Lines changed: 296 additions & 43 deletions

File tree

‎Sprint-3/1-key-errors/0.js‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ======> write your prediction here: I predict that the code should cut out, and make the first letter of the string an uppercase letter,
3+
// then add every letters that comes after
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
67

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
8+
// function capitalise(str) {
9+
// let str= `${str[0].toUpperCase()}${str.slice(1)}`
10+
// return str;
11+
// }
12+
13+
14+
// =============> write your explanation here: The error read "SyntaxError: Identifier 'str' has already been declared",
15+
// this is as a result of having two variables with same name. The error occoured in this code because the variable "str" has been decleared twice
1116

12-
// =============> write your explanation here
1317
// =============> write your new code here
18+
19+
function capitalise(str) {
20+
let str2= `${str[0].toUpperCase()}${str.slice(1)}`
21+
return str2;
22+
}
23+
console.log(capitalise("money"))

‎Sprint-3/1-key-errors/1.js‎

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// Answer below:
5+
// A syntax error will occur for two reasons, first because there variable const "decimalNumber" being redecleared
6+
// The second reason is because the function "convertToPercentage" is not called correctly.
57

6-
// Try playing computer with the example to work out what is going on
8+
// =============> write your prediction here:
9+
// Answer below:
10+
// I predicted that the code will not work because of the variable (decimalNumber) inside the function,
11+
// Also because the console.log(decimalNumber) is just a name that exist inside the function
712

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
1113

12-
return percentage;
13-
}
14+
// Try playing computer with the example to work out what is going on
15+
16+
// function convertToPercentage(decimalNumber) {
17+
// const decimalNumber = 0.5;
18+
// const percentage = `${decimalNumber * 100}%`;
1419

15-
console.log(decimalNumber);
20+
// return percentage;
21+
// }
22+
// console.log(decimalNumber);
1623

1724
// =============> write your explanation here
25+
// Answer:
26+
27+
// const decimalNumber redeclares the parameter name which causes a SyntaxError.
28+
// also console.log(decimalNumber) outside fails because the parameter only exists inside the function"
1829

1930
// Finally, correct the code to fix the problem
2031
// =============> write your new code here
32+
33+
function convertToPercentage(decimalNumber) {
34+
const percentage = `${decimalNumber * 100}%`;
35+
36+
return percentage;
37+
}
38+
console.log(convertToPercentage(10.5));

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11

22
// Predict and explain first BEFORE you run any code...
3+
// Answer below:
4+
// The code will not work because the placeholder variable (3) should be an actual name like "num"
5+
36

47
// this function should square any number but instead we're going to get an error
58

69
// =============> write your prediction of the error here
710

8-
function square(3) {
9-
return num * num;
10-
}
11+
// function square(3) {
12+
// return num * num;
13+
// }
1114

1215
// =============> write the error message here
16+
// SyntaxError: Unexpected number
1317

1418
// =============> explain this error message here
19+
// The error message explains the error in the function parameter (3), the function expects a name parameter instead of a number.
1520

1621
// Finally, correct the code to fix the problem
1722

1823
// =============> write your new code here
1924

25+
function square(num) {
26+
return num * num;
27+
}
28+
console.log(square(10))
29+
30+
2031

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
// Predict and explain first...
2+
// Answer below:
3+
// The code needs a value inside the console.log function to multiply, but no value was useed to replace the placeholder parameters
4+
// It should be an error, no value to return
25

36
// =============> write your prediction here
47

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
8+
// function multiply(a, b) {
9+
// console.log(a * b);
10+
// }
11+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
812

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

1114
// =============> write your explanation here
15+
// Answer below:
16+
// The original logs the product instead of returning it, so the function call inside the template string gives undefined
1217

1318
// Finally, correct the code to fix the problem
1419
// =============> write your new code here
20+
21+
function multiply(a, b) {
22+
return a * b
23+
}
24+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// Code will not work because the paremeters should be on same line as return
34

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
5+
// function sum(a, b) {
6+
// return;
7+
// a + b;
8+
// }
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)}`);
1011

1112
// =============> write your explanation here
13+
// The errors in this codes is the semi-colon in front of the return and the placeholder parameters not on same line as the return
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
17+
function sum(a, b) {
18+
return a + b;
19+
}
20+
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,48 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// Answer below:
6+
// There is a constant variable num with value 103, the code should print last digit of the constant variable.
57

6-
const num = 103;
8+
// const num = 103;
79

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

12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
// Answer below:
21+
// The last digit of 42 is 3
22+
// The last digit of 105 is 3
23+
// The last digit of 806 is 3
24+
25+
1826
// Explain why the output is the way it is
1927
// =============> write your explanation here
28+
// Answer below:
29+
// The code kept repeating same value for all log because of the constant variable decleared before the function and used inside the function.
30+
2031
// Finally, correct the code to fix the problem
2132
// =============> write your new code here
2233

34+
function getLastDigit(num) {
35+
return num.toString().slice(-1);
36+
}
37+
38+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
39+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
40+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
41+
42+
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+
46+
// Answer below:
47+
48+
// The global constant variable outside the function has affected the code and caused it to always reference the last number of the global variable
49+
// To fix this, getLastDigit needed its own parameter, so num comes from whatever you pass in

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
// It should return a string of their Body Mass Index to 1 decimal place
1616

1717
function calculateBMI(weight, height) {
18+
const bmi = weight / (height * height)
19+
return bmi.toFixed(1)
1820
// return the BMI of someone based off their weight and height
1921
}
22+
console.log(`Your BMI is ${calculateBMI(100, 1.62)}`)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
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 upperCase(str) {
19+
const returnUpperCase = str.toUpperCase().replaceAll(" ", "_")
20+
return returnUpperCase
21+
}
22+
console.log(upperCase("what is your name mr man? "))

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,43 @@
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+
9+
// const penceString = "399p";
10+
11+
// const penceStringWithoutTrailingP = penceString.substring(
12+
// 0,
13+
// penceString.length - 1
14+
// );
15+
16+
// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
17+
// const pounds = paddedPenceNumberString.substring(
18+
// 0,
19+
// paddedPenceNumberString.length - 2
20+
// );
21+
22+
// const pence = paddedPenceNumberString
23+
// .substring(paddedPenceNumberString.length - 2)
24+
// .padEnd(2, "0");
25+
26+
// console.log(`£${pounds}.${pence}`);
27+
28+
29+
function toPounds(inPounds) {
30+
const penceStringWithoutTrailingP = inPounds.substring(
31+
0,
32+
inPounds.length - 1
33+
);
34+
35+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
36+
const pounds = paddedPenceNumberString.substring(
37+
0,
38+
paddedPenceNumberString.length - 2
39+
);
40+
41+
const pence = paddedPenceNumberString
42+
.substring(paddedPenceNumberString.length - 2)
43+
.padEnd(2, "0");
44+
return `£${pounds}.${pence}`;
45+
}
46+
console.log(toPounds("599p"))
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
function pad(num) {
2+
// console.log(num)
23
let numString = num.toString();
4+
// console.log(numString, "numstr")
35
while (numString.length < 2) {
46
numString = "0" + numString;
57
}
8+
// console.log(numString, "FULLNUM")
69
return numString;
710
}
811

12+
913
function formatTimeDisplay(seconds) {
1014
const remainingSeconds = seconds % 60;
1115
const totalMinutes = (seconds - remainingSeconds) / 60;
@@ -14,25 +18,38 @@ function formatTimeDisplay(seconds) {
1418

1519
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1620
}
21+
console.log(formatTimeDisplay(61))
22+
23+
24+
1725

1826
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1927
// to help you answer these questions
2028

2129
// Questions
2230

2331
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
32+
// =============> write your answer here
33+
// Answer below:
34+
// Pad runs once for "totalHours", once "remainingMinutes" and once for "remainingSeconds" in total pad is called 3 times.
2535

2636
// Call formatTimeDisplay with an input of 61, now answer the following:
2737

2838
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
39+
// =============> write your answer here
40+
// Answer below:
41+
// The first pad is pad(totalHours) and the value is 0
3042

3143
// c) What is the return value of pad when it is called for the first time?
3244
// =============> write your answer here
45+
// The value is 0 plus the "0" added to it to make the value 00
3346

3447
// 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
48+
// =============> write your answer here
49+
// Answer below:
50+
// The last call is pad(remainingSeconds) and the value is 1
3651

3752
// 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
53+
// =============> write your answer here
54+
// Answer below:
55+
// the return value initially is 1 and "0" was added to it in front to make the value 01

0 commit comments

Comments
 (0)