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

// it will take a string and turn the first letter into a capital.
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

// SyntaxError: Identifier 'str' has already been declared - str cannot be used twice as a variable name.
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// i've changed the variable name from str to result. This corrected the error and return result.
// =============> write your new code here
function capitalise(str) {
let result = `${str[0].toUpperCase()}${str.slice(1)}`;
console.log("result",result);
return result;
}
capitalise("pear")
10 changes: 9 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

// They have used the same variable name twice. This will cause an error. Also line 9 is not needed because the function takes a decimal as an argument.
// Try playing computer with the example to work out what is going on

function convertToPercentage(decimalNumber) {
Expand All @@ -15,6 +15,14 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// SyntaxError: Identifier 'decimalNumber' has already been declared.
// ReferenceError: decimalNumber is not defined

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
console.log("percentage",percentage)
return percentage;
}
convertToPercentage(0.8)
11 changes: 7 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
// this function should square any number but instead we're going to get an error

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

// You can't put a number value into an argument.
function square(3) {
return num * num;
}

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

// SyntaxError: Unexpected number
// =============> explain this error message here

// It is not expecting a number to be in the function argument.
// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


console.log("square",square(5))
10 changes: 9 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

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

// the function will not return anything.
function multiply(a, b) {
console.log(a * b);
}
Expand All @@ -10,5 +10,13 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

// declared a variable called result and stored a*b.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
console.log(a * b);
const result = a*b;
return result;
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11 changes: 9 additions & 2 deletions Sprint-2/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

// This appears correct. The function has been defined and called sum, a is 10 and b is 32 so we should get 42.
function sum(a, b) {
return;
a + b;
a + b;
}

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

// =============> write your explanation here
// The sum of 10 and 32 is undefined. On line five the function has been exited because of the ; this because the function doesn't return anything.
// 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)}`);
13 changes: 13 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Predict the output of the following code:
// =============> Write your prediction here

// It will return three as a string.
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +16,22 @@ 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
// "3"

// Explain why the output is the way it is
// =============> write your explanation here
// This is because they've defined num as 103, and they've used num inside the function, last digit of 103 is 3.
// Finally, correct the code to fix the problem
// =============> write your new code here

function getLastDigit(aNumber) {
return aNumber.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
// This is because they've defined num as 103 so the result will always be 3. The arguments should be used instead (42, 105, and 806) and num should not be used.
11 changes: 10 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
console.log("weight", weight);
console.log("height",height);

const heightSquared = (height * height).toFixed(2);
console.log("heightSquared",heightSquared);
const BMI = (weight / heightSquared).toFixed(2);
console.log("BMI",BMI)
return BMI;
}
console.log("calculateBMI",calculateBMI(70, 1.73))
7 changes: 7 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,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 makeUpperCaseSnakeCase(str){
const result = str.split(" ").join("_").toUpperCase(); //take str, and turn it into an array and then turn it back into a string with underscores then upper case it.
console.log("result",result);
}

console.log(makeUpperCaseSnakeCase("hello there"));
15 changes: 15 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,18 @@
// 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

let count = 0;

count = count + 1;

function toPounds(pennies) {
let pounds = pennies / 100;
return pounds;
}

console.log(toPounds(50));
console.log(toPounds(100));
console.log(toPounds(150));
console.log(toPounds(250));
console.log(toPounds(350));
18 changes: 12 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
function pad(num) {
console.log("num",num);
return num.toString().padStart(2, "0");
}



function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
Expand All @@ -11,24 +14,27 @@ 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
// 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
// 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// 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
// 1, this is because they assign remainingSeconds to num for the last time pad is called.

// 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
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer - "do you mean the return value for pad?"
// 01, this is the result of calling pad for the last time.
35 changes: 35 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ function formatAs12HourClock(time) {
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
if (hours === 0) {
return `${12}:00 am`;
}
return `${time} am`;
}
console.log("formatAs12HourClock",formatAs12HourClock("17:00"))

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

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

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

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

const currentOutput6 = formatAs12HourClock("23:55");
const targetOutput6 = "11:55 am";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
);
// test mid-day
// test 08:30
// test 11:55