Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming.

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

// =============> write your explanation here
// The original error happened because we tried to declare a new variable
// named `str` with `let` even though `str` was already a parameter. That's
// illegal in JavaScript and produces a SyntaxError. By removing the extra
// declaration (or by returning the string directly) the function works.
// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
9 changes: 8 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming.

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

Expand All @@ -18,3 +18,10 @@ console.log(decimalNumber);

// 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));

9 changes: 7 additions & 2 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// I predict that the error will be a SyntaxError because we are trying to declare a function with an invalid name. In JavaScript, function names cannot start with a number. By trying to declare a function named `square(3)`, we are violating this rule, which will result in a SyntaxError when the code is parsed.


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

// =============> write the error message here

// SyntaxError due to an unexpected token or invalid function name.
// =============> explain this error message here

// This is because `square(3)` is not a valid function declaration in JavaScript, and the parser will not be able to understand it as a function definition.
// Finally, correct the code to fix the problem

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


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

// =============> write your prediction here
// I predict that the error will be a ReferenceError because the function `multiply` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The result of multiplying 10 and 32 is undefined" instead of the expected product of 10 and 32.

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

// =============> write your explanation here

// The error occurs because the `multiply` function does not return a value; it only logs the result to the console. When we use `multiply(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return statement in the function. To fix this, we need to add a return statement to the `multiply` function so that it returns the product of `a` and `b` instead of just logging it.
// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}
6 changes: 5 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// =============> write your prediction here

// I predict that the error will be a ReferenceError because the function `sum` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The sum of 10 and 32 is undefined" instead of the expected sum of 10 and 32.
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +9,9 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The error occurs because the `sum` function does not return a value; it only has a return statement without any expression. When we use `sum(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return value from the function. To fix this, we need to return the result of `a + b` instead of just having a return statement with no value.
// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b;
}
16 changes: 14 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Predict and explain first...

// this function should square any number but instead we're going to get an error
// Predict the output of the following code:
// =============> Write your prediction here

// I predict that the output will be:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +18,19 @@ 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 output will be:
// 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 function `getLastDigit` is not taking any parameters, so it always returns the last digit of the global variable `num` (which is 103). This means that regardless of what number is passed to the function, it will always return 3.
// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(num) {
return num.toString().slice(-1);
}

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// the function `getLastDigit` is not working properly because it does not take any parameters, and it always returns the last digit of the global variable `num`. To fix this, we need to modify the function to accept a parameter (the number we want to find the last digit of) and use that parameter instead of the global variable. This way, we can get the correct last digit for each number passed to the function.
2 changes: 2 additions & 0 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const bmi = weight / (height * height);
return Math.round(bmi * 10) / 10;
Comment on lines 17 to +19

Choose a reason for hiding this comment

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

Is there a better method to return the bmi value in one decimal place?

// 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.toUpperCase().replace(/ /g, '_');
// return the string in UPPER_SNAKE_CASE
}
Comment on lines +17 to +21

Choose a reason for hiding this comment

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

Is it best to pass in //g as an argument in your .replace method?

5 changes: 5 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,8 @@
// 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(kilograms) {
return kilograms * 2.20462;
// return the weight in pounds
}
Comment on lines +7 to +11

Choose a reason for hiding this comment

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

You should read the context of the task to understand what you are to do. The task is referring to the code from the previous sprint, which you are to work with to actualise the given task.

6 changes: 5 additions & 1 deletion Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// The `pad` function will be called three times when `formatTimeDisplay` is called, once for each of the time components: hours, minutes, and seconds. Each component is passed to the `pad` function to ensure it is displayed with at least two digits, adding a leading zero if necessary.

// 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
// When `formatTimeDisplay(61)` is called, the first time `pad` is called, `num` is assigned the value of `totalHours`, which is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of `pad(0)` is "00".

// 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

// When `formatTimeDisplay(61)` is called, the last time `pad` is called, `num` is assigned the value of `remainingSeconds`, which is 1.
// 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 of `pad(1)` is "01".
Comment on lines 36 to +38

Choose a reason for hiding this comment

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

You could further explain your reason for this answer by analysing the full output of the given code when the function is called with the passed in argument.