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
13 changes: 12 additions & 1 deletion 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
/* =============> I predict that the function would make 1st character uppercase and
then adds the rest of the string using slice. */

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

//We cannot declare str again because it has already been declared as a parameter of the function.

/* OLD CODE

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

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

function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
13 changes: 11 additions & 2 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> We will get a SyntaxError as decimalNumber has already been declared as a parameter of the function.

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

// =============> write your explanation here
// We don't need to redeclare decimalNumber as it's value comes from function parameter,
// also console.log wont work as decimalNumber is only created 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(0.5));
14 changes: 9 additions & 5 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

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

// =============> write your prediction of the error here
// =============> SyntaxError because function parameter must be a name not a number or value. The error occurs because 3 is a number.

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

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

// =============> explain this error message here
// =============> This error message is cause by trying to assign a number as a parameter, a parameter needs to be identifier such as num.

// Finally, correct the code to fix the problem

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

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

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
// =============> We will log the result in a console but function won't return it as a value.
// console.log only displays the result, it does not return it from the function.

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

//We need to return a * b so the result can be used where the function is called.

// 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)}`);
11 changes: 9 additions & 2 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here

// =============> return on its own means that the function ends without returning a value, so the result is undefined.
/*
function sum(a, b) {
return;
a + b;
}

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

// =============> write your explanation here
// return on it's own means that the function wont refer to parameters and will come up as undefined. a+ b is never reached because it comes after 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)}`);
23 changes: 22 additions & 1 deletion Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

/*
const num = 103;

function getLastDigit() {
Expand All @@ -12,13 +15,31 @@ function getLastDigit() {
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
/*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 this way as there is a num assigned before the function and function don't use the parameter num,
//so the output would only be the return of slice value for const num before the function.

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

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
//Function was not working correctly as it was returning num slice for const num that was assigned before the function, function should use parameter num to work correctly.
6 changes: 5 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,9 @@
// 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 bmiNum = weight / (height * height)
bmiNum=bmiNum.toFixed(1)
return bmiNum
}

console.log(calculateBMI(70,1.73));
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

const str= "hello there"

function strToUpperCase(str) {
return str.toUpperCase().replaceAll(" ","_")
}
console.log(strToUpperCase("lord of the rings"));
43 changes: 43 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,46 @@
// 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

// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js

// You will need to take this code and turn it into a reusable block of code.
// 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(penceString) {
let noP = penceString.substring(0,penceString.length -1)
let paddedP = noP.padStart(3,"0")
let pounds= paddedP.substring(0, paddedP.length -2)

let pence= paddedP.substring(paddedP.length -2).padEnd(2,"0")

return `£${pounds}.${pence}`
}
console.log(toPounds("399p"))
console.log(toPounds("5p"))
console.log(toPounds("52p"))
console.log(toPounds("1244p"))
12 changes: 7 additions & 5 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,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
// =============> pad function will be called 3 times because it's been called 3 times in return statement.

// 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
// =============> Starting value is 0

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> returning value of pad when its called for the 2nd time is "00" :numString = "0" + numString:

// 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
// =============> When pad is called for the last time it's value is 1, The last call is pad(remainingSeconds), and remainingSeconds 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
// =============> // Return value is "01", num is 1, which becomes "1". Since its length is less than 2, the while loop adds "0" to the beginning, making it "01".
62 changes: 59 additions & 3 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = Number(time.slice(3, 5));
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`;
} else if (hours === 12) {
return `${hours}:${minutes.toString().padStart(2, "0")} pm`;
} else if (hours === 0) {
return `12:${minutes.toString().padStart(2, "0")} am`;
}
return `${time} am`;
}
Expand All @@ -14,12 +19,63 @@ 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}`,
);

formatAs12HourClock("19:00");
let output = formatAs12HourClock("19:00");
let target = "07:00 pm";
console.assert(
output === target,
`current output: ${output}, target output: ${target}`,
);
console.log(formatAs12HourClock("19:00"));
// //works
formatAs12HourClock("9:00");
output = formatAs12HourClock("9:00");
target = "9:00 am";
console.assert(
output === target,
`current output: ${output}, target output: ${target}`,
);
console.log(formatAs12HourClock("9:00"));
// //works
formatAs12HourClock("12:00");
output = formatAs12HourClock("12:00");
target = "12:00 pm";
console.assert(
output === target,
`current output: ${output}, target output: ${target}`,
);

console.log(formatAs12HourClock("12:00"));
//Function needs else if statement to includes code behavior when "12:00" will be the argument value.

console.log(formatAs12HourClock("9:00"));
// //works
formatAs12HourClock("00:00");
output = formatAs12HourClock("00:00");
target = "12:00 am";
console.assert(
output === target,
`current output: ${output}, target output: ${target}`,
);
console.log(formatAs12HourClock("00:00"));
//Function needs else if statement to includes code behavior when "00:00" will be the argument value.

formatAs12HourClock("19:37");
output = formatAs12HourClock("19:37");
target = "07:37 pm";
console.assert(
output === target,
`current output: ${output}, target output: ${target}`,
);
console.log(formatAs12HourClock("19:37"));
//For the test to pass I had to create a minutes variable to extract the minutes from the input. When minutes were converted to a number, zero at the start of the string was removed, so padStart(2, "0") was used to add it back.
Loading