Skip to content

Commit 0f24ef2

Browse files
committed
Complete Sprint-3 coursework
1 parent 0d560fc commit 0f24ef2

11 files changed

Lines changed: 365 additions & 365 deletions

File tree

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// Predict and explain first...
2-
// I am predicting the code will generate error because the variable str is declared twice
3-
4-
// call the function capitalise with a string input
5-
// interpret the error message and figure out why an error is occurring
6-
7-
/*function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
11-
console.log(capitalise("hello World"));*/
12-
13-
// After we run the code, it generated this error SyntaxError: Identifier str has already been declared
14-
// This happened because we tried to declare again using the keyword let the variable str
15-
// which was declared before in the parameter of the function
16-
17-
//Here is the fixed code
18-
function capitalise(str) {
19-
str = `${str[0].toUpperCase()}${str.slice(1)}`;
20-
return str;
21-
}
22-
console.log(capitalise("hello World"));
1+
// Predict and explain first...
2+
// I am predicting the code will generate error because the variable str is declared twice
3+
4+
// call the function capitalise with a string input
5+
// interpret the error message and figure out why an error is occurring
6+
7+
/*function capitalise(str) {
8+
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
return str;
10+
}
11+
console.log(capitalise("hello World"));*/
12+
13+
// After we run the code, it generated this error SyntaxError: Identifier str has already been declared
14+
// This happened because we tried to declare again using the keyword let the variable str
15+
// which was declared before in the parameter of the function
16+
17+
//Here is the fixed code
18+
function capitalise(str) {
19+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
20+
return str;
21+
}
22+
console.log(capitalise("hello World"));

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
// Predict and explain first...
2-
3-
// Why will an error occur when this program runs?
4-
// I predict the code will generate 2 errors because one the variable decimalNumber is declared twice and two
5-
// the function is not called instead the variable decimalNumber is called and it can't be accessed because
6-
// it was declared locally as a parameter of the function
7-
8-
// Try playing computer with the example to work out what is going on
9-
10-
/*function convertToPercentage(decimalNumber) {
11-
const decimalNumber = 0.5;
12-
const percentage = `${decimalNumber * 100}%`;
13-
14-
return percentage;
15-
}
16-
17-
console.log(decimalNumber);*/
18-
19-
// After we run the code, it generated an error SyntaxError: Identifier 'decimalNumber' has already been declared
20-
// because we tried to declare decimalNumber again. After fixing this error again we got another error ReferenceError: decimalNumber is not defined
21-
// because the decimalNumber is locally scoped, javascript can't access it when the function is called
22-
// additionally the function was not called console.log(decimalNumber) this simply means print the value of decimalNumber
23-
24-
// Finally, correct the code to fix the problem
25-
// Here is the fixed code
26-
function convertToPercentage(decimalNumber) {
27-
const percentage = `${decimalNumber * 100}%`;
28-
29-
return percentage;
30-
}
31-
32-
console.log(convertToPercentage(0.5));
1+
// Predict and explain first...
2+
3+
// Why will an error occur when this program runs?
4+
// I predict the code will generate 2 errors because one the variable decimalNumber is declared twice and two
5+
// the function is not called instead the variable decimalNumber is called and it can't be accessed because
6+
// it was declared locally as a parameter of the function
7+
8+
// Try playing computer with the example to work out what is going on
9+
10+
/*function convertToPercentage(decimalNumber) {
11+
const decimalNumber = 0.5;
12+
const percentage = `${decimalNumber * 100}%`;
13+
14+
return percentage;
15+
}
16+
17+
console.log(decimalNumber);*/
18+
19+
// After we run the code, it generated an error SyntaxError: Identifier 'decimalNumber' has already been declared
20+
// because we tried to declare decimalNumber again. After fixing this error again we got another error ReferenceError: decimalNumber is not defined
21+
// because the decimalNumber is locally scoped, javascript can't access it when the function is called
22+
// additionally the function was not called console.log(decimalNumber) this simply means print the value of decimalNumber
23+
24+
// Finally, correct the code to fix the problem
25+
// Here is the fixed code
26+
function convertToPercentage(decimalNumber) {
27+
const percentage = `${decimalNumber * 100}%`;
28+
29+
return percentage;
30+
}
31+
32+
console.log(convertToPercentage(0.5));

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// Predict and explain first BEFORE you run any code...
2-
3-
// this function should square any number but instead we're going to get an error
4-
5-
// I predict the error will come from the argument because when we write a function we first declare parameters not values
6-
7-
/*function square(3) {
8-
return num * num;
9-
}*/
10-
11-
// SyntaxError: Unexpected number
12-
13-
// this error tells us javascript expects valid variable identifiers when function is defined not values
14-
15-
// Finally, correct the code to fix the problem
16-
17-
// Here is the fixed code
18-
function square(num) {
19-
return num * num;
20-
}
21-
console.log(square(3));
1+
// Predict and explain first BEFORE you run any code...
2+
3+
// this function should square any number but instead we're going to get an error
4+
5+
// I predict the error will come from the argument because when we write a function we first declare parameters not values
6+
7+
/*function square(3) {
8+
return num * num;
9+
}*/
10+
11+
// SyntaxError: Unexpected number
12+
13+
// this error tells us javascript expects valid variable identifiers when function is defined not values
14+
15+
// Finally, correct the code to fix the problem
16+
17+
// Here is the fixed code
18+
function square(num) {
19+
return num * num;
20+
}
21+
console.log(square(3));

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// Predict and explain first...
2-
3-
// I predict the code will run normally and print the result of a * b on the first console, but it will return undefined on the
4-
// second console.log
5-
6-
/*function multiply(a, b) {
7-
console.log(a * b);
8-
}
9-
10-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/
11-
12-
// The reason for printing undefined is that we didn't define a return value so the js doesn't know what to return and it simply returns undefined
13-
// We can fix the code by replacing the console.log with a return keyword so the js knows what to return when the function is called
14-
15-
// Finally, correct the code to fix the problem
16-
// Here is the fixed code
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)}`);
1+
// Predict and explain first...
2+
3+
// I predict the code will run normally and print the result of a * b on the first console, but it will return undefined on the
4+
// second console.log
5+
6+
/*function multiply(a, b) {
7+
console.log(a * b);
8+
}
9+
10+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/
11+
12+
// The reason for printing undefined is that we didn't define a return value so the js doesn't know what to return and it simply returns undefined
13+
// We can fix the code by replacing the console.log with a return keyword so the js knows what to return when the function is called
14+
15+
// Finally, correct the code to fix the problem
16+
// Here is the fixed code
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: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// Predict and explain first...
2-
// I predict that the code will run but it will print undefined because the return expression is not setup properly
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-
11-
// When we run the code it printed out undefined because the return keyword is there but the expression that needs to be returned is not on the same line
12-
// therefore the return keyword can't reach the expression and the expression is ignored because the function has done execution right after it reaches the return keyword
13-
14-
// Finally, correct the code to fix the problem
15-
// Here is the fixed code
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)}`);
1+
// Predict and explain first...
2+
// I predict that the code will run but it will print undefined because the return expression is not setup properly
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+
11+
// When we run the code it printed out undefined because the return keyword is there but the expression that needs to be returned is not on the same line
12+
// therefore the return keyword can't reach the expression and the expression is ignored because the function has done execution right after it reaches the return keyword
13+
14+
// Finally, correct the code to fix the problem
15+
// Here is the fixed code
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: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
// Predict and explain first...
2-
3-
// Predict the output of the following code:
4-
// I predict the code will run but will give us unexpected output as the function does not have parameter but we provided an argument
5-
6-
/*const num = 103;
7-
8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
11-
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)}`);*/
15-
16-
// Now run the code and compare the output to your prediction
17-
// The last digit of 42 is 3
18-
// The last digit of 105 is 3
19-
// The last digit of 806 is 3
20-
21-
// Explain why the output is the way it is
22-
// It is because we declared a function with no parameters so the function will ignore any arguments we provide
23-
// since the var num is declared and assigned with a value so every time the function is called it will use the var num
24-
25-
// Finally, correct the code to fix the problem
26-
// Here is the fixed code
27-
function getLastDigit(num) {
28-
return num.toString().slice(-1);
29-
}
30-
31-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
34-
35-
// This program should tell the user the last digit of each number.
36-
// Explain why getLastDigit is not working properly - correct the problem
1+
// Predict and explain first...
2+
3+
// Predict the output of the following code:
4+
// I predict the code will run but will give us unexpected output as the function does not have parameter but we provided an argument
5+
6+
/*const num = 103;
7+
8+
function getLastDigit() {
9+
return num.toString().slice(-1);
10+
}
11+
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)}`);*/
15+
16+
// Now run the code and compare the output to your prediction
17+
// The last digit of 42 is 3
18+
// The last digit of 105 is 3
19+
// The last digit of 806 is 3
20+
21+
// Explain why the output is the way it is
22+
// It is because we declared a function with no parameters so the function will ignore any arguments we provide
23+
// since the var num is declared and assigned with a value so every time the function is called it will use the var num
24+
25+
// Finally, correct the code to fix the problem
26+
// Here is the fixed code
27+
function getLastDigit(num) {
28+
return num.toString().slice(-1);
29+
}
30+
31+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
34+
35+
// This program should tell the user the last digit of each number.
36+
// Explain why getLastDigit is not working properly - correct the problem
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
// Below are the steps for how BMI is calculated
2-
3-
// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.
4-
5-
// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:
6-
7-
// squaring your height: 1.73 x 1.73 = 2.99
8-
// dividing 70 by 2.99 = 23.41
9-
// Your result will be displayed to 1 decimal place, for example '23.4'.
10-
11-
// You will need to implement a function that calculates the BMI of someone based off their weight and height
12-
13-
// Given someone's weight in kg and height in metres
14-
// 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
16-
17-
function calculateBMI(weight, height) {
18-
const BMI = weight / height ** 2;
19-
const stringBMI = BMI.toString();
20-
return stringBMI.slice(0, stringBMI.indexOf(".") + 2);
21-
}
22-
23-
console.log(calculateBMI(70, 1.73));
1+
// Below are the steps for how BMI is calculated
2+
3+
// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.
4+
5+
// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:
6+
7+
// squaring your height: 1.73 x 1.73 = 2.99
8+
// dividing 70 by 2.99 = 23.41
9+
// Your result will be displayed to 1 decimal place, for example '23.4'.
10+
11+
// You will need to implement a function that calculates the BMI of someone based off their weight and height
12+
13+
// Given someone's weight in kg and height in metres
14+
// 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
16+
17+
function calculateBMI(weight, height) {
18+
const BMI = weight / height ** 2;
19+
const stringBMI = BMI.toString();
20+
return stringBMI.slice(0, stringBMI.indexOf(".") + 2);
21+
}
22+
23+
console.log(calculateBMI(70, 1.73));

0 commit comments

Comments
 (0)