-1

Normally My code is working fine. but, I get Syntax error. what is mistake in syntax here?

enter image description here

$(document).ready(function() {
$('.myClass').change(function() {
        var ids = ['first', 'second'];
        var totalCount = ids.reduce((prev, id) => parseInt($(`#${id}-passenger`).val()) + prev, 0);
        var mc = $('input[name="myClass"]:checked  + label').text();
        $('#myTotal').val(totalCount + ' - '+mc);
    });
});
4
  • 1
    You have to add ' not ` Commented Jun 14, 2018 at 12:07
  • Your code is fine. Most likely the issue is that the linter used in your IDE is outdated so doesn't recognised ES6 standards such as template literals and arrow functions Commented Jun 14, 2018 at 12:08
  • @dekts No, backticks are used to delimit template literals and are valid: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 14, 2018 at 12:08
  • @dekts I have replace it. but, thought, error is still. Thanks. Commented Jun 14, 2018 at 12:11

2 Answers 2

4

Maybe you have a Javascript version issue and you should try without templating ?

var totalCount = ids.reduce(function (prev, id) {
    return parseInt($('#' + id + '-passenger').val()) + prev
}, 0);

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

It's done for the arrows :) But I think my brackets are fine ?
You're right - my bad. I read the syntax incorrectly in the OP
1

Your code is entirely valid and works fine. Therefore I would surmise that this issue is simply that the JS linter in the IDE you're using is outdated and doesn't support ES6. I'd suggest using a more up to date linter, assuming the IDE lets you change it, or even a better IDE entirely.

If you want to avoid the issue you would need to remove the template literals and arrow functions, like this:

$('.myClass').change(function() {
  var ids = ['first', 'second'];
  var totalCount = ids.reduce(function(prev, id) {
    return parseInt($('#' + id + '-passenger').val()) + prev, 10);
  }, 0);
  var mc = $('input[name="myClass"]:checked + label').text();
  $('#myTotal').val(totalCount + ' - ' + mc);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.