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

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -8,6 +9,13 @@ function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
capitalise("Hello");

// =============> write your explanation here
// SyntaxError: Identifier 'str' has already been declared - This error means that the variable 'str' was declared more than once in the same scope.
// JavaScrip doesn't allow that - once a variable name is declared with let or const, you can't declare it again.
// =============> write your new code here
function capitalise(str) {
return str[0].toUpperCase() + str.slice(1);
}

17 changes: 17 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict that this program will throw a error message - due to the variable decimalNumber being redeclared

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

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

// =============> write your explanation here
// Function covertToPercentage(decimalNumber) - it has a parameter named decimalNumber
// Inside the function: const decimalNumber = 0.5; - this redeclares the same variable name that's already used for the parameter.
// That causes a error: SyntaxError: Identifier 'decimalNumber' has already been declared - because you can't declare a const (or let)
// with the same name as a parameter inside the same function scope.
// JavaScript doesn't allow us to declare a new variable with the same name in the same scope, so it caused a error

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

// Function decimalNumber = 0.5
// It calculates 0.5 * 100 = 50
// It returns "50%"
// console logs 50%
11 changes: 8 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ function square(3) {
}

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

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

// The "Unexpected number"
// Finally, correct the code to fix the problem

// The "Unexpected number" error arises when a number is improperly positioned or used within JvaScript code
// =============> write your new code here
function square(num){
return num * num;
}

console.log(square);


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

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

// It will show a undefined error
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

// The function printed the answer but did't return it, so when it was used in the template string, it came out as 'undefined.'
// 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 multipying 10 and 32 is ${multiply(10, 32)}`);

7 changes: 7 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// I predict it will show a undefined error

function sum(a, b) {
return;
Expand All @@ -9,5 +10,11 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The problem was the semicolon after return, which made the function stop before adding the numbers
// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b){
return a + b; // Once I put a + b on the same line as return, it worked
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
17 changes: 16 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Predict the output of the following code:
// =============> Write your prediction here

// Since getLastDigit() does not declare any parameters, the arguments passed when calling it are going to be ignored
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +15,25 @@ 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 function getLastDigit() does not have any parameters, but it is being called with arguments:
// getLastDigit(42), getLastDigit(105), and getLastDigit(806). Inside the function, it always uses the global const num, which is set to 103.
// The means every call ignores the last digit of 103. Which the '3'


// 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
9 changes: 8 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,11 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
return bmi.toFixed(1);
}

console.log(`Your BMI is ${calculateBMI(68, 1.75)}`);
// How it works: function calculateBMI(weight, height)
// Parameters: weight - number, person's weight in kilograms height - number, person's height in meters
// Returns: the body mass index (BMI), rounded to one decimal place
3 changes: 3 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,6 @@
// 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 greeting = "Hello there."

console.log(greeting.toUpperCase());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach will work, but this task asks you to create a function. Can you create a named function that takes a string and returns it in uppercase?

Making a named function is important as it makes it easier to test code than using console.log()s

10 changes: 10 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,13 @@
// 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 formatPenceToPounds(penceString){
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length -1);
const pounds = paddedPenceNumberString.slice(0, -2);
const pence = paddedPenceNumberString.slice(-2);
return `£${pounds}.${pence}`;
}

console.log(formatPenceToPounds("9p"));
console.log(formatPenceToPounds("39p"));
console.log(formatPenceToPounds("399"));
12 changes: 11 additions & 1 deletion Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// Three 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

/* The first time pad() run, its used for hours part of time.
Because 61 seconds equals 0 hours, the value given to num is 0. */
// c) What is the return value of pad is called for the first time?
// =============> write your answer here
/* The first time pad() runs, it's formatting the hours part of time.
Since the hours value is 0, it turns that into the string '00' by adding a leading zero before returning it. */

// 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 the function runs with 61 seconds, the last time pad() is called is for the second part of the time.
Since 61 seconds has 1 second left over after making a full minute, the value passed into pad() is 1.
Inside the function, num equals 1, and it gets turned into '01' so the final time shows as 00:01: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
/* When pad() is called for the last time, num is 1 because there's 1 second left over.
The function turns that number into the string '01' by adding a leading zero.
That '01' is the value that gets returned and used as the seconds part of the final time display (00:01:01). */
53 changes: 52 additions & 1 deletion Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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) {
/*function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
Expand All @@ -22,4 +22,55 @@ const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
); */

// The function ignores the minutes and did'not handle midnight (00:00) or noon (12:00) properly.
// Better version:
function formatAs12HourClock(time) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work attempting this stretch task!

If we look at your tests, you try 08:00 which gets returned as 08:00 am, and trying 13:00 gives you 1:00 pm. There seems to be a difference in how am/pm handle leading 0s. Can you find why that happens?

const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);

if (hours == 0) return `12:${minutes} am`;
if (hours < 12) return `${time} am`;
if (hours === 12) return `${time} pm`;

return `${hours - 12}:${minutes} pm`;
}
const currentOutput1 = formatAs12HourClock("08:00");
const targetOutput1 = "08:00 am";
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);
// Test failed: due to a typo
const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`

);

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

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

console.log(formatAs12HourClock("08:00"));
console.log(formatAs12HourClock("23:00"));
console.log(formatAs12HourClock("15:00"));
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("12:00") === "12:00 pm");

// The test passed, so my function gave the expected results.
// All the console.assert() tests passed.
// formatAs12HourClock() function works correctly for the inputs I tested.