lundi 1 février 2016

JS If Else statement not matching using == operator

I am trying use an if/else if statement and am somehow failing. Here is a snippet of my code:

  if(type == 1){
     console.log("-1-");
     } 
  else if(type == 2){
       console.log("-2-")
     }
  else {
    console.log("NO MATCH");
  }

I was trying to use something like

  if(type == "one"){ 
     console.log("-1-");
     } 
  else if(type == "two"){ 
       console.log("-2-")
     }
  else {
    console.log("NO MATCH");
  }

but that was failing as well. I think I may have some bigger issue, here is my entire JS file:

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.time);
  var typeee = JSON.stringify(this.state.type);

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

  this.render()
}

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

  testDay(typee, timee);
}

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

var myTest = new Test()

I have been tinkering with this for a while and reviewed examples but am convinced that the issue must be small and I am somehow blind to it. Here is a CodePen with the HTML as well.

Aucun commentaire:

Enregistrer un commentaire