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
16 changes: 12 additions & 4 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
// I believe this will cause an error because str is declared twice

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

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

// The error happens because str is declared twice, once as a parameter & again with let
// If I give the new variable a different name, I hope it should work :)

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

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("frumentius"));
20 changes: 16 additions & 4 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// Because decimalNumber is declared twice, once as a parameter and again with const

// 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(decimalNumber)); */

// =============> write your explanation here
// decimalNumber is already a parameter, so declaring it again with const causes an error
// decimalNumber has already been declared as a parameter in the same function scope
// console.log(decimalNumber) is wrong because decimalNumber is not defined outside the function,
// so we need to call the function with a value

// 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.7));
18 changes: 14 additions & 4 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@

// 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
// I think using 3 will cause an error as parameter
// A parameter must be a valid variable name, not a number

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

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

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

// =============> explain this error message here
// The parser sees the number 3 where it expects a valid parameter name,
// so it cannot understand the structure of the function
// It means we cannot use a number as a parameter name

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(7));
14 changes: 12 additions & 2 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
// Predict and explain first...

// =============> write your prediction here
// I believe the function will calculate 10 x 32

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

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

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

// After calling multiply(10, 32), the result is undefined because there is no return statement
// It can still print 320 because console.log() prints the result inside the function

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there a possibility that the logs are being duplicated ?

13 changes: 11 additions & 2 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// I guess the function will return error or undefined because the return;

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

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

// =============> write your explanation here
// The problem is the semicolon immediately after return that prevent to reach out to a + b

// 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)}`);
21 changes: 19 additions & 2 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,40 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The output will be the same because we don't give the function a parameter

const num = 103;
/*const num = 103;

function getLastDigit() {
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)}`);
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 is the same but the last digit is 3

// Explain why the output is the way it is
// =============> write your explanation here
// Because of the global variable and also because the function has no parameter

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

// Because the function was using the global variable, it ignored the arguments passed to it outside of the function box
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
let BMI = weight / (height * height);

return BMI.toFixed(1);
}
console.log(calculateBMI(62, 1.67));

// return the BMI of someone based off their weight and height
6 changes: 6 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,9 @@
// 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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

apart from uppercase, is this function perhaps adding something that does not exist in the input ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, I noticed that it also replaces the spaces with underscores besides converting to uppercase.

}

console.log(toUpperSnakeCase("alpha and omega"));
18 changes: 18 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,21 @@
// 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(amount) {
const amountWithoutTrailingP = amount.substring(0, amount.length - 1);

const paddedPenceNumberString = amountWithoutTrailingP.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("1250p"));

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.

Line 6 asks you to call the function a number of times, with different inputs. Line 24 calls it once. Try a few more, for example a short one and a long one.

11 changes: 6 additions & 5 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ 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
// 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
// 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
// "01"
4 changes: 2 additions & 2 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
`current output: ${currentOutput}, target output: ${targetOutput}`,
);

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