-2

I'm trying to get the values out of an array. If the value in the array equals false, it will redirect the page to a new window. However, the dialogBehavior() function will not work. Does anyone know what the problem is? Is there an error in the syntax?

var byLawsToken = false;
var meetingSummaryToken = false;
var directoryToken = false;

var tokenArray = new Array(3);
tokenArray[0] = byLawsToken;
tokenArray[1] = meetingSummaryToken;
tokenArray[2] = directoryToken;


var windowLocation = new Array(3);
windowLocation[0] = "bylaws.html";
windowLocation[1] = "index.html";
windowLocation[2] = "about.html";

function dialogBehavior()
{
    var hashedPassword = "6909b1c8fa84hd98b1f25aa1a4bccc23";
    var hashedInput = CryptoJS.MD5($("#passwordTXT").val()).toString();

    if(hashedInput == hashedPassword)
    {

       // The part I'm having trouble with 
       for(var i = 0; i < tokenArray.length; i++)
       {
           if(tokenArray[i].get() == true)
           {
               window.location.href = windowLocation[i];
               break;
           }
       }
    }

    else
    {
        $("#error").show();
        $("#passwordTXT").val("")
    }

}

function throwByLawsToken()
{
    byLawsToken = true;

}

function throwMeetingSummaryToken()
{
    meetingSummaryToken = true;

}

function throwDirectoryToken()
{
    directoryToken = true;
}

function resetTokens()
{
    var byLawsToken = false;
    var meetingSummaryToken = false;
    var directoryToken = false;
}


function hideDialog()
{
    $("#error").hide();
    $("#passwordTXT").val("")

}

function writeDate()
{
    document.write(new Date().getFullYear());
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
o_in25
  • 136
  • 1
  • 9
  • 1
    "_the dialogBehavior() function will not work_" Please elaborate. What does it actually do? What did you do in order to try and fix it? – takendarkk Aug 15 '15 at 03:27
  • Always use triple equals `===` when doing comparisons in JavaScript. – dnavas77 Aug 15 '15 at 03:29
  • 2
    You also say "_If the value in the array equals false, it will redirect the page to a new window_" but then your code says the opposite. `if(tokenArray[i].get() == true) { window.location.href = windowLocation[i]; }` – takendarkk Aug 15 '15 at 03:30
  • 1
    @navasd That is just simply wrong. If you were supposed to only use the === operator, why would the others exist at all? – takendarkk Aug 15 '15 at 03:31
  • The function dialogBehavior( ) will redirect a person to a new webpage, based on which link they clicked. However, the function will not redirect the person to the new page. The if statement in the loop will not execute. – o_in25 Aug 15 '15 at 03:32
  • @Takendarkk. do your research and understand why in JS we always use === (strict comparison operator). It helps maintain data integrity and makes your code easier to test. – dnavas77 Aug 15 '15 at 03:36
  • @navasd Only if you are guaranteed to not need any sort of type conversion anywhere in the lifecycle of the script. [Here](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) is where you can do _your_ research. – takendarkk Aug 15 '15 at 03:47
  • @takendarkk: if you are using == , you are assuming implicit type conversion which is a bad practice in JS. – dnavas77 Aug 15 '15 at 04:05
  • @navasd Yes, but dealing with code that doesn't follow all best practices is extremely common when you actually work in the industry. If your advice to people is "Go replace all the == with === in all the js you see" you are creating even more problems than you are helping. – takendarkk Aug 15 '15 at 13:55

2 Answers2

2

It looks like you're trying to put references to the tokens in your tokenArray, but your code is just populating the array with the initial values of the tokens.

var someVariable = false;
tokenArray[0] = someVariable; // tokenArray[0] === false
someVariable = true; // tokenArray[0] still === false

There is no long-lived association between your token variables and tokenArray.

You could do something like this:

var byLawsToken = {val: false};
var meetingSummaryToken = {val: false};
var directoryToken = {val: false};

and then

if(tokenArray[i].val) // no need to do an equality check with true

Then change the throws functions to modify tokenVariable.val instead of tokenVariable.

You've also got a problem in resetTokens where you're trying to reset your global token variables, but you're declaring local variables using var. You can just set them directly, no var; otherwise the global variables will remain untouched.

Also, there are probably more elegant ways to do what you're looking to do, if you will give us more details on what you're trying to accomplish, big-picture-wise.

Ozan Bellik
  • 483
  • 2
  • 6
  • Sure thing. If I answered your question, you can accept and upvote my answer. http://stackoverflow.com/help/someone-answers – Ozan Bellik Aug 15 '15 at 05:20
0

I think the problem here:

if(tokenArray[i].get() == true)

.get() Function Not from JavaScript core functions .. So may be it's the reason of your problem.

Mohamed Saeed
  • 381
  • 2
  • 7