简体   繁体   中英

Javascript variable stays undefined

I have a problem with this script, something is going wrong. Rnumer stays undefined.This script should return and write all uneven digits from the random number list. Can someone tell me what I do wrong. Thanks in advance

var Rnumber = new Array();

for (i = 0; i<= 100;i++)
{
    Rnumber[i] = Math.ceil(Math.random()*101);
//  document.write(Rnumber[i] + "<br/>");
}

function unevenAndDivisible(Rnumber)
{
    var remainder = new Array();    

    for (i = 0; i<= 100; i++)
    {
        remainder = parseInt(Rnumber[i])%2;
    }
    return remainder;
}

document.write(unevenAndDivisible());

Changed to

var Rnumber = new Array();

for (i = 0; i<= 100;i++)
{
    Rnumber[i] = Math.ceil(Math.random()*101);
//  document.write(Rnumber[i] + "<br/>");
}

function unevenAndDivisible(Rnumber)
{
    var remainder = new Array();

    for (i = 0; i<= 100; i++)
    {
        remainder[i] = Rnumber[i]%2;
    }
    return remainder;
}
document.write(unevenAndDivisible(Rnumber));

but now i get the result : 0,1,0,0,1,0,0,0,1,1,0,0,1,0,1,1,1....

I simply want maybe I asked it wrong the first time, to write al uneven numbers from the random list of Rnumbers Then I need to divide that through 7 and return that.

EDIT

Allmost all problems are clear , thanks everyone for that. Their is still one question left: In this code below it only take the first uneven value from remainder and I want that it takes all values that are uneven to the next if statement to check %7. Maybe you see the problem better if you run it for youreself

var Rnumber = new Array();

for (i = 0; i<= 100;i++)
{
    Rnumber[i] = Math.ceil(Math.random()*101);
}

function unevenAndDivisible()
{
    var remainder = [];
    var answer = [];
    for (i = 0; i<= 100; i++)
    {
        if (Rnumber[i]%2 !== 0)
        {
            remainder.push(Rnumber[i]); 

            for (c = 0; c <= remainder.length;c++)
            {
                if (remainder[c]%7 == 0)
                {
                    answer.push(remainder[c]);
                }
            }           
        }
    }
    return answer;
}

answer = unevenAndDivisible();
document.write(answer);

Problem solved , Thanks everyone

You don't need to pass Rnumber to the function, as it's already available in scope:

function unevenAndDivisible()
    {
        var remainder = [];

        for (i = 0; i<= 100; i++)
        {
            if (Rnumber[i]%2 !== 0) {
                remainder.push(Rnumber[i]);
            }

        }
        return remainder;
    }

remainder = unevenAndDivisible();
console.log(remainder);

JS Fiddle demo .


Edited in response to question from OP (in comments to question , above):

...can someone explain what this mean: var remainder = [];

Sure, it's array-literal notation, which is equal to: var remainder = new Array(); , it's just a little more concise, and I prefer to save myself the typing. I get the impression, from JS Lint , whenever I use var x = new Array(); therein that the above version is generally preferred (since it complains otherwise), but I don't know why.

Either pass Rnumber to the function unevenAndDivisible or omit it from the argument list. Since it is an argument, it has more local scope than the initial declaration of Rnumber.

1 - you is not defining the Rnumber value that's function argument. 2 - in loop, you're defining remainder to divised value of ranumber and is not saving in array; try:

change:

remainder = parseInt(Rnumber[i])%2;

to

remainder[i] = parseInt(Rnumber[i])%2;  

Your problem is the line

function unevenAndDivisible(Rnumber)

You are passing in Rnumber in as an argument, but when you call unevenAndDivisible() you are not passing it it. Consequently for the body of the function Rnumber is undefined (cause you passed nothing in)

The following snippet is equivalent to what you wrote nad might explain better

function unevenAndDivisible(xyz)
{
    var remainder = new Array();    

    for (i = 0; i<= 100; i++)
    {
        remainder = parseInt(xyz[i])%2;
    }
    return remainder;
}

then called as

unevenAndDivisible(undefined)

to fix it remove the argument from the call definition

ie define it as

function unevenAndDivisible()
var array = [],
    i = 100;

while (i--) {
    var random = Math.random()*i|0;
    if(random % 2)
       array.unshift(random);
}

alert(array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM