0

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
}
6
  • are those <br> tags part of your code or is that a formatting issue? Commented Feb 9, 2016 at 0:29
  • 3
    Where do you have your numbers array? Commented Feb 9, 2016 at 0:31
  • 1
    Looks like they are defining numbers as a function and treating it like an array inside that function. Commented Feb 9, 2016 at 0:32
  • 2
    Should the for said: for (i = 0; i < iLoveThree.length; i++) instead of numbers.length?. Just guessing because of the if (iLoveThree[i] % 3 === 0) Commented Feb 9, 2016 at 0:32
  • 2
    Looks like a simple error. You are bounding i to numbers.length, yet you are looking in the array iLoveThree, not numbers'. Commented Feb 9, 2016 at 0:32

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It's the small things I always over look that trip me up.
1

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

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

2 Comments

Rolled-back my edit in case the prototype editing bothered some developers (to be be honest it is not universally regarded as a good idea), here is a JSFiddle explaining the code jsfiddle.net/ekkk4e53
That works too, I'm always scared of extending native object prototypes doe
1

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>

1 Comment

@RobG- you are absolutely right:) Updated with vanilla JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.