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
10 changes: 5 additions & 5 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Predict and explain first...
// =============> write your prediction here
// ===========> I predict there will be an error when the function is called.

// 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)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// =============> write your new code here
capitalise("hello");
// ===========> The error happens because str has already been declared as a function parameter.
// ===========> Removed let because str is already declared as a function parameter.
9 changes: 4 additions & 5 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// ===========> I predict there will be an error because decimalNumber is declared more than once.

// 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(convertToPercentage(0.5));

// =============> write your explanation here
// ===========> The error happens because decimalNumber is already declared as a function parameter.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ===========> Removed the duplicate declaration and called the function with 0.5.
15 changes: 6 additions & 9 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@

// Predict and explain first BEFORE you run any code...

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

// =============> write your prediction of the error here
// ===========> I predict there will be a syntax error because 3 cannot be used as a function parameter.

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

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

// =============> explain this error message here
// ===========> The error happens because a number cannot be used as a function parameter name.

// Finally, correct the code to fix the problem

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


// ===========> Changed 3 to num so the function has a valid parameter name.
8 changes: 4 additions & 4 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Predict and explain first...

// =============> write your prediction here
// ===========> I predict the multiplication will print 320, but the final result will show undefined.

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

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

// =============> write your explanation here
// ===========> The function prints the result instead of returning it, so the function returns undefined.

// Finally, correct the code to fix the problem
// =============> write your new code here
// ===========> Changed console.log to return so the function returns the multiplication result.
9 changes: 4 additions & 5 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Predict and explain first...
// =============> write your prediction here
// ===========> I predict the result will be undefined because the function returns before adding the numbers.

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

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

// =============> write your explanation here
// ===========> The function returns before a + b is calculated, so the result is undefined.
// Finally, correct the code to fix the problem
// =============> write your new code here
// ===========> Changed the return statement so it returns a + b.
11 changes: 5 additions & 6 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// ===========> I predict all three results will be 3 because the function always uses num, which is 103.

const num = 103;

function getLastDigit() {
function getLastDigit(num) {
return num.toString().slice(-1);
}

Expand All @@ -15,10 +13,11 @@ 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
// ============> The output is 3 for all three numbers.
// ============> The function ignores the numbers passed to it and always uses num, which is 103.
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
// ============> Added num as a parameter so the function uses the number passed to it.

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
3 changes: 2 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,6 @@
// 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
const bmi = weight / (height * height);
return bmi.toFixed(1);
}
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
function toUpperSnakeCase(str) {
const upperCaseString = str.toUpperCase();
const snakeCaseString = upperCaseString.replaceAll(" ", "_");
return snakeCaseString;
}
console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
9 changes: 9 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,12 @@
// 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(pence) {

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.

Your function works for numbers, but it is not the program this exercise asks for.

The exercise wants you to take the program in Sprint-2/3-mandatory-interpret/3-to-pounds.js and turn it into a function. That program:

  • takes a string like "399p"
  • gives back a string like "£3.99"

Your function takes a number, like 250, and gives back a number, 2.5. A price should show as £2.50.

Please open that Sprint 2 file. Put its code inside your toPounds function, and use the parameter in place of "399p". Then call the function with a few different inputs, for example "399p", "5p" and "2500p".

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.

Your function works now. A string goes in, and a string comes out. Good.

But this exercise had a different aim. The Sprint 2 code in Sprint-2/3-mandatory-interpret/3-to-pounds.js already changes pence to pounds, and it works. But it only works for one price, because "399p" is fixed on line 1. To change "5p" or "2500p", you would need to copy all the lines again.

A function solves this. When we need the same lines many times, we put them in a function. The part that changes each time becomes the parameter. Here, the part that changes is the pence amount. The steps stay the same.

So you did not need to write new steps. Your three lines do the same jobs as the Sprint 2 lines:

Job Your code Sprint 2 code
Remove the p line 8: Number(pence.replace("p", "")) lines 3 to 6: penceString.substring(...)
Split into pounds and pence line 9: penceNumber / 100 lines 8 to 16: padStart, substring, padEnd
Build the price line 10: `£${pounds.toFixed(2)}` line 18: `£${pounds}.${pence}`

You only needed to change two Sprint 2 lines:

  • Sprint 2 line 1, const penceString = "399p";, goes away. The parameter takes its place. If you call the parameter penceString, lines 3 to 16 work with no changes at all.
  • Sprint 2 line 18 uses console.log. In a function, change it to return.

The function looks like this:

function toPounds(penceString) {
  // Sprint 2 lines 3 to 16 go here, with no changes
  return `£${pounds}.${pence}`;
}

Now the same lines work for every price. toPounds("399p"), toPounds("5p") and toPounds("2500p") all use them. Only the value you pass in changes.

The rule: if you need the same lines many times, put them in a function. Make the part that changes a parameter.

const penceNumber = Number(pence.replace("p", ""));
const pounds = penceNumber / 100;
return `£${pounds.toFixed(2)}`;
}

console.log(toPounds("399p"));
console.log(toPounds("5p"));
console.log(toPounds("2500p"));
14 changes: 7 additions & 7 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
// 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 when it 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
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// ============> 1, because 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
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// ============> "01", because pad adds a 0 to make the value two characters long.
33 changes: 18 additions & 15 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@

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

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";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
return `${String(hours).padStart(2, "0")}${minutes} am`;
}

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.assert(formatAs12HourClock("08:00") === "08:00 am");
console.assert(formatAs12HourClock("23:00") === "11:00 pm");
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("12:00") === "12:00 pm");
console.assert(formatAs12HourClock("13:30") === "01:30 pm");
Loading