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
21 changes: 16 additions & 5 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here
// ==============> write your prediction here
// I predict this will throw a SyntaxError, because the parameter 'str' is
// being redeclared with 'let' inside the function.

// 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;
let result = `${str[0].toUpperCase()}${str.slice(1)}`;
return result;
}

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("hello"));

// ==============> write your explanation here
// The parameter 'str' already existed in the function's scope. The original
// code tried to declare another variable with 'let str', which caused a
// naming collision — JavaScript doesn't allow redeclaring a variable with
// 'let' in the same scope. This threw a SyntaxError before the function
// could even run. Renaming the new variable to 'result' fixes the collision.

// ==============> write your new code here
// (the fixed function above already satisfies this)
25 changes: 19 additions & 6 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// ==============> write your prediction here
// I predict this will throw a SyntaxError, because 'decimalNumber' is being
// redeclared with 'const' inside the function, even though it's already the
// function's parameter.

// 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
// ==============> write your explanation here
// 'decimalNumber' was already declared as the function's parameter. The
// original code tried to declare another variable with the same name using
// 'const decimalNumber = 0.5;', which caused a naming collision — JavaScript
// doesn't allow redeclaring a variable in the same scope. This threw a
// SyntaxError before the function could run.
//
// There was also a second issue: the original 'console.log(decimalNumber);'
// was outside the function, so 'decimalNumber' didn't exist there (it's
// scoped only inside 'convertToPercentage'), and the function was never
// actually called. The fix removes the duplicate declaration and calls
// convertToPercentage(0.5) directly, logging its return value instead.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ==============> write your new code here
// (the fixed function above already satisfies this)
35 changes: 23 additions & 12 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@

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

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

// =============> write your prediction of the error here
// this function should square any number but instead we're getting an error
// ==============> write your prediction of the error here
// I predict this will throw a SyntaxError, because '3' is used as the
// parameter name in 'function square(3)', and a number can't be used as a
// parameter name — parameter names must be valid identifiers (like variable
// names).

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

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

// =============> explain this error message here
console.log(square(3));

// Finally, correct the code to fix the problem

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

// ==============> explain this error message here
// Function parameters must be valid identifiers — the same rules as variable
// names (they must start with a letter, '$', or '_', never a digit). The
// original code used '3' as the parameter name, which isn't a valid
// identifier, so JavaScript couldn't even parse the function definition.
// This error happens before any code runs. There was also a second bug: the
// function body referred to 'num', which didn't match the (invalid)
// parameter name at all — fixing the parameter name to 'num' resolves both
// issues at once.

// Finally, correct the code to fix the problem
// ==============> write your new code here
// (the fixed function above already satisfies this)
22 changes: 17 additions & 5 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
// Predict and explain first...

// =============> write your prediction here
// ==============> write your prediction here
// I predict this will run without an error, but print the wrong result at
// the end. The 'multiply' function uses console.log internally instead of
// returning a value, so when it's used inside the template literal, it will
// show as 'undefined' instead of the actual multiplication result.

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
// ==============> write your explanation here
// The original 'multiply' function printed the result with console.log
// instead of returning it. A function that doesn't explicitly return a value
// returns 'undefined' by default. When the outer console.log tried to use
// multiply(10, 32) inside the template literal, it inserted 'undefined'
// instead of the actual number, because it was using the function's return
// value, not what it printed internally. Changing console.log to return
// inside the function fixes this, since now the function actually hands
// back the calculated value.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ==============> write your new code here
// (the fixed function above already satisfies this)
24 changes: 19 additions & 5 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// Predict and explain first...
// =============> write your prediction here
// ==============> write your prediction here
// I predict this will run without an error, but the result will show as
// 'undefined' instead of the actual sum. The 'return' statement is on its
// own line, separate from 'a + b;' on the next line — JavaScript will treat
// these as two separate statements, so the function returns immediately
// with nothing, and 'a + b' never actually gets returned.

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
// ==============> write your explanation here
// JavaScript has a feature called Automatic Semicolon Insertion (ASI). When
// 'return' appears on its own line with nothing after it, JavaScript
// automatically treats it as 'return;' — ending the function right there and
// returning 'undefined'. The following line, 'a + b;', becomes dead code
// that never runs, because the function has already exited. This is why the
// original code always produced 'undefined' instead of the sum. The fix is
// to put the expression on the same line as 'return', so JavaScript knows
// it's part of the return statement: 'return a + b;'.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ==============> write your new code here
// (the fixed function above already satisfies this)
22 changes: 14 additions & 8 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// ==============> write your prediction here
// I predict every call to getLastDigit will return the same result: "3",
// no matter what number is passed in. This is because the function doesn't
// take any parameters — it always uses the outer 'num' variable (103)
// instead of the value passed into the function call.

const num = 103;

Expand All @@ -14,11 +18,13 @@ 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
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
// ==============> 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

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// Explain why the output is the way it is
// ==============> write your explanation here
// The function getLastDigit() is defined with no parameters, so the values
// 42, 105, and 806 passed into each call are simply ignored — they go
// nowhere. Instead, the
7 changes: 6 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,10 @@
// 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));
console.log(calculateBMI(60, 1.6));
console.log(calculateBMI(90, 1.85));
7 changes: 7 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,10 @@
// 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 convertToUpperSnakeCase(str) {
return str.toUpperCase().split(' ').join('_');
}

console.log(convertToUpperSnakeCase("hello there"));
console.log(convertToUpperSnakeCase("lord of the rings"));
8 changes: 8 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
// 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(kg) {
return kg * 2.20462;
}

console.log(toPounds(70));
console.log(toPounds(60));
console.log(toPounds(90));
39 changes: 26 additions & 13 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,41 @@ function formatTimeDisplay(seconds) {
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
// 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
// ==============> write your answer here
// pad will be called 3 times — once for totalHours, once for remainingMinutes,
// and once for remainingSeconds, since all three appear inside the template
// literal on 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

// c) What is the return value of pad when it is called for the first time?
// =============> 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
// =============> 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
// =============> write your answer here
// ==============> write your answer here
// num = 0 (this is totalHours, since (61-1)/60 = 1 minute total, and
// (1-1)/60 = 0 hours, and pad(totalHours) is the first call in the template
// literal)

// c) What is the return value of pad it is called for the first time?
// ==============> write your answer here
// "00" — pad(0) converts 0 to the string "0", which has length 1, so a
// leading "0" is added, making it "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 (this is remainingSeconds — 61 % 60 = 1 — and pad(remainingSeconds)
// is the last of the three pad() calls in the template literal)

// 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
// "01" — pad(1) converts 1 to the string "1", which has length 1, so a
// leading "0" is added, making it "01"

console.log(formatTimeDisplay(61)); // "00:01:01"
54 changes: 51 additions & 3 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const minutes = time.slice(3);

if (hours === 0) {
return `12:${minutes} am`;
} else if (hours === 12) {
return `12:${minutes} pm`;
} else if (hours > 12) {
return `${hours - 12}:${minutes} pm`;
} else {
return `${hours}:${minutes} am`;
}
return `${time} am`;
}

// Original tests
const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
Expand All @@ -23,3 +31,43 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// Edge case: midnight
const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

// Edge case: noon
const currentOutput4 = formatAs12HourClock("12:00");
const targetOutput4 = "12:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

// Edge case: PM time with non-zero minutes (checks minutes aren't lost)
const currentOutput5 = formatAs12HourClock("13:45");
const targetOutput5 = "1:45 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);

// Edge case: one minute before midnight
const currentOutput6 = formatAs12HourClock("23:59");
const targetOutput6 = "11:59 pm";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
);

// Edge case: noon with non-zero minutes
const currentOutput7 = formatAs12HourClock("12:15");
const targetOutput7 = "12:15 pm";
console.assert(
currentOutput7 === targetOutput7,
`current output: ${currentOutput7}, target output: ${targetOutput7}`
);
Loading