Skip to content
Open
23 changes: 17 additions & 6 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:
// Answer below:

// I predict that the code should make the first letter of the string an uppercase letter,
// then add every letters that comes after

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

// =============> write your explanation here
// =============> write your explanation here:
// Answer below:

// The error read "SyntaxError: Identifier 'str' has already been declared",
// This is as a result of having two variables with same name "str".
// The error occoured in this code because the variable "str" has been declared twice

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

function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
console.log(capitalise("money"));
34 changes: 26 additions & 8 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// Answer below:
// A syntax error will occur for two reasons, first because there variable const "decimalNumber" being redecleared
// The second reason is because the function "convertToPercentage" is not called correctly.

// Try playing computer with the example to work out what is going on
// =============> write your prediction here:
// Answer below:
// I predicted that the code will not work because of the variable (decimalNumber) inside the function,
// Also because the console.log(decimalNumber) is just a name that exist inside the function

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

return percentage;
}
// Try playing computer with the example to work out what is going on

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

console.log(decimalNumber);
// return percentage;
// }
// console.log(decimalNumber);

// =============> write your explanation here
// Answer:

// const decimalNumber redeclares the parameter name which causes a SyntaxError.
// also console.log(decimalNumber) outside fails because the parameter only exists inside the function"

// 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(10.5));
17 changes: 14 additions & 3 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...
// Answer below:
// The code will not work because the placeholder variable (3) should be an actual name like "num"


// 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) {
return num * num;
}
// function square(3) {
// return num * num;
// }

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

// =============> explain this error message here
// The error message explains the error in the function parameter (3), the function expects a name parameter instead of a number.

// Finally, correct the code to fix the problem

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

function square(num) {
return num * num;
}
console.log(square(10))



18 changes: 14 additions & 4 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...
// Answer below:
// The code needs a value inside the console.log function to multiply, but no value was useed to replace the placeholder parameters
// It should be an error, no value to return

// =============> write your prediction here

function multiply(a, b) {
console.log(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
// Answer below:
// The original logs the product instead of returning it, so the function call inside the template string gives 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)}`);
18 changes: 13 additions & 5 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
// Code will not work because the paremeters should be on same line as return

function sum(a, b) {
return;
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 errors in this codes is the semi-colon in front of the return and the placeholder parameters not on same line as the return
// 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)}`);
39 changes: 32 additions & 7 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,48 @@

// Predict the output of the following code:
// =============> Write your prediction here
// Answer below:
// There is a constant variable num with value 103, the code should print last digit of the constant variable.

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

function getLastDigit() {
return num.toString().slice(-1);
}
// 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 42 is ${getLastDigit(42)}`);
// 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
// Answer below:
// 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
// Answer below:
// The code kept repeating same value for all log because of the constant variable decleared before the function and used inside the function.

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

// Answer below:

// The global constant variable outside the function has affected the code and caused it to always reference the last number of the global variable
// To fix this, getLastDigit needed its own parameter, so num comes from whatever you pass in
3 changes: 3 additions & 0 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const bmi = weight / (height * height)
return bmi.toFixed(1)
// return the BMI of someone based off their weight and height
}
console.log(`Your BMI is ${calculateBMI(100, 1.62)}`)
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 upperCase(str) {
const returnUpperCase = str.toUpperCase().replaceAll(" ", "_");
return returnUpperCase;
}
console.log(upperCase("what is your name mr man? "));
38 changes: 38 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,41 @@
// 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

// const penceString = "399p";

// 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");

// console.log(`£${pounds}.${pence}`);

function toPounds(inPounds) {
const penceStringWithoutTrailingP = inPounds.substring(
0,
inPounds.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("599p"));
17 changes: 11 additions & 6 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
function pad(num) {
let numString = num.toString();
while (numString.length < 2) {
numString = "0" + numString;
}
return numString;
return num.toString().padStart(2, "0");
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
Expand All @@ -14,6 +9,7 @@ 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
Expand All @@ -22,17 +18,26 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// Answer below:
// Pad runs once for "totalHours", once "remainingMinutes" and once for "remainingSeconds" in total pad is called 3 times.

// 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
// Answer below:
// The first pad is pad(totalHours) and the value is 0

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// The value is 0 plus the "0" added to it to make the value "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
// Answer below:
// The last call is pad(remainingSeconds) and the value is 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
// Answer below:
// the return value initially is 1 and "0" was added to it in front to make the value 01
39 changes: 29 additions & 10 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
// This is the latest solution to the problem from the prep.
// Make sure to do the prep before you do the coursework
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.


// Test result:
// Hours and Minutes worked in the AM function for different time test.
// 12:00 in the afternoon doesn't function correctly
// 00:00 mid-night doesn't work
// Minutes in the PM are not working.

// To fix bug:

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);

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.

Note: The .slice() method supports negative indices, which count positions from the end of the string.

For example, str.slice(-3) returns the substring containing last three characters from str.


if (hours === 0) {
return `12:${minutes} am`;
}
if (hours === 12) {
return `12:${minutes} pm`;
}
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
}

return `${time} am`;
}

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

const currentOutput2 = formatAs12HourClock("19:30");
const targetOutput2 = "07:30 pm";

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

console.log(formatAs12HourClock("19:30"));
Loading