Search code examples
javascriptvelo

if statement not running calculation on wix, am I missing something obvious?


I'm a beginner and can't figure out why my if statements aren't working. Im using wixcode and javascript. Each if statement below shows a different way I have tried to get the calculation to run. The number that is input as SR is displayed in the textbox onClick but doesn't run the calculation to manipulate the input number, Or maybe it is not running the if statements all together. Am I missing something obvious?

    $w.onReady(function () {
$w("#generatequote").onClick((event) => {

    var SR = $w("#SR").value;
    if (SR<100) {
        $w("#SR").value = SR *2
        //example one. Tried making it write the input * 2 if the input number is less than 100
    }
    else if (SR>=100&&SR<300) {
        $w("#SR").value = ("#SR")*1.5;
        //example two. Tried making it write the input * 1.5 if the input number is between 100 and 300
    }
    else if (SR>=300&&SR<600) {
        $w("#SR").value * 1.25;
        //example three. Tried making it write the input * 1.25 if the input is between 300 and 600
    }
    else if(SR>=600) {
        $w("#SR").value = ("SR");
    }

    $w("#quotetext").value =(SR)

Solution

  • value returned is in string format and your if statement is comparing with numbers because of which none of your if statements evaluates to truthy value. First convert the input value in number/integer type and then make comparison

    var SR = Number($w("#SR").value);