Comparison with the if

Asked

Viewed 115 times

0

Personal what is wrong in the code, I made a function that calls a css if a value is greater than 5 , the higher value works but the lower that would be 3 where would appear other css does not work follows the code

SetInterval(function () {
roundedValue = Math.round( parseFloat( sensorValor ) * 100) / 100;
// roundedValue = math.round(parseFloat($('#testValue').val()( * 100 / 100;
if ((roundedValue >= 5) $$ testEnable ) {
$('#pag-1').hide();
$('#pag-2').show();
testEnable = false;
consolo.Log('teste ok!')
}

if ((roundedValue <= 3) $$ testEnable ) {
$('#pag-1').hide();
$('#pag-4').show();
testEnable = false;
consolo.Log('teste ok!')
}

there’s something wrong with the comparative?

  • 1

    $$ would not be &&? Read more on logic operated

  • In addition to the error observed by @Marconi, the function closing key is also missing. Put HTML code also so we can have more background to help fix the problem.

  • Your code is missing }); at the end of the line and exchange $$ for &&.

3 answers

1

I believe you should replace your code with this:

SetInterval(function() {
  roundedValue = Math.round(parseFloat(sensorValor) * 100) / 100;
  // roundedValue = math.round(parseFloat($('#testValue').val()( * 100 / 100;
  if (roundedValue >= 5 && testEnable) {
    $('#pag-1').hide();
    $('#pag-2').show();
    testEnable = false;
    console.Log('teste ok!')
  }

  if (roundedValue <= 3 && testEnable) {
    $('#pag-1').hide();
    $('#pag-4').show();
    testEnable = false;
    console.Log('teste ok!')
  }
});

Young hug.

1


I believe the code would be correct as follows:

SetInterval(function () {
    roundedValue = Math.round( parseFloat( sensorValor ) * 100) / 100;
    // roundedValue = math.round(parseFloat($('#testValue').val()( * 100 / 100);
    if (roundedValue >= 5 && testEnable ) {
        $('#pag-1').hide();
        $('#pag-2').show();
        testEnable = false;
        console.log('teste ok!')
    }

    else if (roundedValue <= 3 && testEnable ) {
        $('#pag-1').hide();
        $('#pag-4').show();
        testEnable = false;
        console.log('teste ok!')
    }
});
  • 1

    Fight buddy, it worked perfect now

1

It’s hard to know what you want to do with this code, but I believe the code below can help you.

(function () {
  var value = 4;
  var pag1 = document.getElementById("pag-1");
  var pag2 = document.getElementById("pag-2");
  var pag4 = document.getElementById("pag-4");
  
  var onChange = function (oldValue, newValue) {
    pag1.classList.toggle("hide", newValue != 4);
    pag2.classList.toggle("hide", newValue <= 4);
    pag4.classList.toggle("hide", newValue >= 4);
  };
  Object.defineProperty(window, "sensorValor", {
    get: function () { return value; },
    set: function (newValue) { 
      if (value != newValue) {
        onChange(value, newValue);
        value = newValue; 
      }
    }
  });
})();

var sensor = document.getElementById("sensor");
sensor.addEventListener("input", function (event) {
  sensorValor = parseInt(sensor.value);
});
.hide {
  display: none;
}
<input id="sensor" type="text" value="4" />

<div id="pag-1">Pagina 1</div>
<div id="pag-2" class="hide">Pagina 2</div>
<div id="pag-4" class="hide">Pagina 4</div>

In the example above there is a monitoring of the changes of the variable sensorValor, then every life that it has its value changed, a function will be sensitized.

the input#sensor with the event input only serves to demonstrate the behavior after updating the value of the sensorValor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.