Javascript loop an array to find numbers divisible by 3

Javascript loop an array to find numbers divisible by 3

You can loop through an array in JavaScript using various methods such as a for loop, forEach method, or for...of loop, and then check each element to see if it's divisible by 3. Here's how you can do it using a forEach loop:

// Example array const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Array to store numbers divisible by 3 const divisibleBy3 = []; // Loop through the array using forEach numbers.forEach(number => { if (number % 3 === 0) { divisibleBy3.push(number); } }); // Print the numbers divisible by 3 console.log("Numbers divisible by 3:", divisibleBy3); 

This code will output:

Numbers divisible by 3: [3, 6, 9] 

In this example:

  • We define an array numbers containing some numbers.
  • We initialize an empty array divisibleBy3 to store numbers divisible by 3.
  • We loop through the numbers array using the forEach method.
  • For each element in the array, we check if it's divisible by 3 using the modulo operator %. If the remainder is 0, it means the number is divisible by 3, so we push it into the divisibleBy3 array.
  • Finally, we print the numbers in the divisibleBy3 array.

Examples

  1. "JavaScript loop array find numbers divisible by 3" Description: This query seeks methods to iterate through an array in JavaScript and identify numbers that are divisible by 3.

    // Loop through an array to find numbers divisible by 3 const numbers = [1, 3, 6, 9, 12, 15]; const divisibleByThree = []; for (let num of numbers) { if (num % 3 === 0) { divisibleByThree.push(num); } } console.log(divisibleByThree); // Output: [3, 6, 9, 12, 15] 
  2. "JavaScript find array elements divisible by 3" Description: This query focuses on finding elements in an array that are divisible by 3 using JavaScript.

    // Find array elements divisible by 3 using a loop const numbers = [2, 3, 5, 6, 9, 12]; const divisibleByThree = []; for (let i = 0; i < numbers.length; i++) { if (numbers[i] % 3 === 0) { divisibleByThree.push(numbers[i]); } } console.log(divisibleByThree); // Output: [3, 6, 9, 12] 
  3. "Loop through JavaScript array and find multiples of 3" Description: This query looks for ways to iterate over a JavaScript array and identify elements that are multiples of 3.

    // Loop through an array to find multiples of 3 const numbers = [4, 6, 8, 9, 15, 18]; const multiplesOfThree = []; for (let number of numbers) { if (number % 3 === 0) { multiplesOfThree.push(number); } } console.log(multiplesOfThree); // Output: [6, 9, 15, 18] 
  4. "JavaScript iterate array to find numbers divisible by 3" Description: This query focuses on iterating through a JavaScript array to identify numbers that are divisible by 3.

    // Iterate through an array to find numbers divisible by 3 const numbers = [3, 4, 6, 8, 9, 12]; const divisibleByThree = []; numbers.forEach(number => { if (number % 3 === 0) { divisibleByThree.push(number); } }); console.log(divisibleByThree); // Output: [3, 6, 9, 12] 
  5. "JavaScript array loop to find numbers divisible by 3" Description: This query is about looping through a JavaScript array to find numbers that are divisible by 3.

    // Loop through an array to find numbers divisible by 3 const numbers = [2, 3, 6, 8, 9, 12]; const divisibleByThree = []; for (let i = 0; i < numbers.length; i++) { if (numbers[i] % 3 === 0) { divisibleByThree.push(numbers[i]); } } console.log(divisibleByThree); // Output: [3, 6, 9, 12] 
  6. "JavaScript iterate through array to find numbers divisible by 3" Description: This query aims to iterate through a JavaScript array and identify numbers divisible by 3.

    // Iterate through an array to find numbers divisible by 3 const numbers = [3, 5, 6, 9, 10, 12]; const divisibleByThree = []; for (let i = 0; i < numbers.length; i++) { if (numbers[i] % 3 === 0) { divisibleByThree.push(numbers[i]); } } console.log(divisibleByThree); // Output: [3, 6, 9, 12] 
  7. "JavaScript loop array for numbers divisible by 3" Description: This query looks for a loop-based approach in JavaScript to identify numbers divisible by 3 within an array.

    // Loop through an array to find numbers divisible by 3 const numbers = [2, 3, 6, 8, 9, 12]; const divisibleByThree = []; for (let i = 0; i < numbers.length; i++) { if (numbers[i] % 3 === 0) { divisibleByThree.push(numbers[i]); } } console.log(divisibleByThree); // Output: [3, 6, 9, 12] 
  8. "How to find elements divisible by 3 in JavaScript array" Description: This query seeks methods to find elements divisible by 3 within a JavaScript array.

    // Find elements divisible by 3 in a JavaScript array const numbers = [2, 3, 5, 6, 9, 12]; const divisibleByThree = numbers.filter(num => num % 3 === 0); console.log(divisibleByThree); // Output: [3, 6, 9, 12] 
  9. "JavaScript loop through array to find numbers divisible by three" Description: This query is about looping through a JavaScript array to identify numbers that are divisible by three.

    // Loop through an array to find numbers divisible by three const numbers = [3, 6, 9, 12, 15, 18]; const divisibleByThree = []; for (let i = 0; i < numbers.length; i++) { if (numbers[i] % 3 === 0) { divisibleByThree.push(numbers[i]); } } console.log(divisibleByThree); // Output: [3, 6, 9, 12, 15, 18] 
  10. "JavaScript iterate through array and find multiples of 3" Description: This query seeks methods to iterate through a JavaScript array and identify elements that are multiples of 3.

    // Iterate through an array and find multiples of 3 const numbers = [2, 3, 6, 8, 9, 12]; const multiplesOfThree = []; for (let num of numbers) { if (num % 3 === 0) { multiplesOfThree.push(num); } } console.log(multiplesOfThree); // Output: [3, 6, 9, 12] 

More Tags

ssh-agent numerical-methods click ckfinder vlc bootstrap-selectpicker android-linearlayout libavformat request-promise egit

More Programming Questions

More Physical chemistry Calculators

More Biochemistry Calculators

More Housing Building Calculators

More Livestock Calculators