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
15 changes: 14 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here

// I predict that the code will throw an error when trying to call the function capitalise with a string input
// because the variable 'str' is being declared twice within the same scope, which is not allowed in JavaScript.

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

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

// The code will throw a SyntaxError because the variable 'str' is being declared twice within the same scope.
// =============> write your new code here

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

// Example:
console.log(capitalise("hello")); // Output: "Hello"
15 changes: 12 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Predict and explain first...

// I predict that the code will throw an error when trying to call the function convertToPercentage with a decimal input
// because the variable 'decimalNumber' is being declared twice within the same scope, which is not allowed in JavaScript.
// Why will an error occur when this program runs?
// =============> write your prediction here
// The code will throw a SyntaxError because the variable 'decimalNumber' is being declared twice within the same scope.



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

Expand All @@ -16,5 +19,11 @@ console.log(decimalNumber);

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

// Finally, correct the code to fix the problem
// The code will throw a SyntaxError because the variable 'decimalNumber' is being declared twice within the same scope.
// =============> write your new code here

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

return percentage;
Comment on lines +25 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

  • This is an incomplete implementation of the function.
  • This function will always return "50%", which is not very useful. Can you improve its reusability so that it can be used to convert any number to an equivalent string in percentage format?

19 changes: 18 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@

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

// I predict that the code will throw a ReferenceError because the variable 'num' is not defined within the function scope.

// 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 code will throw a ReferenceError because the variable 'num' is not defined within the function scope.

/

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

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

// ReferenceError: num is not defined

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

// This error message indicates that the variable 'num' is being used before it has been declared or defined.
// In the function 'square', the parameter is incorrectly defined as a literal value (3) instead of a variable name.

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
// Example:
console.log(square(3)); // Output: 9
13 changes: 12 additions & 1 deletion 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 that the code will output the result of the multiplication, but the function multiply will not return a value.

// this function should multiply two numbers and return the result


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

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

// The function multiply is using console.log to output the result of the multiplication, but it is not returning a value.
// Therefore, the template literal will not be able to access the result of the multiplication.

// 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)}`);
16 changes: 14 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// Predict and explain first...
// =============> write your prediction here

// I predict that the code will output 'undefined' because the function sum does not return any value.

// this function should sum two numbers and return the result

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

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

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


// The function sum correctly returns the sum of the two numbers, so the template literal will output the correct result.

// 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)}`); // No change needed as the code is already correct
23 changes: 22 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// Predict the output of the following code:
// =============> Write your prediction here

// I predict that the code will output 'undefined' for each call to getLastDigit because the function does not use its parameter.

// This program should tell the user the last digit of each number.

const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +19,27 @@ 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 undefined
// The last digit of 105 is undefined
// The last digit of 806 is undefined

// Explain why the output is the way it is
// =============> write your explanation here
// The output is 'undefined' because the function getLastDigit does not use its parameter.
// It always returns the last digit of the variable num, which is 103, not the number passed to it.

// 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.
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
25 changes: 24 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,27 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
}

// Predict and explain first...
// =============> write your prediction here
// I predict that the code will output 'undefined' because the function calculateBMI
// does not return any value yet.

// =============> write your explanation here
// The function calculateBMI currently has no return statement, so it will return 'undefined'.
// To fix this, we need to perform the BMI calculation:
// BMI = weight / (height * height)
// Then, we round the result to one decimal place using toFixed(1).

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

function calculateBMI(weight, height) {
const bmi = weight / (height * height);
return bmi.toFixed(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

What type of value do you expect your function to return? A number or a string?
Does your function return the type of value you expect?

Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example,

  console.log(123);              // Output 123
  console.log("123");            // Output 123
  
  // Treated differently in the program
  let sum1 = 123 + 100;         // Evaluate to 223 -- a number
  let sum 2 = "123" + 100;      // Evaluate to "123100" -- a string.

}

// Example:
console.log(`The BMI of someone who weighs 70kg and is 1.73m tall is ${calculateBMI(70, 1.73)}`);
// Output: The BMI of someone who weighs 70kg and is 1.73m tall is 23.4
27 changes: 27 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,30 @@
// 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 the string in UPPER_SNAKE_CASE
}

// Predict and explain first...
// =============> write your prediction here
// I predict that the code will output 'undefined' because the function toUpperSnakeCase
// does not currently return anything.

// =============> write your explanation here
// The function needs to replace spaces in the string with underscores ("_")
// and then convert all the letters to uppercase.
// We can do this using string methods:
// - replaceAll(" ", "_") (or replace(/ /g, "_") for older versions of JS)
// - toUpperCase() to convert to uppercase.

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

function toUpperSnakeCase(str) {
return str.replaceAll(" ", "_").toUpperCase();
}

// Example:
console.log(toUpperSnakeCase("hello there")); // Output: HELLO_THERE
console.log(toUpperSnakeCase("lord of the rings")); // Output: LORD_OF_THE_RINGS
28 changes: 28 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,31 @@
// 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(penceString) {
// normalize input to a trimmed string
const input = String(penceString).trim();

// remove trailing 'p' if present
const numericPart = input.endsWith('p') ? input.slice(0, -1) : input;

// ensure at least 3 digits so we can safely split into pounds / last two pence
const padded = numericPart.padStart(3, '0');

// all but the last two digits are pounds
const pounds = padded.slice(0, padded.length - 2);

// last two digits are pence; ensure two chars
const pence = padded.slice(-2).padEnd(2, '0');

return `£${pounds}.${pence}`;
}

// Test calls
console.log(toPounds("399p")); // £3.99
console.log(toPounds("5p")); // £0.05
console.log(toPounds("75p")); // £0.75
console.log(toPounds("0p")); // £0.00
console.log(toPounds("1234p")); // £12.34
console.log(toPounds(" 42p ")); // £0.42 (works with extra whitespace)
console.log(toPounds("500")); // £5.00 (works without trailing 'p')
15 changes: 10 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 times (once for hours, minutes, and seconds)

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

// c) What is the return value of pad 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
// =============> num = 1
// (The last call to pad() is for remainingSeconds, which is 1 when seconds = 61)

// 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
// =============> "01"
// (pad() converts 1 to "1" and then pads to two digits, returning "01")

// Example output:
console.log(formatTimeDisplay(61)); // Output: "00:01:01"
Loading