1

I'm sure I'm just screwing up my code somehow (still a bit of a noobie) but I'm trying to create a random number (which is working fine) then use the variable from that random number to send 1 of every 3 users to a post-course survey (which isn't working fine at all).

Is there a way to do this, and if so, what am I doing wrong?

Here's the code (this is inside of an adobe Captivate course so I'm making use of the API interface library as well, but that part is also working fine).

Currently this string of code sends the user to the listed link every time instead of depending on the variable.

var jsRandomNumber = 0

function getRandomInt(min, max) {
  var jsRandomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
  alert(jsRandomNumber);
  window.cpAPIInterface.setVariableValue('randomnumber', jsRandomNumber);
}

getRandomInt (1, 3)

function randomQuizGenerator() {
    if (var jsRandomNumber = 1) {
      window.open("http://www.w3schools.com");
      window.cpAPIInterface.next();
    } else {
      window.cpAPIInterface.next();
    }
}

randomQuizGenerator()

3 Answers3

2

Based on your code, I see three problems, two of which are a misunderstanding of variables, the third being a misunderstanding of conditionals.

First, you only need to declare your variable var jsRandomNumber the one time at the top. Then you can access it by simply using its name, jsRandomNumber. Every other time you put var in front of it, you are creating a new variable within that scope.

For the if statement, again use jsRandomNumber instead of var jsRandomNumber, but also you need to use == to compare two values. Using jsRandomNumber = 1 will set jsRandomNumber to 1, and then that value is used to determine if the if should pass or not, and in this case it always will because 1 happens to evaluate to true. jsRandomNumber == 1 will return true or false depending on the value of jsRandomNumber.

var jsRandomNumber = 0

function getRandomInt(min, max) {
  jsRandomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
  alert(jsRandomNumber);
  window.cpAPIInterface.setVariableValue('randomnumber', jsRandomNumber);
}

getRandomInt (1, 3)

function randomQuizGenerator() {
    if (jsRandomNumber == 1) {
      window.open("http://www.w3schools.com");
      window.cpAPIInterface.next();
    } else {
      window.cpAPIInterface.next();
    }
}

randomQuizGenerator()
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
  • Thanks for the help! This cleaned up my variable output. Unfortunately, this still fails to open the link in a new browser window (or at all) for some reason. I also noted this under the answer provided by @CarolynLee. I used a couple of code previewers online to double-check. The variable is still working well with this example, of course. – Isaac Stewart Aug 17 '16 at 18:34
  • Yea.. this doesn't actually fix that issue, unfortunately. There might be some useful information [here](http://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript) that can help you. Also, I agree with you that @CarolynLee's answer is cleaner, so feel free to mark that as the accepted answer if you used her code. – jonhopkins Aug 17 '16 at 18:39
  • Thanks again, I'll take a look. I'm glad I can post stupid questions in this community without being ridiculed. – Isaac Stewart Aug 17 '16 at 18:49
  • Awesome! What did it? – jonhopkins Aug 17 '16 at 18:58
  • I made sure to use the correct variable syntax per your instructions and from Carolyn Lee, in addition to the two "==" characters. It successfully redirected me in one out of every three clicks. Thanks again. – Isaac Stewart Aug 25 '16 at 01:38
  • Ok cool. I was just expecting it to be more because in one of your comments you said it wasn't redirecting at all. – jonhopkins Aug 25 '16 at 01:41
1

There are a couple of small fixes you can make that should resolve your problem.

  1. Don't declare jsRandomNumber multiple times. Each time you use var, you are re-declaring that variable, so it's value is changing throughout your script.
  2. Instead of updating a global variable, just make that jsRandomNumber variable local to the getRandomInt function. You can return it and use the return value. Local variables make more sense here because you only need to change that number in one place. If you leave the variable in the global scope, then code outside of the getRandomInt function will be able to access and change that variable. It makes it harder to debug problems.
  3. When comparing two values, use == or ===. Using = won't work, because the single equals sign is the assignment operator.

Here's an updated example:

function getRandomInt(min, max) {
  var jsRandomNumber = Math.floor(Math.random() * (max - min + 1)) + min; // only declare the variable once
  alert(jsRandomNumber);
  window.cpAPIInterface.setVariableValue('randomnumber', jsRandomNumber);
  return jsRandomNumber; // Return the variable, instead of changing a global variable
}

function randomQuizGenerator() {
    var myNumber = getRandomInt (1, 3); // using return value, not global variable
    if (myNumber === 1) { // triple equals sign for checking equality
      window.open("http://www.w3schools.com");
      window.cpAPIInterface.next();
    } else {
      window.cpAPIInterface.next();
    }
}

randomQuizGenerator()
Carolyn Conway
  • 1,356
  • 1
  • 15
  • 21
  • 1
    Thanks for the comment, lots of other great commenters responded but this appears to be the cleanest way to do it just by a hair. This still fails to open the link in a new browser window (or at all) for some reason. I used a couple of code previewers online to double-check. The variable is still working well with this example, of course. – Isaac Stewart Aug 17 '16 at 18:31
0

Firstly, there's no need to redeclare jsRandomNumber. var is mainly used to declaring a variable for the first time. Hence, you can remove the extraneous var's that happen several times.

Secondly, when comparing values inside your if statement, you need to use the == operator. A single = is the assignment operator, and will also return the value that you set. So for example, if(a=1) will always evaluate to true because its setting a equal to 1 and then returning 1. Take a look here for more information. So, you should change your if statement to:

 if(jsRandomNumber == 1) {

      ....

 }
Community
  • 1
  • 1
Ted
  • 746
  • 1
  • 7
  • 19