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
14 changes: 11 additions & 3 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here:this will throw an error, because the function has a parameter called str and then tries to declare another variable also called str inside the function body using let.

// call the function capitalise with a string input
// call the function capitalise with a string input: SyntaxError: Identifier 'str' has already been declared
// 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;
}

// =============> write your explanation here
// =============> write your explanation here
:in JavaScript, a function's parameters are treated like variables that already exist within that function's scope the moment the function starts. So writing function capitalise(str) { ... }, str is already declared as soon as you're inside the function. Trying to declare another variable with the same name using let str = ... inside that same scope is not allowed — JavaScript won't let you redeclare an existing let/const/parameter name in the same scope, since it would be ambiguous which "str" you mean.

// =============> write your new code here
:function capitalise(str) {
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalised;
}

console.log(capitalise("hello")); // "Hello"
8 changes: 8 additions & 0 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
: inside the function, decimalNumber is already the parameter name, but then it's redeclared with const decimalNumber = 0.5; inside the function body. console.log(decimalNumber); tries to access decimalNumber completely outside the function. But decimalNumber only exists inside convertToPercentage — it was never declared anywhere in the outer/global scope.

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

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

// =============> write your explanation here
: the redundant inner redeclaration is removed entirely — the function just uses the parameter decimalNumber it's given. And instead of trying to log a variable that doesn't exist outside the function, the code calls the function itself, passing in 0.5, and logs whatever it returns — which will correctly print "50%".

// 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));
12 changes: 10 additions & 2 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

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

// =============> write your prediction of the error here
// =============> write your prediction of the error here
: This should fail before it even runs, because 3 is being used as a parameter name in the function definition — function square(3) { ... }. Parameter names have to follow the same rules as variable names, and just like a variable, they can't just be a plain number. A parameter name is meant to be a placeholder/label for whatever value gets passed in later, not a literal value itself.

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

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

// =============> explain this error message here
:hen you write a function like function square(num) { ... }, num is a name that acts as a stand-in for whatever value someone passes in when they call square(5) or square(10).Writing 3 there doesn't make sense to the parser; it's like trying to say "let 3 be the placeholder," which conflicts with how the language works. the function body refers to num, but num was never actually defined as the parameter name, so even fixing the syntax error alone wouldn't be enough.

// Finally, correct the code to fix the problem

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

console.log(square(3)); // 9


7 changes: 7 additions & 0 deletions Sprint-3/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
:The function multiply uses console.log to print the result inside itself, but it doesn't have a return statement

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

// =============> write your explanation here
: console.log() and return do two completely different jobs. console.log() just prints something to the console for you to see — it doesn't hand any value back to the code that called the function. return is what actually sends a value back out of the function so it can be used elsewhere, like inside another expression or template literal.

// 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)}`);
9 changes: 8 additions & 1 deletion Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
:the return statement sits on its own line, completely separate from a + b below it.JavaScript will treat return; as returning nothing at all, and a + b will just never actually run, since the function exits right at the return line.

function sum(a, b) {
return;
Expand All @@ -8,6 +9,12 @@ function sum(a, b) {

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

// =============> write your explanation here
// =============> write your explanation here JavaScript automatically inserts a semicolon after return if there's a line break immediately following it, effectively treating it as return; — returning undefined — and then completely ignoring whatever comes after on the next line, since the function has already exited by that point. So a + b becomes dead code that never executes; the function returns before it even gets there.
// 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)}`); // "The sum of 10 and 32 is 42"
15 changes: 13 additions & 2 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here : Even though the function calls pass in different numbers (42, 105, 806), the getLastDigit function doesn't take an input at all all three lines will print the same result: the last digit of 103, which is 3 — regardless of what number is passed in.

const num = 103;

Expand All @@ -15,10 +15,21 @@ 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 arguments 42, 105, and 806 are being passed into getLastDigit(42) etc., but since the function is defined as function getLastDigit() — with empty parentheses — it has no parameters to receive those values. The passed-in arguments are simply discarded. inside the function isn't a parameter at all — it's referring to the completely separate outer num variable (103) via closure/scope, which is why every call gives the same answer regardless of input.
// 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: 7 additions & 0 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@
function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}

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

console.log(calculateBMI(70, 1.73)); // "23.4"
8 changes: 8 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,11 @@
// 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().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"

21 changes: 21 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,24 @@
// 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) {
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

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

10 changes: 5 additions & 5 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> write your answer here : 3 times, once for totalHours, once for remainingMinutes, once for remainingSeconds, since the template literal contains three separate pad(...) calls.

// 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
// =============> write your answer here : totalHours is 0, num is 0 on the first call.

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> 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
// =============> write your answer here : pad(remainingSeconds), and since remainingSeconds is 1, num is 1 on this final call.

// 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 : "01"
54 changes: 54 additions & 0 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,57 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);


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

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

// PM time with non-zero minutes
const currentOutput5 = formatAs12HourClock("13:45");
const targetOutput5 = "01:45 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);

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


//FIXED VERSIONS

function formatAs12HourClock(time) {
const [hoursString, minutes] = time.split(":");
let hours = Number(hoursString);
let period = "am";

if (hours === 0) {
hours = 12;
} else if (hours === 12) {
period = "pm";
} else if (hours > 12) {
hours = hours - 12;
period = "pm";
}

const paddedHours = hours.toString().padStart(2, "0");
return `${paddedHours}:${minutes} ${period}`;
}
Loading