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

//Should return error as str was already declared?
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

Expand All @@ -10,4 +10,11 @@ function capitalise(str) {
}

// =============> write your explanation here
//The parameter name str is already declared as a variable
// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

console.log(capitalise("string"));
11 changes: 9 additions & 2 deletions 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

//2 errors: 1. The parameter decimalNumber is already declared 2. it should show error or undefined as console.log is calling variable not the function
// Try playing computer with the example to work out what is going on

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

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

// Identifier 'decimalNumber' has already been declared -
// 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));
17 changes: 10 additions & 7 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

function square(3) {
return num * num;
}
//we can't declare number as a variable
// function square(3) {
// return num * num;
// }

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

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

//we can't declare number as a variable ?
// Finally, correct the code to fix the problem

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


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

// =============> write your prediction here
// =============> write your prediction here - It will print 320 inside the function, but the template string will show "undefined" because multiply does not return a value.

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
// =============> write your explanation here - multiply() uses console.log to display the result, but it doesn't return anything. In JavaScript, a function with no return statement returns undefined, so ${multiply(10, 32)} becomes undefined even though 320 was logged earlier.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiplyFixed(a, b) {
return a * b;
}
console.log(`The result of multiplying 10 and 32 is ${multiplyFixed(10, 32)}`);
10 changes: 7 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Predict and explain first...
// =============> write your prediction here

// =============> write your prediction here - should show undefined as there are ";" after return inside the function
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you expect from the following function calls?

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

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

console.log(sum3(1, 2));
console.log(sum4(1, 2));

Copy link
Author

Choose a reason for hiding this comment

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

Aren't those the same?
3
3

Copy link
Contributor

Choose a reason for hiding this comment

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

Not the same result. You can test them, and use AI to find out why.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, I didn't know that! The JS auto considers there is ";" after return if there is nothing else in the row

function sum(a, b) {
return;
a + b;
Expand All @@ -10,4 +9,9 @@ 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
// =============> write your new code here - you can't divide the return parameters with ";"
function sum(a, b) {
return a + b;
}

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

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here - it should print last digit of 103

const num = 103;

Expand All @@ -14,11 +14,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
// =============> write the output here - we have set num as constant value 103, inside the function we use it as it is constant, ignoring other inputs
// 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
// =============> write your explanation here -
// 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
// It wasn't working because it used the outer variable num instead of the input value
7 changes: 5 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// It should return 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 Number(bmi.toFixed(1));
}

console.log(calculateBMI(70, 1.73));
6 changes: 6 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,9 @@
// 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 upperSnake(string)
{
return string.trim().split(" ").join("_").toUpperCase()
}
console.log(upperSnake("hello there"))
24 changes: 24 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,27 @@
// 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(str) {
// 1. const penceString = "399p": initialises a string variable with the value "399p"
const penceStringWithoutTrailingP = str.substring(0, str.length - 1);

//2. removes "P" from the string
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//3. Ensures the string is at least 3 characters long by adding 0 to the start
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

//4. Extracts everything except the last 2 digits of paddedPenceNumberString
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
return pence;
}
//5. takes the last 2 digits as the pence from paddedPenceNumberStringCollapse comment
console.log(toPounds("399p"));
console.log(toPounds("400p"));
console.log(toPounds("301p"));
console.log(toPounds("302p"));
14 changes: 7 additions & 7 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ function formatTimeDisplay(seconds) {

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 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
// b) What is the value assigned to num when pad is called for the first time? -
// =============> write your answer here - First call is pad(totalHours)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> write your answer here - it returs 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
// =============> write your answer here - Last call is pad(remainingSeconds) For 61, remainingSeconds = 61 % 60 = 1, so num is 1.

// 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
// =============> write your answer here - With num = 1, pad(1) returns "01". "1".padStart(2, "0") adds zero to make it 2 characters long.
39 changes: 23 additions & 16 deletions Sprint-2/5-stretch-extend/format-time.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not try completing the implementation of formatAs12HourClock(time)?

Copy link
Author

Choose a reason for hiding this comment

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

I was too lazy, will try to.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's never a good excuse!

Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@
// 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) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);
Copy link
Contributor

Choose a reason for hiding this comment

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

.slice(-2) is probably more expressive to mean "extract the last two characters".

let ampm = "am";
let newHours = hours;

if (hours === 0) {
newHours = 12; // midnight
} else if (hours === 12) {
ampm = "pm"; // noon
} else if (hours > 12) {
newHours = hours - 12;
ampm = "pm";
}
return `${time} am`;

const showHours = newHours.toString().padStart(2, "0");
return `${showHours}:${minutes} ${ampm}`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
// Now let's test different times

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