Why won't this if statement work?

This if statement always returns 100*userQty even if the length is 16
template.order.helpers({ total() { let total = 0; const len = userLength.get(); if (len === 16) { total = 250 * userQty.get(); } else { total = 100 * userQty.get(); } return total; }, });

While this works
template.order.helpers({ total() { let total = 0; if (userClass === "Premium") { total = 250 * userQty.get(); } else { total = 100 * userQty.get(); } return total; }, });

The events

Template.hairSingle.events({ 'change #select-length': (event) => { userLength.set($(event.target).val()); console.log(userLength.get()); }, 'change #select-class': (event, templateInstance) => { userClass.set(templateInstance.find('input:radio[name=class]:checked').value); console.log(userClass.get()); }, 'blur #select-qty': (event) => { userQty.set(event.target.value); console.log(userQty.get()); }, 'keypress #select-qty': (event) => { userQty.set(event.target.value); console.log(userQty.get()); }, });

and at the very very top I declared the reactive vars

const userLength = new ReactiveVar(); const userClass = new ReactiveVar(); const userQty = new ReactiveVar();

The console.log gives back the correct values every time.

This is an idea of what I want to achieve in the end though it doesn’t work

if (userClass.get() === 'Premium') { switch (userLength.get()) { case 10: total = 77.50 * userQty.get(); break; case 12: total = 87.50 * userQty.get(); break; case 14: total = 93.60 * userQty.get(); break; ...

Also, is there a better way to bind the user input when they start typing, like two way data binding. This is a little messy I guess.
Thanks

It looks possible like the issue is that userLength contains string values instead of numeric values. Have you tried using == 16 (double equality signs) instead of ===? Or storing the value as numeric values in the event, for example something like userLength.set(parseInt($(event.target).val())).

Hope that helps.

1 Like

Both solutions work, going with the second one.
But ===‘16’ didn’t work, and from the console it shows they are integers
That’s an array of the class, colors and lengths.
Thanks a lot

No problem, glad I was able to help :slight_smile: