Skip to content
18 changes: 15 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// Predict and explain first...
// =============> write your prediction here
// I thought it was going to work, however my prediction was wrong. =============> write your prediction here

// 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 new code here



// The identifier "str" has already been declared as a parameter in the function and cannot be redeclared withing a block scope.// =============> write your explanation here


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

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

// =============> write your explanation here
// we already have a parameter "decimalNumber" which has been declared in the function. declaring it again with constant is going to give syntax error.
// Also the console.log is trying to print "decimalNumber" which only exist in a function that is outside its scope.=============> write your explanation here

// 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));// =============> write your new code here
15 changes: 9 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

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

// =============> write your prediction of the error here
// A number cannot be used as a parameter.=============> write your prediction of the error here

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

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


// =============> explain this error message here
// The parameter is not meant to be a number. Also we are trying to return num that was not never created. =============> explain this error message here

// Finally, correct the code to fix the problem


function square (num) {
return num * num;

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


19 changes: 12 additions & 7 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here
// I am predicting there will be an error. =============> write your prediction here



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

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

// =============> write your explanation here
// Although it is going to print the value to be 320, it won't return back the value as 320.=============> write your explanation here

// Finally, correct the code to fix the problem
// =============> write your new code here


function multiply(a, b) {
return (a * b);
}

console.log(`${multiply(10, 32)}`)
//=============> write your new code here
19 changes: 11 additions & 8 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Predict and explain first...
// =============> write your prediction here
// There will be an error because there is going to be nothing to define after return. =============> write your prediction here

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

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

// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here


//Finally, correct the code to fix the problem
function sum(a, b) {
return a+b;

}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
//=============> write your new code here
20 changes: 10 additions & 10 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// IPredict the output of the following code:
// I am predicting an error because the variable const num = 103 is outside the function. =============> Write your prediction here
// There is no parameters in the function condition

const num = 103;

function getLastDigit() {

// Now run the code and compare the output to your prediction
// when i ran the output i got 3,3,3.=============> write the output here
// Explain why the output is the way it is
// the argument for the console.log is ignored because "getlastDigit" does not have a parameter. =============> write your explanation here
// Finally, correct the code to fix 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)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// 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.
Expand Down
8 changes: 7 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
// It should return 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 (calculateBMI(90,1.92));

// After running node console.;og printed (24.4).
9 changes: 9 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,12 @@
// 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("Code Your Future"));

// After i ran node it printed (CODE_YOUR_FUTURE).
29 changes: 29 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,32 @@
// 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("399p")); //£3.99
console.log (toPounds("156p")); //£1.56
console.log (toPounds("10p")); //£0.10
console.log (toPounds("56p")); //£0.56
console.log (toPounds("25p")); // £0.25

// After running node it printed the above respectively.
10 changes: 5 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 3 times. =============> write your answer here

// 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 num is 0. =============> write your answer here

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
num.toString().padStart(2, "0"); // answer will be "00"=============> write your answer here

// 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
// num will be 1. The last pad is for the seconds value. =============> write your answer here

// 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
// Ans is "01" because padStart will enable two digits thereby adding 0 in front of it. =============> write your answer here
31 changes: 26 additions & 5 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
// 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.


function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
let [hours, minutes] = time.split(":").map(Number);
let period = hours >= 12 ? "pm" : "am";

if (hours === 0) {
hours = 12;
} else if (hours > 12) {
hours -= 12;
}
return `${time} am`;
}

const hoursString = hours < 10 ? `0${hours}` : `${hours}`;
const minutesString = minutes < 10 ? `0${minutes}` : `${minutes}`;

return `${hoursString}:${minutesString} ${period}`;
const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
Expand All @@ -23,3 +30,17 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

const currentOutput3 = formatAs12HourClock("21:00");
const targetOutput3 = "09:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);
}



console.log(formatAs12HourClock("08:00")); // 08:00 am
console.log(formatAs12HourClock("23:00")); // 11:00 pm
console.log(formatAs12HourClock("21:00")); // 09:00 pm
Loading