Skip to content

Instantly share code, notes, and snippets.

@ikotse-code
Created January 27, 2026 03:27
Show Gist options
  • Select an option

  • Save ikotse-code/f21797dac26e836a37d90166ba34d0e9 to your computer and use it in GitHub Desktop.

Select an option

Save ikotse-code/f21797dac26e836a37d90166ba34d0e9 to your computer and use it in GitHub Desktop.
HW15
// length property tells how many elements are inside the array
let fruits = ["apple", "banana", "orange"];
console.log(fruits.length);
// push() method: adds a new element to the end of the array
fruits = ["apple", "banana"];
fruits.push("orange");
console.log(fruits.length);
// pop() method: removes the last element from the array
fruits = ["apple", "banana", "orange"];
fruits.pop();
console.log(fruits.length);
console.log("=====================================================================")
// ARRAY TASKS
console.log("ARRAY TASKS");
// 1️⃣ Create an array with three numbers
console.log("=====================================================================")
console.log("1. Create an array with three numbers")
let numbers = [1, 2, 3];
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
// 2️⃣ Create an array with two strings
console.log("=====================================================================")
console.log("2. Create an array with two strings")
console.log("2.1 Add one more string to the end of the array using the push method")
let strings = ["string 1", "string 2"];
strings.push("string 3");
console.log("Array: ", strings);
console.log("Length: ", strings.length);
// 3️⃣ Create an array with three strings
console.log("=====================================================================")
console.log("3. Create an array with three strings")
console.log("3.1 Remove the last element using the pop method")
strings = ["string 1", "string 2", "string 3"];
strings.pop();
console.log("Array: ", strings);
console.log("Length: ", strings.length);
// 4️⃣ Create an empty array of numbers.
console.log("=====================================================================")
console.log("4 Create an empty array of numbers.")
console.log("4.1 Add two numbers to the array")
numbers = [];
numbers.push(1);
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
numbers.push(2);
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
console.log("=====================================================================")
// LOOP TASKS
console.log("LOOP TASKS");
// 1️⃣ Create an array of five numbers
console.log("=====================================================================")
console.log("1. Create an array of five numbers")
numbers = [1, 2, 3, 4, 5];
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
for (let i = 0; i < numbers.length; i++) {
console.log(`Element ${[i]}: `, numbers[i]);
}
// 2️⃣ Create an array of five numbers
console.log("=====================================================================")
console.log("2. Create an array of five numbers")
console.log("2.1 Use a for loop to calculate the sum of all elements")
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
console.log(`Sum: `, sum);
// 3️⃣ Create an array of three numbers
console.log("=====================================================================")
console.log("3. Create an array of three numbers")
console.log("3.1 Create a new array where each element multiplied by 2")
numbers = [1, 2, 3];
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
for (let i = 0; i < numbers.length; i++) {
numbers[i] = numbers[i] * 2;
}
console.log("New array: ", numbers);
// 4️⃣ Create an array of three numbers
console.log("=====================================================================")
console.log("4. Create an array of three numbers")
console.log("4.1 Use a for loop to print elements of the array in reverse order")
numbers = [1, 2, 3];
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
// const newArray = [];
// for (let i = numbers.length - 1; i >= 0; i--) {
// newArray.push(numbers[i]);
// }
for (let i = 0; i < numbers.length / 2; i++) {
const temp = numbers[i];
numbers[i] = numbers[numbers.length - 1 - i];
numbers[numbers.length - 1 - i] = temp;
}
console.log("Reversed array: ", numbers);
console.log("=====================================================================")
// TYPICAL INTERVIEW TASKS
console.log("TYPICAL INTERVIEW TASKS");
// 1️⃣ Find the maximum number in an array
console.log("=====================================================================")
console.log("1. Find the maximum number in an array")
numbers = [7, 2, 10, 4, 5];
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
let max = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
console.log("Max number: ", max);
// 2️⃣ Find the minimum number in an array
console.log("=====================================================================")
console.log("2. Find the minimum number in an array")
numbers = [7, 2, 10, 4, 5];
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
let min = numbers[0];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
console.log("Min number: ", min);
// 3️⃣ Count the number of even numbers in an array
console.log("=====================================================================")
console.log("3. Count the number of even numbers in an array")
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
let count = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
count++;
}
}
console.log("Even numbers: ", count);
// 4️⃣ Create a new array from positive numbers
console.log("=====================================================================")
console.log("4. Create a new array from positive numbers")
numbers = [7, 0, -10, 4, -5];
console.log("Array: ", numbers);
console.log("Length: ", numbers.length);
let emptyArray = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > 0) {
emptyArray.push(numbers[i]);
}
}
console.log("Positive numbers: ", emptyArray);
// using map
let positiveNumbers = numbers
.map(num => (num > 0 ? num : null))
.filter(num => num !== null);
console.log("Positive numbers:", positiveNumbers);
console.log("=====================================================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment