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
15 changes: 11 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Predict and explain first...
// =============> write your prediction here

// we are expecting the code to return a string with the first character being capitalised.
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

Expand All @@ -9,5 +8,13 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
// While calling the function it gave an error "SyntaxError" Identifier 'str' has already been declared " This means that in the code we redeclared a variable name that has been already cleared which is not acceptable in javascript because it cause a naming conflict.
// To fix this we have to change the "let str" into another variable name like " let capitaliseStr" or we can reasign the variable.
// function capitalise(str) {
// let capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`;
//return capitaliseStr;}

//console.log(capitaliseStr("hello"))



19 changes: 14 additions & 5 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Predict and explain first...
// This code should return a percentage value but there are errors in the code wo it wil not work.

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

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

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

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
// The error is the parameter "decimalNumber" has been redeclared as a variable which cause a naming conflict.
//Because the code redeclares a new constant with the same name as the parameter, the argument passed to the function is ignored.
// Next error is is the console.log(decimalNumber), In here you cannot acces a declaredvariale inside a local frame from a global frame


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

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

//console.log(result);
18 changes: 12 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,24 @@

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

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

// We will get an error because we've given the function parameter a (literal value) which is 3 and not a (identifier)parameter name so this will give us a syntax error.
// Next is we are trying to return num * num which has not been declared locally so it will give us an error.
function square(3) {
return num * num;
}

// =============> write the error message here

// =============> explain this error message here
//The errors would be
// syntax error and
//reference error.

// Syntax error- the identifier should be a literal value,it should be parameter name
//reference error - the num* num has not been declared locally
// Finally, correct the code to fix the problem

// =============> write your new code here
//function square(number){
//return number * number
//}
//cosole.log(square(3))



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

// =============> write your prediction here
// In this code we are supposed to have a result of multiplying a and b value but there are errors in tthis code.

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
// This function takes 2 parameters and logs the product of a * b inside the function.
// console.log(a * b) will print the product immediately inside the function,
// but it does not return a value to the caller.
// Therefore, using multiply(10, 32) inside a template literal shows undefined,
// because the function must return a value for the template literal to display it.
/
/ Finally, correct the code to fix the problem
// function multiply (a,b){

// return a*b;
// }

// Finally, correct the code to fix the problem
// =============> write your new code here
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
17 changes: 13 additions & 4 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// This code should return a value of adding a and b but this code has an error

function sum(a, b) {
return;
Expand All @@ -8,6 +8,15 @@ function sum(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
// The function sum takes 2 parameter
//return; was used as statement without a value so the return is undefined.
//a+b; will not run because it comes after return
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); using th function as a template literal will give you undefined since 10+32 was never executed

// // 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)}`)
25 changes: 21 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// In this code we are supposed to get the last digit of the given argument but it will give an error
//because the variable was declared constant so it will ignore the argument given and also the function getLastDigit was not given a parameter so it will not take any argument
// return num.toString().slice(-1); - .slice works on strings and arrays, so here we convert the number to a string so that .slice(-1) works
//the output you will always get is
//The last digit of 42 is 3
//The last digit of 105 is 3
//The last digit of 806 is 3

const num = 103;

Expand All @@ -14,11 +20,22 @@ 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
// I run the code and the output is the same as my prediction
// the output you will always get is
//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 output is the way it is because first there was a costant variable declared which 103
// Then also the function was not give a parameter name so it will not take any argumentt.
// 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
7 changes: 6 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,10 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {

let bmiValue = weight/(height**2)
return bmiValue.toFixed(1)
}

console.log(calculateBMI(70,1.73))
// return the BMI of someone based off their weight and height
}
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){
let capitaliseWord = str.toUpperCase().replace(/ /g, '_')

return capitaliseWord}

console.log(toUpperSnakeCase("hello there"))
console.log(toUpperSnakeCase("lord of the rings"))


17 changes: 17 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,20 @@
// 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"));
console.log(toPounds("5p"));
console.log(toPounds("1250p"));
16 changes: 10 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ 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
// Answer :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: num = 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// Answer: 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 : return value0 =1
// Explanation: num = 1 when pad is called for the last time because this corresponds to remainingSeconds.
// Since num is a number, num.toString() converts it to the string "1".
// Then, .padStart(2, "0") ensures the string is at least 2 characters long, so a "0" is added before "1", giving "01".
// 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
//Answer: return value01
//Explanation: The pad function always returns a string of at least 2 characters. Since the last call’s num was 1, it becomes "01" after padding.
40 changes: 37 additions & 3 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@

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

let displayHours = hours;
let period = "am";

if (hours === 0) {
displayHours = 12; // Midnight
} else if (hours === 12) {
period = "pm"; // Noon
} else if (hours > 12) {
displayHours = hours - 12;
period = "pm";
}
return `${time} am`;

const displayHoursStr = displayHours.toString().padStart(2, "0");
return `${displayHoursStr}:${minutes} ${period}`;
}


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

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

const currentOutput4 = formatAs12HourClock("00:15");
const targetOutput4 = "12:15 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

const currentOutput5 = formatAs12HourClock("13:45");
const targetOutput5 = "01:45 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);