0

I am have trouble accessing the random index that is being chosen through randomTest. When I call randomTest in analytics_test(), I receive an undefined error. I feel as if I am not actually accessing this variable. Any tips or ideas to fix this problem? I would appreciate it, thank you!

//This function creates a loop for 5 random numbers. Each index in the array is given a variable
function createTest_Var() {
    var testNumber = [];
    var i; 
    for (i = 0; i < 5; i++) {
        testNumber[i] = (Math.random() * 10);
   }
 var randomTest = testNumber[Math.floor(Math.random() * testNumber[i].length)]; 
return testNumber;
}
//This function takes chooses a random index from the array and compares it to the random number "y".
function analytics_test() {
    var y = (Math.random() * 10);
    var i = createTest_Var.randomTest;
        if (y < i) {
         //Just a test console.log ("the random numbers are: " + (Math.random() * 10));
            console.log ("It is greater! " + i + "<" + y);
        }
        else {
            console.log("not big enough " + i + ">" + y);
        }
}
5
  • 1
    var i = createTest_Var.randomTest; --- what does this statement do? And why do you have 2 returns in createTest_Var? Commented Jun 13, 2016 at 21:55
  • I set i equal to the "random index" that it is suppose to access in createTest_Var(). I wanted to try to compare that random number to Y in the function analytics_test(). Also, I have two return statements because I was just testing around my code, I'll remove the second one. Commented Jun 13, 2016 at 21:58
  • 1
    I don't mean to be mean but I think you need to work through a couple of JavaScript tutorials. Having a var statement after a return basically does nothing. It certainly doesn't add a property to a function that you can access outside of the function. Commented Jun 13, 2016 at 22:09
  • 1
    How come randomTest is assigned after a return statement? Can you provide a code sample on JSFiddle\JSBin? Commented Jun 13, 2016 at 22:10
  • @NeilDCruz that was an accident on my end. Commented Jun 13, 2016 at 22:13

1 Answer 1

1

Several issues:

  • randomTest never gets its value, because you exit the function before reaching that code. But even if you would move the return statement after it, it would not be accessible from outside the function body.
  • you never execute the function createTest_Var. The code createTest_Var.randomTest tries to access an unexisting property of the function object. To call the function you should write createTest_Var()
  • Even if you would call it you are still not actually picking a random element from the generated array because of the first issue. For this you could write a separate function, and call it like this: pickFromArray(createTest_Var())
  • testNumber[i].length is not correct: you want the length of the array, not its i-th element, so write testNumber.length.

Here is the code corrected:

//This function creates a loop for 5 random numbers. Each element in the array is given a value
function createTest_Var() {
  var testNumber = [];
  var i; 
  for (i = 0; i < 5; i++) {
    testNumber[i] = (Math.random() * 10);
  }
  return testNumber;
}

// Function to pick random element from array
function pickFromArray(testNumber) {
  return testNumber[Math.floor(Math.random() * testNumber.length)]; 
}

//This function takes chooses a random element from the array and compares it to the random number "y".
function analytics_test() {
  var y = (Math.random() * 10);
  var i = pickFromArray(createTest_Var());
  if (y < i) {
    //Just a test console.log ("the random numbers are: " + (Math.random() * 10));
    console.log ("It is greater! " + i + "<" + y);
  }
  else {
    console.log("not big enough " + i + ">" + y);
  }
}

analytics_test();

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

1 Comment

thank you so much! that was a stupid mistake on my - exiting the function before declaring randomTest. That truly helped a low, just with the overall understanding of what I was doing wrong. Thanks again.

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.