whileLoop
- While loop
// Returns an array of even numbers from 0 up to the given value
const whileLoop = (value: number): number[] => {
const result: number[] = [];
let number = 0;
while (number <= value) {
result.push(number);
number = number + 2;
}
return result;
}
console.log(whileLoop(14));
listOfThree
- Array creation
In this code, a
, b
, and c
are the parameter names used inside the function. The console.log
calls the function listOfThree
and passes specific values to it. So, console.log
decides which function to execute and what values to provide.
const listOfThree = (a: any, b: any, c: any): any[] => {
return [a, b, c];
}
console.log(listOfThree(1, 2, 3)); // prints: [1, 2, 3]
console.log(listOfThree("a", "b", "c")); // prints: ["a", "b", "c"]
console.log(listOfThree(28, "yo", false)); // prints: [28, "yo", false]
console.log(listOfThree(undefined, 0, "hi")); // prints: [undefined, 0, "hi"]
createPerson
- Person object creation
In this code, name
, age
, and city
are the parameter names used inside the function. The console.log
calls the function createPerson
and passes specific values to it. So, console.log
decides which function to execute and what values to provide.
const createPerson = (name: string, age: number, city: string) => {
return { name, age, city };
}
console.log(createPerson('John', 30, 'New York')); // prints: { name: 'John', age: 30, city: 'New York' }; example with John
console.log(createPerson('Alice', 25, 'Los Angeles')); // prints: { name: 'Alice', age: 25, city: 'Los Angeles' }; example with Alice
console.log(createPerson('Bob', 40, 'Chicago')); // prints: { name: 'Bob', age: 40, city: 'Chicago' }; example with Bob
returnCompareNumbers
and Task 6 - Number comparison and modification
The function returnCompareNumbers
takes two numbers and returns 'BIGGER'
if the first is bigger, 'LOWER'
if smaller, or 'you decide'
if equal. Task 6 builds on this by adding functions that modify numbers before comparing.
const returnCompareNumbers = (
firstValueInput: number,
secondValueInput: number
): string => {
if (firstValueInput > secondValueInput) {
return 'BIGGER';
} else if (firstValueInput < secondValueInput) {
return 'LOWER';
} else {
return 'you decide';
}
};
// Part 1: Add 5 to first number, then return the bigger of the two
const part1ChangeUpNumbers = (first: number, second: number): number => {
const updatedFirst = first + 5;
const comparison = returnCompareNumbers(updatedFirst, second);
return comparison === 'BIGGER' ? updatedFirst : second;
};
// Part 2: If first is bigger, return it. Otherwise, add 2 repeatedly until it becomes bigger, then return
const part2ChangeUpNumbers = (first: number, second: number): number => {
if (returnCompareNumbers(first, second) === 'BIGGER') {
return first;
}
while (returnCompareNumbers(first, second) !== 'BIGGER') {
first += 2;
}
return first;
};
// Examples:
console.log(part1ChangeUpNumbers(4, 7)); // prints: 9
console.log(part2ChangeUpNumbers(1, 8)); // prints: 9
division
- Math operators
In this code, value1
and value2
are just names used inside the function. The console.log
is the part that runs the function divideNumbers
, and it sends in the values firstInput
and secondInput
. So, console.log
decides which function to use and which values to give it.
const divideNumbers = (value1: number, value2: number): number => {
if (value2 === 0) {
throw new Error("Division by zero is not allowed.");
}
return value1 / value2;
}
const firstInput: number = 10;
const secondInput: number = 5;
console.log(divideNumbers(firstInput, secondInput));
comparison / less-than
- String multiplication
In this code, text
is a string and times
is a number that tells how many times to show that string. The function stringMultiplication
uses a loop to print the string to the console again and again, based on the number given. When the function is called with a string and a number, it prints that string the right number of times, each on its own line using console.log
.