2

this is my first javascript project and I'm having trouble passing a variable into a function.

Here is the relevant section from my "global variables"

var timesran = []; for (var x= 0; x<38; x++){ timesran[x] = 0; } 

Below is the first function that is trying to pass x into the function so that I can have the results stored in different arrays

function happytimes(){ for (var x= 0; x < 38; x++){ switch (x){ case 0: if (shouldiFlip[x]){ randomizer(x); //input that we want to feed into the function x++; } } 

(please note: I have stripped the rest of the case1-38 from the excerpt for clarity, all the other cases look the same, and have the same output - the rando function is working in each different function of the randomizer function though as it is getting new output)

Following is the randomizer function

function randomizer(a){ if (startrunning){ var rando = []; rando = Math.floor(Math.random()*4+1); timestorun[a] = rando[a]; pos[a] = 0; console.log("hi there you are in new run now"+pos[a]+rando+timestorun[a]); } else{ pos[a] = pos[a] + 1; if (pos[a] >156){ pos[a] = 0; } if (masterlet[pos[a]] == letter[a]){ timesran[a] = timesran[a] +1; if (timesran[a] == timestorun[a]){ console.log("ELSE THING"+pos[a]+rando+timestorun[a]); shouldiFlip[a] = 0; } } } 

The output of the first console log here is

hi there you are in new run now03undefined jquery.solari.letters.js:386 

As you can see, timesran[] is coming back undefined. This makes me sad.

Am I handling this correctly? I've been working on this for about 7 hours perfecting the code and this is my last hangup. Thank you for your help!!!

8
  • I have heard of the legend of "return" but I am unfamiliar with how it would help me in this case. Commented Apr 1, 2013 at 8:43
  • Meh, I think I caught it. Commented Apr 1, 2013 at 8:46
  • 1
    Do you really have 38 case blocks in the switch with the exact same content? Why on earth? Commented Apr 1, 2013 at 8:46
  • 2
    I can't really follow your code but it seems to basically depend on global variables for everything. That makes it all way more difficult than it should. Can't you just make function accept arguments and return results? Commented Apr 1, 2013 at 8:48
  • @ÁlvaroG.Vicario I would love to do this but I have not had to do this before- I'll google it up. Commented Apr 1, 2013 at 8:53

2 Answers 2

2

This is not working because you are setting the array of rando as a variable of Math.floor(Math.random()*4+1); you should update it rando[a] = Math.floor(Math.random()*4+1);

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

5 Comments

the rando variable is local to the if block, and thus discarded afterward, so although strictly correct, there's likely no use in treating it like a (single-value) array, instead of just using a number.
Yes, this was definitely the issue!
he is trying to get the x into rando so that each individual item will have a different random rather then all of them being the same.
@Keleko hmm... i can't figure this one out... the way the code is written, isn't the rando variable reinitialized with an empty array each time the randomizer method is run?
@Grim Actually it sounds like you have a decent idea here, I could just drop rando as an array and load it in each time since that is all I am doing anyway
1

You are creating the variable rando as an array, then overriding it with a number - the result of the random extraction - discarding the old array value at the same time, and then index it like an array. Best way is to simply set:

timestorun[a] = rando; 

instead of

timestorun[a] = rando[a]; 

and assign the variable directly to the number - instead of:

var rando = []; rando = Math.floor(Math.random()*4+1); 

use only

var rando = Math.floor(Math.random()*4+1); 

4 Comments

I do need to have 38 different randoms to achieve the effect I am looking for. This controls 38 different characters to make them come to a stop a different times.
isn't that the purpose of your timestorun variable, as opposed to the rando one?
I think what I'm trying to do is to load rando into times to run so it will hold the 38 different random numbers and then it will count against the timesran to know how many times to flip before it stops
Exactly what i thought - in this case you don't need to create an array for rando since it's timestorun that is keeping track of all the numbers. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.