Remove even numbers from an array using JavaScript

JavaScript

JavaScript is a lightweight, cross-platform, single-threaded programming language used to create dynamic content on websites. JavaScript is an interpreted language that executes code line by line providing more flexibility. Widely used in web development, JavaScript enables the creation of interactive and dynamic elements in web applications.

Removing even numbers from an array

Here is a pseudo algorithm to remove even numbers from an array:

  1. Start
  2. Input: An array of numbers
  3. Initialize:
    • originalArray ← input array
    • resultArray ← empty array
  4. For each element number in originalArray do:
    • If number is not even (number % 2 ≠ 0) then:
      • Add number to resultArray
  5. Output: resultArray which contains only the odd numbers from originalArray
  6. End

Removal of even numbers from an array can also be performed in JavaScript. The code below is a function in JavaScript which performs the removal of even numbers from any user-input array.

function removeEvenNumbers() {
    let input = document.getElementById('userInput').value;
    let array = input.split(',').map(Number);
    let oddArray = array.filter(num => num % 2 !== 0);
    document.getElementById('result').innerText = `Result: ${oddArray.join(', ')}`;
}

Explanation of code(line by line):

function removeEvenNumbers() {

This line defines a new function named removeEvenNumbers. Functions in JavaScript are blocks of code designed to perform a particular task when called.

let input = document.getElementById('userInput').value;

document.getElementById('userInput') retrieves the HTML element with the ID userInput, which is an <input> field in this case.

.value gets the current value (text) that the user has entered into this input field.

let input declares a variable named input and assigns it the value from the input field.

let array = input.split(',').map(Number);

input.split(',') splits the string from the input field into an array of substrings based on the comma separator. For example, "1,2,3" becomes ["1", "2", "3"].

.map(Number) converts each substring in the array to a number. This is necessary because split creates an array of strings, but we need an array of numbers for further operations. For example, ["1", "2", "3"] becomes [1, 2, 3].

let array declares a variable named array and assigns it this newly created array of numbers.

let oddArray = array.filter(num => num % 2 !== 0);

array.filter(num => num % 2 !== 0) creates a new array containing only the elements that pass the test implemented by the provided function.

num => num % 2 !== 0 is an arrow function that returns true for numbers that are not divisible by 2 (i.e., odd numbers).

let oddArray declares a variable named oddArray and assigns it the result of filtering out even numbers from the original array.

document.getElementById('result').innerText = Result: ${oddArray.join(‘, ‘)};

document.getElementById('result') retrieves the HTML element with the ID result, which is a <div> in this case.

.innerText sets or returns the text content of this element. Here, it updates the text to show the result.

`Result: ${oddArray.join(', ')}` is a template literal used to create a string. ${oddArray.join(', ')} converts the oddArray into a string where the numbers are separated by commas (e.g., 1, 3, 5). This is inserted into the template literal to create a full result string.

Output

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top