michaelto20
michaelto20

Reputation: 73

Trouble pushing to an array in JS

Below is just a section of my code but I know it's problematic because I can't get it to return any value except 'undefined'. I have been over this for hours and cannot figure it out.

I want to be able to input a number and have its factors pushed to an array. I have tested it by alerting the first item in the array and I get nothing. I'm sure this is a pretty easy but I just can't figure it out. Here is the code:

    var numberInQuestion = prompt("Of what number are you wanting to find the largest        prime factor?");

    //determine factors and push to array for later use
    var factorsArray = [];
    function factors(numberInQuestion){
        for(var i = 2; i < numberInQuestion-1; i++){
            if(numberInQuestion % i === 0){
                return factorsArray.push[i];
            } else {
                continue;
            }
        }
    };
    factors(numberInQuestion);
    alert(factorsArray[0]);

Thanks for any help!

Upvotes: 1

Views: 154

Answers (4)

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

The function should not return when pushing to the array. Return the array after executing the loop. The else clause is also unnecessary.

var numberInQuestion = prompt("Of what number are you wanting to find the largest        prime factor?");

function factors(numberInQuestion){
    var factorsArray = [];
    for(var i = 2; i < numberInQuestion-1; i++){
        if(numberInQuestion % i === 0 && isPrime(i)){
            factorsArray.push(i);
        }
    }
    return factorsArray; 
};
var factors = factors(numberInQuestion);
alert(factors[factors.length-1]);

//From: http://stackoverflow.com/questions/11966520/how-to-find-prime-numbers
function isPrime (n)
{
    if (n < 2) return false;
    var q = Math.sqrt (n);

    for (var i = 2; i <= q; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }

    return true;
}

Given the purpose of the example two items must be considered

The code does not determine if the number is actually prime. The code will return the smallest factor possible since the loop starts at two and increments, then returns the first element in the array. The largest factor would actually be the last element in the array. I have corrected the example to find the greatest prime factor. You can test it via this fiddle: http://jsfiddle.net/whKGB/1/

Upvotes: 0

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

You have an error in your pushing syntax. Correct syntax for pushing is -

factorsArray.push(i);

Also returning immediately from the function after finding the first divisor will not give you the full list. You probably want to return after you've found out all the divisors.

Taking all of the above into consideration, you should rewrite your function as follow -

function factors(numberInQuestion){

    for(var i = 2; i < numberInQuestion - 1; i++){
        if(numberInQuestion % i === 0) {
            factorsArray.push(i);
        }
    }
}

and you will be OK.

Upvotes: 1

tckmn
tckmn

Reputation: 59283

  • you can only return one value
  • you must use (), not [] for calling push
  • factorsArray should be local to factors (put the definition inside the function)
  • the else { continue; } is useless

Here is the fully corrected code:

var numberInQuestion = prompt("Of what number are you wanting to find the factors of?");

//determine factors
function factors(numberInQuestion){
    var factorsArray = []; // make it local
    for (var i = 2; i < numberInQuestion-1; i++){
        if(numberInQuestion % i === 0){
            factorsArray.push(i); // use (), and don't return here
        } // no need for else { continue; } because it's a loop anyway
    }
    return factorsArray; // return at the end
};
var result = factors(numberInQuestion); // assign the result to a variable
alert(result);

Here's a JSFiddle.

Upvotes: 2

Pointy
Pointy

Reputation: 413717

You've coded this so that when you find the first factor your function returns immediately. Just get rid of the return keyword in that statement. (What "return" means in JavaScript and other similar languages is to immediately exit the function and resume from where the function was called.)

Oh, also, you call functions (like .push()) with parentheses, not square brackets.

Upvotes: 0

Related Questions