1

I am currently working on a challenge to convert any amount of arbitrary numbers entered into the function below as a currency (expressed by a string where every three characters are separated by a comma). All was okay until I realized that if the length of the number entered was 4, then the comma would need to be placed as 1,234 rather than 123,4. It seems the function has spiralled out of control a little when I wrapped the initial if statement around my for loop. (1) I keep getting thrown an 'Uncaught SyntaxError: Unexpected token else' in the console. (2) I thought it may be due trying to place an if/else around the for/if/else. Any light shed on my error would be much appreciated. Charlie

function toCurrency(price){


      var newStrng = price.toString().split("");

      var stringCh = [];

      if(newStrng.length===4){ 

          console.log("gotcha");
          stringCh = newStrng.splice(1,0,",");
          return stringCh;

        } else {

        for(var i = 0; i < newStrng.length; i++) {
          if(i %3 === 0 && i !== 0){

              stringCh.push(",");
              stringCh.push(newStrng[i]);

          }  
              } else {

                  stringCh.push(newStrng[i]);
              } 
        }

        var finallyDone = stringCh.join("");
        return finallyDone;

    }//EO function
1
  • 2
    Well, there is no for { } else { } statement in js :-) Always indent your code properly. Commented Aug 20, 2015 at 22:10

3 Answers 3

1

The if statement in the following block

for(var i = 0; i < newStrng.length; i++) {
      if(i %3 === 0 && i !== 0){

          stringCh.push(",");
          stringCh.push(newStrng[i]);

      }  
          } else {

              stringCh.push(newStrng[i]);
          } 
    }

has an extra }, right before the else. Take it out and you should no long receive the syntax error.

You can make it easier to spot mistakes like this in the future by ensuring your code is properly indented so you can see which brackets are associated with which blocks. Though I appreciate it could be reasonably indented in the source code and may not have copied across to SO perfectly.

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

Comments

1

There is a } in the wrong place inside the for loop, that need to be moved after the else block. As it is, the if statement is located in the for loop, but the else block is located outside of the loop, which is not valid syntax.

function toCurrency(price){
    var newStrng = price.toString().split("");
    var stringCh = [];

    if(newStrng.length===4){ 
        console.log("gotcha");
        stringCh = newStrng.splice(1,0,",");
        return stringCh;
    } else {
        for(var i = 0; i < newStrng.length; i++) {
            if(i %3 === 0 && i !== 0){
                stringCh.push(",");
                stringCh.push(newStrng[i]);
            } else {
                stringCh.push(newStrng[i]);
            }
        } 
    }

    var finallyDone = stringCh.join("");
    return finallyDone;

}//EO function

Comments

0

remove } after,

stringCh.push(newStrng[i]);

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.