0

i am trying to iterate through a string and remove all of the spaces. The js console is telling me this about line 1: Uncaught SyntaxError: Unexpected end of input.

userinput is connected to an input tag with the id "number"

any ideas would be appreciated.

userinput = $('input#number').val();


function thing(x) {
    for (i = 0; i <= x.length(); i++) {
        if (x.charAt(i) === (" ")) {
            return x.indexOf(i).replace("")
        }
    }
    $(document).ready(function() {
        $('form#factorial').submit(function(event) {

            console.log(thing(userinput));
            event.preventDefault();

        });
    });

}
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
arfarf
  • 11
  • 1
  • 5

1 Answers1

0

This code is a lot shorter and more efficient to remove the spaces.

var str = 'this is my sentence';
str = str.split(' ').join(''); //str === 'thisismysentence';

If you do do it the other way, it should be

for (i = 0; i <= x.length; i++) {

not

for (i = 0; i <= x.length(); i++) {

You're also missing a } on your function.

Tyler McGinnis
  • 34,836
  • 16
  • 72
  • 77