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

// this let statement is trying to declare a variable with the same name as the function parameter 'str',
// which will cause a syntax error because you cannot redeclare a parameter within the same scope.

// call the function capitalise with a string input

// we don't call the function here, but if we did, it would result in an error due to the redeclaration of 'str'.
// interpret the error message and figure out why an error is occurring

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
// function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }

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


function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise('hello'));
// Hello
23 changes: 17 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// SyntaxError: Identifier 'decimalNumber' has already been declared
// We don't need to redeclare the variable decimalNumber inside the function because it's already declared as a
// parameter. This causes a conflict in the scope of the function.
// =============> write your prediction here

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

return percentage;
}
// return percentage;
// }

console.log(decimalNumber);
// console.log(decimalNumber);

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

// 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.75)); // 75%
console.log(convertToPercentage(0.2)); // 20%
console.log(convertToPercentage(0.5)); // 50%
10 changes: 8 additions & 2 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@

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

// We neet to call the function square with a number argument, and change the parameter name to a num here.
// 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) {
function square(num) { // changed parameter name from 3 to num
return num * num;
}
console.log(square(4)); // 16
console.log(square(10));

// =============> write the error message here
// function square(3) {
// ^

// SyntaxError: Unexpected number

// =============> explain this error message here

Expand Down
6 changes: 4 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Predict and explain first...

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

// We're going to get 320 and 'The result of multiplying 10 and 32 is undefined'
// because the multiply function does not return a value; it only logs the product to the console.
function multiply(a, b) {
console.log(a * b);
return (a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
Expand All @@ -12,3 +13,4 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// Finally, correct the code to fix the problem
// =============> write your new code here
// we have to replace console.log with return in the multiply function
5 changes: 3 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Predict and explain first...
// =============> write your prediction here
// We're going to get undefined because the sum function has a return statement that does not return any value.

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
// Finally, correct the code to fix the problem
// =============> write your new code here
// we just remove the semicolon after return statement and return the sum of a and b
14 changes: 10 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

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

// The output will be:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3
// because the function getLastDigit always returns the last digit of the constant variable num which is set to 103.
const num = 103;

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

}

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
Expand All @@ -19,6 +24,7 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here

// now it's working properly because we are returning the last digit of the argument a passed to the function getLastDigit
// instead of the constant variable num.
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
8 changes: 7 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const bmi = weight / (height * height);
return parseFloat(bmi.toFixed(1));
// return the BMI of someone based off their weight and height
}
}
const yourBMI = calculateBMI(70, 1.73);
console.log(`Your BMI is ${yourBMI}`); // Your BMI is 23.4
const anotherBMI = calculateBMI(95, 1.88);
console.log(`Another person's BMI is ${anotherBMI}`); // Another person's BMI is 26.8
9 changes: 9 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,12 @@
// 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) {
return str.toUpperCase().replace(/ /g, '_');

}

console.log(toUpperSnakeCase('hello there')); // "HELLO_THERE"
console.log(toUpperSnakeCase('lord of the rings')); // "LORD_OF_THE_RINGS"
console.log(toUpperSnakeCase('JavaScript is fun')); // "JAVASCRIPT_IS_FUN"
19 changes: 19 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,22 @@
// 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(penceString) {
// return the amount in pounds and pence format
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
// Ensure at least 3 digits for correct slicing
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// Split into pounds and pence
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// Get last two digits for pence
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");

return `£${pounds}.${pence}`;
}

console.log(toPounds("399p")); // £3.99
console.log(toPounds("50p")); // £0.50
console.log(toPounds("5p")); // £0.05
console.log(toPounds("1234p")); // £12.34
77 changes: 43 additions & 34 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
function pad(num) {
return num.toString().padStart(2, "0");
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

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

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

// c) What is the return value of pad is called for the first time?
// =============> 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
// =============> 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
// =============> write your answer here
function pad(num) {
return num.toString().padStart(2, "0");
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;
console.log(totalHours);

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(formatTimeDisplay(12345));

// 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 (when calculating hours)

// 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 (when calculating seconds)


// 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
// "01" (when calculating seconds)
23 changes: 23 additions & 0 deletions Sprint-2/4-mandatory-interpret/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function formatAs12HourClock(time) {
const [hoursStr, minutes] = time.split(":");
const hours = parseInt(hoursStr, 10);
if (hours > 24 || minutes >= 60) {
throw new Error("Invalid time format");
}
if (hours >= 12) {
const adjustedHours = hours > 12 ? hours - 12 : hours;
return `${adjustedHours}:${minutes} pm`;
} else {
return `${hours}:${minutes} am`;
}
}

const moment = "13:45";
console.log(formatAs12HourClock(moment)); // Output: 2:00 pm
// console.log(formatAs12HourClock("00:30")); // "12:30 am"

// const currentOutput = formatAs12HourClock("08:00");
// const targetOutput = "08:00 am";
// console.assert(
// currentOutput === targetOutput,
// `current output: ${currentOutput}, target output: ${targetOutput}`);
16 changes: 16 additions & 0 deletions Sprint-2/codewars/Create Phone Number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function createPhoneNumber(numbers) {
const area = numbers.slice(0, 3).join('');
const middle = numbers.slice(3, 6).join('');
const last = numbers.slice(6).join('');

return `(${area}) ${middle}-${last}`;
}


console.assert(createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) === '(123) 456-7890', 'Test Case 1 Failed');
console.assert(createPhoneNumber([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) === '(012) 345-6789', 'Test Case 2 Failed');



console.log(createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])); // Output: (123) 456-7890
console.log(createPhoneNumber([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])); // Output: (012) 345-6789
34 changes: 34 additions & 0 deletions Sprint-2/codewars/Fun with ES6 Classes
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Human {
firstName: string;
lastName: string;
age: number;
gender: string;

constructor(
firstName: string = "John",
lastName: string = "Doe",
age: number = 0,
gender: string = "Male"
) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
}

sayFullName(): string {
return `${this.firstName} ${this.lastName}`;
}

static greetExtraTerrestrials(raceName: string): string {
return `Welcome to Planet Earth ${raceName}`;
}
}

// Example usage
const roman = new Person("Roman", "Pavlenko", 36, "Male");
console.log(roman.sayFullName());
// Output: Roman Pavlenko

console.log(Person.greetExtraTerrestrials("Martians"));
// Output: Welcome to Planet Earth Martians
15 changes: 15 additions & 0 deletions Sprint-2/codewars/Fun with ES6 Classes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Person {
constructor(firstName = 'John', lastName = 'Doe', age = 0, gender = 'Male') {
Object.assign(this, { firstName, lastName, age, gender });
}
sayFullName() {
return `${this.firstName} ${this.lastName}`;
}
static greetExtraTerrestrials(raceName) {
return `Welcome to Planet Earth ${raceName}`;
}
}
const person1 = new Person('Roman', 'Pavlenko', 36, 'Male');
console.log(person1.sayFullName()); // Output: Jane Doe

console.log(Person.greetExtraTerrestrials('Martians')); // Output: Welcome to Planet Earth Martians
Loading