Skip to content

Commit a70897f

Browse files
committed
Complete Sprint 3 exercises
1 parent 5d55703 commit a70897f

10 files changed

Lines changed: 130 additions & 58 deletions

File tree

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
3-
2+
//This code will throw an error as we haven't passed an argument into str? or str is the function parameter and variable already on line 5 so it may throw an error to declare again as a variable inside the function?
43
// call the function capitalise with a string input
54
// interpret the error message and figure out why an error is occurring
6-
5+
//function capitalise(str) {
6+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
7+
// return str;
8+
//}
9+
//console.log(capitalise("mars"));
10+
//SyntaxError: Identifier 'str' has already been declared
11+
// This code threw an error as "str" has already been defined in the function capitalise(str) so let str is causing the error as it cannot be defined twice.
712
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
13+
let result = `${str[0].toUpperCase()}${str.slice(1)}`;
14+
return result;
1015
}
11-
12-
// =============> write your explanation here
13-
// =============> write your new code here
16+
console.log(capitalise("mars"));
17+
// In my new code i'm calling the function capitalise(str) in my code "str" is only the parameter and not the variable aswell. In the variable result it will take any argument and using the toUpperCase method itll make a copy of the the letter at index zero as a capital letter so as i passed in "mars" itll return M and store it in the variable result. The method slice will return a copy of the argument from index 1 onwards so "ars" and because it's in a template literal it will join "M" and "ars" together and this is what will get stored in result and by returning result on line 14 i'm asking the function to return what is stored in result so javascript can print it using console.log or i can use it elsewhere as i'm coding.

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// like the original code in 0.js a syntax error will occur because decimalNumber has already been declared as the parameter in the function so it cannot be redeclared inside the function with the same name.
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+
//Because decimalNumber is declared twice it wont work but when i call the function convertToPercentage and pass in 0.5 it will get assigned to the parameter decimalNumber so i need to update the code to reflect this.
17+
818
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1019
const percentage = `${decimalNumber * 100}%`;
11-
1220
return percentage;
1321
}
22+
console.log(convertToPercentage(0.5));
1423

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
24+
//console output: 50%

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

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

6-
// =============> write your prediction of the error here
5+
// I think this code will throw an error because we are passing an actual number into the parameters but javascript only allows a variable to be passed into the parameters that can be used to store information so i would need to change the number 3 into num ( and num here would act as the placeholder for any number passed in when the function gets called later) for this code to work because as it is i believe num would be undefined as it hasn't been defined anywhere.
76

8-
function square(3) {
9-
return num * num;
10-
}
7+
//function square(3) {
8+
// return num * num;
9+
//}
1110

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

14-
// =============> explain this error message here
13+
// This error means javascript couldn't run the code because it doesn't accept numbers as a parameter.
1514

1615
// Finally, correct the code to fix the problem
1716

18-
// =============> write your new code here
17+
function square(num) {
18+
let numResult = num * num;
1919

20+
return numResult;
21+
}
2022

23+
console.log(square(3));

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

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

3-
// =============> write your prediction here
3+
// here we are declaring a function called multiply which takes in two arguments and multiplies them but because the function doesn't return the value when we call the function it hasnt been stored anywhere for it to be called upon so i think it will return undefined in the template literal. I also think because there are two console logs one will print the value 320 and the other will print the template literal but as multiply hasn't been stored anywhere it will print undefined ?
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+
//320
12+
//The result of multiplying 10 and 32 is undefined
13+
// the function as it is only storing the result of a and b in two console logs one asking for the total which is 320 so it prints it and the other is template literal so it prints the template literal but multiply is undefined.
1214

1315
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
16+
function multiply(a, b) {
17+
let multiplyResult = a * b;
18+
return multiplyResult;
19+
}
20+
21+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// The function will not return the expected result because whilst sum takes in two arguments it cannot add a+b together because it hasn't been stored in a variable before return so when we call sum it will be undefined?
3+
4+
//function sum(a, b) {
5+
// return;
6+
// a + b;
7+
//}
8+
9+
//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10+
//console output: The sum of 10 and 32 is undefined
11+
// sum is undefined because we haven't stored the operation we want it to perform correctly in a variable so its value is undefined.
12+
// Finally, correct the code to fix the problem
313

414
function sum(a, b) {
5-
return;
6-
a + b;
15+
let sumResult = a + b;
16+
return sumResult;
717
}
8-
918
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10-
11-
// =============> write your explanation here
12-
// Finally, correct the code to fix the problem
13-
// =============> write your new code here

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// here we have a constant variable called num which stores the number 103 we then have declared the function getLastDigit which has an empty parameter we are then asking it to return the value stored inside num and apply the .toString method to it which turns the number 103 into a string "103" we are then taking "103" and applying the .slice method to it which will start from position -1 and return everything after it which leaves us with the string "3" then outside of the function, the function getLastDigit is being called 3 times in template literal using console.log with the arguments 42, 105 and 806 but because the variable num = "3" it will print the template literal with the "3" in the console when javascript gets to the function call for each value, which isn't the expected result.
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

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)}`);
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)}`);
1515

1616
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
17+
// output: The last digit of 42 is 3
18+
//The last digit of 105 is 3
19+
//The last digit of 806 is 3
1820
// Explain why the output is the way it is
19-
// =============> write your explanation here
21+
// the console output prints this because the the methods have only been applied to the number stored in the num variable so javascript isn't able to access the numbers passed in when the function is called in the template literal as the function getLastDigit() has no parameter so it passes in the value stored in the num variable which is "3" and so every time the function is called "3" is returned.
2022
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
23+
function getLastDigit(num) {
24+
let getLastDigitResult = num.toString().slice(-1);
25+
return getLastDigitResult;
26+
}
27+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
28+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
29+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2230

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

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,11 @@
1515
// It should return a string of 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+
// Store height squared (calculation wont change so can be stored in const)
19+
const heightSquared = height * height;
20+
// Store weight divided by height squared (calculation wont change so can be stored in const)
21+
const bmi = weight / heightSquared;
22+
//return bmi to 1 decimal place
23+
return bmi.toFixed(1);
1924
}
25+
console.log(calculateBMI(70, 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+
// define function
19+
function stringToUpperCase(str) {
20+
//store string in upper snake case (calculation wont change store in const)
21+
const stringToUpperCaseResult = str.toUpperCase().replaceAll(" ", "_");
22+
// return result
23+
return stringToUpperCaseResult;
24+
}
25+
// call function
26+
console.log(stringToUpperCase("welcome to cyf"));

‎Sprint-3/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(pence) {
9+
//Declare penceString variable
10+
let penceString = pence;
11+
// Store penceString value with pence on the end and make sure its at least 3 characters long add 0 at start if needed.
12+
let penceStringWithoutTrailingP = penceString
13+
.substring(0, penceString.length - 1)
14+
.padStart(3, "0");
15+
// copy everything apart from the last two characters
16+
let poundsResult = penceStringWithoutTrailingP.substring(
17+
0,
18+
penceStringWithoutTrailingP.length - 2,
19+
);
20+
// start at the last two characters and take everything from there to end and make sure that value is two characters long add 0 if needed
21+
let penceResult = penceStringWithoutTrailingP
22+
.substring(penceStringWithoutTrailingP.length - 2)
23+
.padEnd(2, "0");
24+
// return result of poundResult and penceResult in a template string
25+
return `£${poundsResult}.${penceResult}`;
26+
}
27+
//call function to see if works with different inputs
28+
console.log(toPounds("399p"));
29+
console.log(toPounds("1000p"));
30+
console.log(toPounds("15000p"));

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@ function formatTimeDisplay(seconds) {
1515
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1616
}
1717

18+
console.log(formatTimeDisplay(61));
19+
1820
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1921
// to help you answer these questions
2022

2123
// Questions
2224

2325
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
26+
// Pad will be called 3 times which is seen on line 15
2527

2628
// Call formatTimeDisplay with an input of 61, now answer the following:
2729

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

3133
// c) What is the return value of pad when it is called for the first time?
32-
// =============> write your answer here
34+
// the return value of pad when called for the first time is 00
3335

3436
// 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
37+
// 1 second is stored in remainingSeconds so the value 1 is assigned to num when pad is called for the last time.
3638

3739
// 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
40+
// the return value is 01 as the variable numString stores "01"

0 commit comments

Comments
 (0)