0

var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF'];
  var i;
  for (var i = 0; i < ccode.length; i++) {
     var ccode[i] = fx.convert(amount, {to: 'ccode[i]'});
  }

The bit above var ccode[i] is causing an error

SyntaxError: Unexpected token '['. Expected ';' after variable declaration.

I am still new to JS so please bear with me.

I am editing my question here since people asked why I was re-declaring var ccode[i] and its because I need to output this:

var EUR = fx.convert(amount, {to: "EUR"});
4
  • 1
    why are you declaring ccode again? Commented Nov 24, 2017 at 10:23
  • for (var i = 0; re declares i again. Remove the var, yes and same for ccode Commented Nov 24, 2017 at 10:24
  • what does var do in Javascript Commented Nov 24, 2017 at 10:26
  • I had var ccode[i] because I need to output this "var EUR = fx.convert(amount, {to: "EUR"});" Commented Nov 24, 2017 at 10:33

3 Answers 3

1
var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF'];
var i;
  for (i = 0; i < ccode.length; i++) {
     ccode[i] = fx.convert(amount, {to: 'ccode[i]'});
  }

ccode is already declared.. no need to use var

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

1 Comment

same for goes for i
0

Its not about declaring ccode twice, as variable declaration syntax is wrong

var ccode[i]; should not be array, see for reference

You should remove var.

var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF'];
  var i;
  for (var i = 0; i < ccode.length; i++) {
     ccode[i] = fx.convert(amount, {to: 'ccode[i]'});
  }

Comments

0

I would suggest using an array map:

var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF'];
ccode = ccode.map(function(code) {
  return fx.convert(amount, {to: code})
})

1 Comment

map returns a new array, but you do not assign this result.

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.