Skip to content

Commit 163a0fb

Browse files
complete coursework
1 parent d28242a commit 163a0fb

10 files changed

Lines changed: 107 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> It should take a string and capitalise the first letter but i predict it will throw an
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+
/*The error occurs because str is declared twice. The function already receives str as a parameter, and then let str tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring a variable with let, so it throws an error.*/
8+
79
function capitalise(str) {
810
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
911
return str;
1012
}
1113

1214
// =============> write your explanation here
15+
16+
/* This function takes a string and capitalise the first letter. It uses `str[0].toUpperCase()` to convert the first character to uppercase and `str.slice(1)` to get the rest of the word unchanged. The function then joins both parts together and returns the new string. When `console.log(capitalise("hello"), capitalise("world"))` is run, the function executes twice and prints `Hello World`.
17+
*/
18+
1319
// =============> write your new code here
20+
function capitalise(str) {
21+
return `${str[0].toUpperCase()}${str.slice(1)}`;
22+
}
23+
console.log(capitalise("hello"), capitalise("world"));

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Predict and explain first...
2-
32
// Why will an error occur when this program runs?
43
// =============> write your prediction here
54

@@ -16,5 +15,15 @@ console.log(decimalNumber);
1615

1716
// =============> write your explanation here
1817

18+
/*An error will occur when the program runs because decimalNumber is declared twice. The function already receives decimalNumber as a parameter, but then const decimalNumber = 0.5 tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring variables with const.
19+
Another error will happen at console.log(decimalNumber) because decimalNumber only exists inside the function and cannot be accessed outside of it.*/
20+
1921
// Finally, correct the code to fix the problem
2022
// =============> write your new code here
23+
function convertToPercentage(decimalNumber) {
24+
const percentage = `${decimalNumber * 100}%`;
25+
26+
return percentage;
27+
}
28+
29+
console.log(convertToPercentage(0.5));

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
/*An error will occur because a number was used as a function parameter instead of a variable name.*/
78

89
function square(3) {
910
return num * num;
1011
}
1112

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

1416
// =============> explain this error message here
17+
/*The error happens because JavaScript expects a variable name inside the function brackets, but it found a number instead.*/
1518

1619
// Finally, correct the code to fix the problem
1720

1821
// =============> write your new code here
1922

23+
function square(num) {
24+
return num * num
25+
}
26+
console.log(square(3));
2027

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

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

33
// =============> write your prediction here
4+
/*The code will print a correct multiplication inside the function*/
45

56
function multiply(a, b) {
67
console.log(a * b);
@@ -9,6 +10,13 @@ function multiply(a, b) {
910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
/*The result shows undefined because the function prints the answer with console.log instead of returning it, so nothing is passed back into the template string.*/
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+
}
21+
22+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
//The sum of 10 and 32 is undefined
44
function sum(a, b) {
55
return;
66
a + b;
@@ -9,5 +9,14 @@ function sum(a, b) {
99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
13+
/*The function returns undefined because the return; statement ends the function immediately, so the calculation a + b never runs or gets returned.*/
14+
1215
// Finally, correct the code to fix the problem
1316
// =============> write your new code here
17+
18+
function sum(a, b) {
19+
return a + b;
20+
}
21+
22+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
/*The output will be:
6+
7+
The last digit of 42 is 3
8+
The last digit of 105 is 3
9+
The last digit of 806 is 3*/
510

611
const num = 103;
712

@@ -15,10 +20,28 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1520

1621
// Now run the code and compare the output to your prediction
1722
// =============> write the output here
23+
24+
/*The last digit of 42 is 3
25+
The last digit of 105 is 3
26+
The last digit of 806 is 3*/
27+
1828
// Explain why the output is the way it is
1929
// =============> write your explanation here
30+
31+
/*The function always returns the last digit of 103 because it ignores the input parameter and uses a fixed global variable instead of the number passed into it.*/
32+
2033
// Finally, correct the code to fix the problem
2134
// =============> write your new code here
2235

36+
function getLastDigit(number) {
37+
return number.toString().slice(-1);
38+
}
39+
40+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
41+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
42+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
43+
2344
// This program should tell the user the last digit of each number.
2445
// Explain why getLastDigit is not working properly - correct the problem
46+
47+
/*The function is not working properly because it ignores the input values and always uses the global variable num (which is 103), so every result returns the last digit of 103 instead of the number passed into the function.*/

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
// squaring your height: 1.73 x 1.73 = 2.99
88
// dividing 70 by 2.99 = 23.41
9-
// Your result will be displayed to 1 decimal place, for example '23.4'.
9+
// Your result will be displayed to 1 decimal place, for example 23.4.
1010

1111
// You will need to implement a function that calculates the BMI of someone based off their weight and height
1212

1313
// Given someone's weight in kg and height in metres
1414
// Then when we call this function with the weight and height
15-
// It should return a string of their Body Mass Index to 1 decimal place
15+
// 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
18+
const bmi = weight / (height * height);
19+
return Number(bmi.toFixed(1)); // return the BMI of someone based off their weight and height
1920
}
21+
console.log(calculateBMI(80, 1.73));

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@
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(str) {
19+
return str
20+
.split(" ")
21+
.map((word) => word.toUpperCase())
22+
.join("_");
23+
}
24+
25+
console.log(toUpperSnakeCase("hello there"));
26+
console.log(toUpperSnakeCase("lord of the rings"));
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js
1+
// In Sprint-1, there is a program written in interpret/to-pounds.js
22

33
// You will need to take this code and turn it into a reusable block of code.
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 withoutP = penceString.slice(0, -1);
10+
11+
const padded = withoutP.padStart(3, "0");
12+
13+
const pounds = padded.slice(0, -2);
14+
const pence = padded.slice(-2);
15+
16+
return `£${pounds}.${pence}`;
17+
}
18+
19+
console.log(toPounds("399p"));
20+
console.log(toPounds("45p"));
21+
console.log(toPounds("5p"));

‎Sprint-3/4-mandatory-interpret/time-format.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@ function formatTimeDisplay(seconds) {
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
2424
// =============> write your answer here
25+
// a) 3 times
2526

2627
// Call formatTimeDisplay with an input of 61, now answer the following:
2728

2829
// b) What is the value assigned to num when pad is called for the first time?
2930
// =============> write your answer here
31+
// b) 0
3032

31-
// c) What is the return value of pad when it is called for the first time?
33+
// c) What is the return value of pad is called for the first time?
3234
// =============> write your answer here
35+
// c) "00"
3336

3437
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3538
// =============> write your answer here
39+
// d) 1, because the last time pad is called it is for the remaining seconds which is 1 second in this case
3640

3741
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3842
// =============> write your answer here
43+
44+
// e) "01", because the last time pad is called it is for the remaining seconds which is 1 second in this case, and pad adds a leading zero to make it two digits

0 commit comments

Comments
 (0)