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
19 changes: 13 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// =============> This will not run because the variable str has already been used
// as a parameter. This will cause a syntax error


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

// =============> write your explanation here
// =============> write your new code here
// =============> The first mistake this renders is indeed in line 14 that
// "Identifier 'str' has already been declared"
// =============>
//
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
27 changes: 27 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// An error will occur because the variable decimalNumber is being declared as both
// a parameter and as a local variable inside the function. This will cause a
// syntax error. Additionally, the variable decimalNumber is not defined
// outside the function, so it will throw a ReferenceError when trying to log it
// to the console.
// The percentage sign is not seen as a text character but instead as code for
// the function to calculate the remainder after a division operation: the modulo.
// It will therefore not return a percentage as desired


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

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

// =============> write your explanation here
// The first error is that the variable decimalNumber is being declared twice,
// first as a parameter and then as a local variable inside the function. This
// will cause a syntax error.
// The second error is that the variable decimalNumber is not defined outside the
// function. It will not log to the console but throw a ReferenceError because it
// is not accessible in that scope.

// Also, on testing the code I saw that I had misjudged the modulo issue - it is not
// an issue at all because the percentage sign is being used as a text character in a
// template literal, so it will be treated as such and not as code for the modulo operator.

// 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));
11 changes: 10 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// "num" has not been defined and the parameter is not used in the function;
// it has a number instead


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

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


// =============> explain this error message here
// The error message is saying that it was not expecting a number in the function
// declaration. This is because the parameter should be a variable name, not a number

// Finally, correct the code to fix the problem

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

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

12 changes: 12 additions & 0 deletions 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
// The function multiply does not return anything, it only logs the result to the console.
// Therefore the outcome of the final console.log will be undefined


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

// =============> write your explanation here
// The function multiply does not return a value, it only logs the result to the console.
// Therefore, on using the template literal to write the result of the function, it doesn't
// have this result and renders "undefined" instead


// 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)}`);
11 changes: 11 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Predict and explain first...
// =============> write your prediction here
// the semicolon after "return" will hamper the return statement because this
// character will end the statement before receiving a definition of what it
// needs to return.


function sum(a, b) {
return;
Expand All @@ -9,5 +13,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// as above

// 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)}`);
25 changes: 23 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// The "const" is hampering the function getLastDigit because the return
// function will always use the value of the const for its calculations and
// ignore any other input



const num = 103;

Expand All @@ -15,10 +22,24 @@ 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 function getLastDigit is not working properly because it is using the variable
// num, which is defined outside the function with the value of 103. It will always
// return the last digit of 103 instead of the given parameter in the template literal

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

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 @@ -16,4 +16,6 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
const bmi = weight / (height * height);
return parseFloat(bmi.toFixed(1));
}
8 changes: 8 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,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 convertToUpperSnakeCase(str) {
const upperSnakeCase = str.toUpperCase().replace(/ /g, '_');
return upperSnakeCase;
}

console.log(convertToUpperSnakeCase("hello there"));
console.log(convertToUpperSnakeCase("lord of the rings"));
25 changes: 25 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,28 @@
// 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}`;
}

console.log(toPounds("123p"));
console.log(toPounds("5p"));
console.log(toPounds("0p"));
console.log(toPounds("98237986367887293p"));
23 changes: 18 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,38 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(formatTimeDisplay(61));

// 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
// =============> Three times:
// pad(totalHours)
// pad(remainingMinutes)
// pad(remainingSeconds)

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

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 0 for totalHours, which is then converted to "00" by the padStart
// method because the string length of "0" is less than 2

// 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
// =============> 1 for remainingSeconds; it is the num parameter, not the string

// 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 string "01" because the string length of "1" is less than 2

// By the way - the Python visualiser didn't want to work with this. It said:
// File "<string>", line 1
// function pad(num) {
// ^^^
// SyntaxError: invalid syntax

// I asked Claude.ai to help instead
1 change: 1 addition & 0 deletions Sprint-2/Project-CLI-Treasure-Hunt
Submodule Project-CLI-Treasure-Hunt added at 4374e4
17 changes: 17 additions & 0 deletions Sprint-2/prep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function formatAs12HourClock(time) {
return `${time} am`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

const currentOutput = formatAs12HourClock("23:00");
const targetOutput = "11:00 pm";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);