mardi 2 février 2016

JavaScript IF statement Operators not matching (x == 1 && y == 'Parameter')

I have been banging my head on this for a few days and am probably just blind to the issue at this point.

I am setting parameters and passing them into a function and want to make my next step by matching parameters within a series of IF/Else IF statements. Here is an example:

function Test(type, time) {
  this.state = {
    type: 1,
    time: '1H'
  }
  this.render()
}

Test.prototype.setState = function(newState) {
  for (var key in newState) {
    this.state[key] = newState[key]
  }
  var timeee = JSON.stringify(this.state.type);
  var typeee = JSON.stringify(this.state.time);

  document.getElementById('test1').innerHTML = timeee;
  document.getElementById('test2').innerHTML = typeee;

  this.render()
}

Test.prototype.render = function() {
  var type = this.state.type;
  var time = JSON.stringify(this.state.time);

  testDay(type, time);
}

function testDay(type, time) {
  console.log(">>>>> TYPE IS: " + type + " " + "TIME IS: " + time + " <<<<<")
  if(type == 1 && time == '1H'){
     console.log("-1-24-");
     } 
  else if(type == 2){
       console.log("-2-");
     }
  else {
    console.log("NO MATCH");
  }
};

var myTest = new Test()

Somehow, when I add the second parameter of 'time' it breaks the whole thing. If you have any thoughts or experience with this I'd really appreciate it since I am going a bit mad. My end goal is something like this:

  if(type == 1 && time == '1M'){ 
     //call specific function
     } 
  else if(type == 1 && time == '1H'){ 
     //call specific function
     }
  else if(type == 1 && time == '1D'){ 
     //call specific function
     }
  else if(type == 2 && time == '1M'){ 
     //call specific function
     }
  else if(type == 2 && time == '1H'){ 
     //call specific function
     }
  else if(type == 2 && time == '1D'){ 
     //call specific function
     }
  else {
    console.log("NO MATCH");
  }

Here is an example of it that you can tinker with:

http://ift.tt/1KUO1sU

Aucun commentaire:

Enregistrer un commentaire