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
18 changes: 13 additions & 5 deletions Sprint-3/1-key-errors/0.js

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.

Great explanations - nice refactor of the code

Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: I predict that the code will produce an error because
// str is already declared as a function parameter and is declared
// again with let.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
return `${str[0].toUpperCase()}${str.slice(1)}`;
}

// =============> write your explanation here
// =============> write your new code here
// Explanation: The parameter str is already declared when the
// function is created. We cannot declare another variable called
// str with let in the same scope.

// New code:
// The function now uses the existing str parameter and returns
// the capitalised string.

console.log(capitalise("hello"));
18 changes: 13 additions & 5 deletions Sprint-3/1-key-errors/1.js

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.

Again good accurate explanations and rework here

Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// Prediction: An error will occur because decimalNumber is already
// declared as a function parameter and is declared again with const.
// Also, decimalNumber is being used outside the function where it
// is not available.

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
console.log(convertToPercentage(0.5));

// =============> write your explanation here
// Explanation: The function parameter decimalNumber already contains
// the value we give to the function. We cannot declare another
// decimalNumber inside the function with const. The original code
// also tried to use decimalNumber outside the function, where it
// does not exist.

// Finally, correct the code to fix the problem
// =============> write your new code here

// New code: The function uses the decimalNumber parameter and returns
// the percentage. We call the function with 0.5 and log the result.
15 changes: 12 additions & 3 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

// Predict and explain first BEFORE you run any code...

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

// Prediction: I predict that an error will occur because 3 cannot
// be used as a function parameter name.

// =============> write your prediction of the error here

function square(3) {
function square(num) {

return num * num;

}

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

// SyntaxError: Unexpected number

// =============> explain this error message here

// The error happens because 3 is a number, not a valid parameter name.
// A function parameter needs to be a variable name, such as num.

// Finally, correct the code to fix the problem

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


console.log(square(3));
15 changes: 13 additions & 2 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
// Predict and explain first...

// Prediction: I predict that the function will print 320,
// but the final message will say undefined because the function
// does not return the result.

// =============> write your prediction here

function multiply(a, b) {
console.log(a * b);
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

// Explanation: console.log() displays a value on the screen,
// but return sends a value back from the function.
// The original function used console.log() instead of return,
// so the function returned undefined.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code here

// The corrected function returns the result of multiplying a and b.
14 changes: 11 additions & 3 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...

// Prediction: I predict that the function will return undefined
// because return stops the function before a + b can be calculated.

// =============> write your prediction here

function sum(a, b) {
return;
a + b;
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// Explanation: The original code used return on its own line.
// When JavaScript reaches return, the function stops and returns
// undefined. The line a + b is therefore never used.
// We need to return a + b together.

// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code here
27 changes: 22 additions & 5 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
// Predict and explain first...

// Predict the output of the following code:
// Prediction: I predict that the program will print 3 for all
// three numbers because the function always uses the value 103.

// =============> Write your prediction here

const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
function getLastDigit(number) {
return number.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)}`);

// Now run the code and compare the output to your prediction
// Output after correction:
// The last digit of 42 is 2
// The last digit of 105 is 5
// The last digit of 806 is 6

// =============> write the output here
// Explain why the output is the way it is

// Explanation: The original getLastDigit function did not have
// a parameter, so the numbers 42, 105, and 806 passed to the
// function were ignored. Instead, the function always used the
// global value 103, so it returned 3 every time.
Comment on lines +27 to +30

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.

nice explanation here well done

//
// The corrected function has a parameter called number.
// This allows it to receive the number passed into the function.

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

// Finally, correct the code to fix the problem
// =============> write your new code here

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// The function now receives a number as an argument and returns
// its last digit.
6 changes: 5 additions & 1 deletion Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
// It should return a string of 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);
}

console.log(calculateBMI(70, 1.73));
4 changes: 4 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
// 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(input) {
return input.replaceAll(" ", "_").toUpperCase();
}
11 changes: 6 additions & 5 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js
function toPounds(kg) {
return kg * 2.20462;
}

// You will need to take this code and turn it into a reusable block of code.
// 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
console.log(toPounds(1));
console.log(toPounds(5));
console.log(toPounds(10));
36 changes: 7 additions & 29 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
function pad(num) {
let numString = num.toString();
while (numString.length < 2) {
numString = "0" + numString;
}
return numString;
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// Questions

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

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

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> "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
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> 1, because the remaining seconds are 1.

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> "01", because pad adds a 0 to the front of 1.
Loading