Sam
Sam

Reputation: 3

JavaScript loop push to new array

I am attempting to push all numbers divisible by 3 into a new array "threes" but have hit a wall with why this code is not working.

var numbers = function () {
  var threes =[]
  for (i = 0; i < numbers.length; i++) {
    if (iLoveThree[i] % 3 === 0){
      threes.push(numbers[i])
    }
  }
  return threes
}

Upvotes: 0

Views: 164

Answers (3)

g.carey
g.carey

Reputation: 710

There are a couple of problems with your example:

  • You have named your function "numbers", but then also reference a non-existing array called "numbers" inside the function
  • iLoveThree is being referenced as an array, but was never declared

Depending on the needs of your application, you might need to either get all numbers divisible by three that are between a minimum & maximum value, or you might need to pluck divisible-by-three numbers from a pre-defined array. I have included examples for both scenarios in the code below:

var isDivisibleByThree = function(num){
  return i % 3 === 0;
}

var getThrees = function (min, max) {
  // given a numeric min and max value,
  // return all numbers from a minimum to a maximum value that are divisible by three
  var threes =[];
  for (i = min; i <= max; i++) {
    if (isDivisibleByThree(i)){
      threes.push(i);
    }
  }
  return threes;
}

var getThreesFromArray = function(numbers){
  // given an array of numbers, 
  // return a subset of that array including  only numbers divisible by 3
  var threes = [];
  for (i = 0; i < numbers.length; i++) {
    if (isDivisibleByThree(numbers[i])){
      threes.push(i);
    }
  }
  return threes;
                 
}
var fromZeroTo50 = getThrees(0, 50);
var fromArray = getThreesFromArray([5, 0, 6, 17, 12, 4, 18]);

// Grab example values and display them in the HTML for demonstration purposes
document.getElementById("fromZeroTo50").innerHTML = fromZeroTo50.join(",");
document.getElementById("fromArray").innerHTML = fromArray.join(",");
<h2>Get all threes from 0 to 50: </h2>
<div id="fromZeroTo50"></div>

<h2>Get threes from a pre-defined array: </h2>
<div id="fromArray"></div>

Upvotes: 1

Budianto IP
Budianto IP

Reputation: 1297

I have fixed your problem, create a new html file and type this :

<!doctype html>
<HTML>

    <BODY>
        <SCRIPT>
            (function(){
                var numbers = function (iLoveThree) {
                    var threes =[];
                    for (i = 0; i < iLoveThree.length; i++) {
                        if (iLoveThree[i] % 3 === 0){
                            threes.push(iLoveThree[i]);
                        }
                      }
                    return threes;
                }
                alert(numbers([1, 2, 3, 4, 5, 6]));
            })();
        </SCRIPT>
    </BODY>
</HTML>

Hope it helps :)
Explaination :
- You need to include function parameter, this parameter will be accessed inside the function (the parameter is named iLoveThree)
- You were using numbers variable, but this variable had not been declared before, and I fixed this by changing from numbers to iLoveThree
- You missed several ; (semicolon), it's simple but will cause you a lot of trouble

PS : Thanks to RobG for reminding me about giving explaination.

Upvotes: 2

Scott Schwalbe
Scott Schwalbe

Reputation: 430

I think it would be simpler just to use a filter on numbers

var threes = numbers.filter(function(number) {
  return number % 3 === 0;
});

Upvotes: 1

Related Questions