Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// This function is trying to capitalise the first letter of a string.
// it takes the first character using str[0], converts it to uppercase,and joins it with the rest of the string using str.slice(1).

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -10,4 +12,9 @@ function capitalise(str) {
}

// =============> write your explanation here
// =============> write your new code here
// The error is occurring because the function is trying to declare a variable with the same name as the parameter, which is not allowed in JavaScript.
// The variable `str` is being redeclared inside the function, causing a syntax error.
function capitalise(str) {
let result = str[0].toUpperCase() + str.slice(1);
return result;
}
15 changes: 15 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict this function is trying to convert a decimal number
// into a percentage. It multiples the decimal number by 100 and
// then it adds the % symbol to the end of the number.

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

Expand All @@ -15,6 +18,18 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// The error happens becouse 'decimalNumber' is already the parameter
// of the function. Inside the function the code tries to declare 'decimalNumber'
// again using the const, which is not allowed.

// a other problem is that console.log(decimalNumber) is outside
// the function but 'decimalNumber' only exists inside the function.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}

console.log(convertToPercentage(0.5));
10 changes: 10 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// I predict an error will happen becouse the function uses 3 as the parameter
// but parameters should be varible names. Also the code uses "num" inside the
// function even though it was never defiend.

function square(3) {
return num * num;
}

// =============> write the error message here
// SyntaxError: Unexpected number

// =============> explain this error message here
// The error happens becouse 3 is used as the function parameter but parameters
// must be varible names. Also the code tries to use "num" inside the function even
// though it was never dec;ared.

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


11 changes: 11 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Predict and explain first...

// =============> write your prediction here
// I predict the program will print 320 first.
// Then it will print "the result of multipying 10 and 32 is undefined"
// becouse the function logs the result but does not return it.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +12,14 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// The problem is that the function uses console.log insted ofreturning the value.
// Becouse the function does not return anything, the value of multiply(10, 32)
// becomes undefined when it is used insdie the temple string.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}

console.log('The result of multiplying 10 and 32 is ' + multiply(10, 32));
10 changes: 10 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Predict and explain first...
// =============> write your prediction here
// I predict the program will print "The sum of 10 and 32 is underfined"
// becouse the return statment does not return a value and the addition
// is written on the next line.

function sum(a, b) {
return;
Expand All @@ -9,5 +12,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The problem is that the return us written on its own line.
// Becouse of this, the function returns undefined before executing a + b.
// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a,b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
20 changes: 20 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

// Predict the output of the following code:
// =============> Write your prediction here
// I predict the program will print:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3
// This happens because the function always uses the variable num which is 103.

const num = 103;

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

// Now run the code and compare the output to your prediction
// =============> write the output here
// The last digit of 42 is 3
//The last digit of 105 is 3
// The last digit of 806 is 3

// Explain why the output is the way it is
// =============> write your explanation here
// The problem is that the function always uses the varible num which is 103.
// Becouse of this, the last digit is always 3.
// The numbers passed into the function (42, 105, 806) are ignored.

// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(num) {
return num.toString().slice(-1);
}

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
7 changes: 5 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
return bmi.toFixed(1);
Comment thread
KKtech06 marked this conversation as resolved.
Outdated
}
console.log(calculateBMI(70, 1.73));
// return the BMI of someone based off their weight and height
5 changes: 5 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

function toUpperSnakeCase(str) {
return str.replaceAll(" ", "_").toUpperCase();
}
console.log(toUpperSnakeCase("hello there"));
7 changes: 7 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs

function toPounds(pence) {
return "£" + (pence / 100).toFixed(2);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in Sprint-1 takes as "input" a pence string in the form "123p" instead of a number 123. Can you update your function to match that behavior?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for spotting the error CJ I have fixed it and hopefully now the function matches the behaviour :)

console.log(toPounds(399));
console.log(toPounds(250));
console.log(toPounds(100));
5 changes: 5 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// It will be called 3 times becouse it is used three times in the return line.

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

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// The value is 0 becouse totalHours is 0 when the input is 61 seconds.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value is "00" beccouse padStart adds a 0 in front to make it two digits.

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// The value is 1 becouse remainingSeconds is 1 when the input is 61 seconds.

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// The return value is "01" becouse padStart adds a 0 at the beginning ti make it two digits.