Skip to content
20 changes: 14 additions & 6 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
// function parameter and variable inside the function have same name.

// call the function capitalise with a string input
console.log(capitalise("message"));
// 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;
}
/*Identifier 'str' has already been declared .
This mean that 'str' has already been declared as a function parameter,
so we cannot declare another variable with the same name using 'let'.
*/

// =============> write your explanation here
// The error occurs because 'str' is already declare as a function parameter.
//The 'let str' inside the function tries to declare 'str' again in the same scope.
//I changed the name of new variable to 'result' to fix the error.

// =============> write your new code here
function capitalise(str) {
let result = `${str[0].toUpperCase()}${str.slice(1)}`;
return result;
}
24 changes: 16 additions & 8 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

// Why will an error occur when this program runs?
// =============> write your prediction here

// If we run the program, I predict we will get an error.
// The first problem is that 'decimalNumber' is already function parameter, but you try to declare another variable with the same name inside the function.
//another problem is that 'decimalNumber' is a local variable inside the function
// and we cannot access to it outside the function.
//------------------------------------------------------
// Try playing computer with the example to work out what is going on
//SyntaxError: Identifier 'decimalNumber' has already been declared

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

// The error occurs because 'decimalNumber' has already been declared
// as a function parameter. we cannot declare it again with 'const'

// Finally, correct the code to fix the problem
// =============> write your new code here
const decimalNumber = 0.5;

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

return percentage;
}

console.log(decimalNumber);

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

// Finally, correct the code to fix the problem
// =============> write your new code here
console.log(convertToPercentage(decimalNumber));
17 changes: 12 additions & 5 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@

// Predict and explain first BEFORE you run any code...
// when we run the code, I predict that we will got the error in function argument
//because it is not a variable

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

// =============> write your prediction of the error here
//

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

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

// =============> explain this error message here
// The error occurs because 3 is a number, not a valid variable name.

// Finally, correct the code to fix the problem

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


const num = 3;
function square(num) {
return num * num;
}
console.log(square(num));
16 changes: 13 additions & 3 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
//when we run the code, I predict that the function just execute but doesn't return any thing.
// function just print the result of multiply
// I am not sure what going to be show in the second console.log

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 running the code, the multiply function display the result (320)
// but it does not return result back to the code
// therefore, multiply(10, 32) evaluate undefined

// 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)}`);
14 changes: 11 additions & 3 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
// =============> I think the position of return inside the function is wrong
//probably it is show nothing

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
// After running code it is show "The sum of 10 and 32 is undefined"
// because return has no value so it return undefined.

// 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)}`);
23 changes: 19 additions & 4 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
// Predict and explain first...
// In my idea we can not change value of num as it is defined by const

// Predict the output of the following code:
// =============> Write your prediction here
//perhaps we will get value definition error

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 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
// I was wrong in all the function call, the result is the same because the function always uses the global num.

// Finally, correct the code to fix the problem
// =============> write your new code here
let num = 103;

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.

Is num on line 28 used now? Your function uses its own num parameter. If line 28 is not needed, remove it.


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
4 changes: 3 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,7 @@
// 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(`your body mass is ${calculateBMI(87, 1.58)}`);
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 toUpperSnakeCase(text) {
const newText = text.replace(" ", "_");

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.

Try toUpperSnakeCase("lord of the rings"). What does it return? The exercise expects "LORD_OF_THE_RINGS". How many spaces does replace change? The MDN page for replace will help.

return newText.toUpperCase();
}
console.log(
`The upper snake case of "hello there" is ${toUpperSnakeCase("hello there")}`,
);
20 changes: 20 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,23 @@
// 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 toPound(penceString) {

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 4 asks for a function called toPounds. What is your function called?

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(toPound("399p"));
console.log(toPound("2p"));
console.log(toPound("4500p"));
console.log(toPound("95p"));
console.log(toPound("3675p"));
15 changes: 9 additions & 6 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ 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
// =============> The pad function is called three times on line 15.

// 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
// =============> The value assigned to num is 0.

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> The value of pad when it is called for the first time "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
// =============> The value assign to num is 1.
// because the last call to pad is pad(remainingSeconds), and remainingSeconds is 1
// when formatTimeDisplay(61) is called.

// 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
// =============> The return value is "01" because the last call to pad is pad(1).
// The pad function add a zero to make the number two digits.
48 changes: 41 additions & 7 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,56 @@

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

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
const currentOutput = formatAs12HourClock("08:30");
const targetOutput = "08:30 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";
const currentOutput2 = formatAs12HourClock("15:30");
const targetOutput2 = "03:30 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
);

const currentOutput5 = formatAs12HourClock("11:59");
const targetOutput5 = "11:59 am";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`,
);

const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";

console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
);
const currentOutput4 = formatAs12HourClock("12:02");
const targetOutput4 = "12:02 pm";

console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
);

console.log(currentOutput, targetOutput);
console.log(currentOutput2, targetOutput2);
console.log(currentOutput3, targetOutput3);
console.log(currentOutput4, targetOutput4);
console.log(currentOutput5, targetOutput5);
Loading