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: 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
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"))
28 changes: 28 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,31 @@
// 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.
14 changes: 14 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

console.log(formatAs12HourClock("23:14"))
console.log(formatAs12HourClock("00:00"))
console.log(formatAs12HourClock("00:01"))
console.log(formatAs12HourClock("11:59"))
console.log(formatAs12HourClock("12:00"))
console.log(formatAs12HourClock("12:01"))
console.log(formatAs12HourClock("13:45"))