Cannot array.push(i)

Once again I am having trouble with my for loop…

function something(str) {
  var splitter = str.split(" ");
  var Array = [];
  for (var i = 0; i < splitter.lenght; i++) {
  return Array;

something("What are you doing");

This only returns [] , but when I set the for loop like this (var i = 0; i < 5; i++) and leave the rest the same… i get [0,1,2,3,4] or if i set the for loop like (var i = 0; i < 10; i++) … i get [0,1,2,3,4,5,6,7,8,9] and so on…
Is there a specific reason why I cannot array.push(i) when i < splitter.length ?
my splitter variable is just something that i am using to split a sentence,which is basically a parameter for the something function.

typo in lenght…

@moT01 has already replied about the typo, and that should get your code working.

I wanted to comment about the logic of the code inside your function. It seems that you are trying to return an array with the different words of the String that is passed(str).

But from my understanding of split , that is exaclty what that built in function does , so you would not need your for loop to put the values in a seperate array, but just return result of the split(splitter).

With the for loop you are just copying the reult from one array to another.

> function something(str) {
>   return splitter = str.split(" ");
> }
> something("What are you doing");

Inder23 is right. A further note, you can simplify that as there’s no need to create a temporary locally-scoped variable, especially since that code (without a let/var declaration) will add it to the global (window) scope. Most JS compilers will probably optimise that out, but it’s better to just avoid that by doing this:

function something(str){
    return str.split(" ");

However, I’d go as far as to say this function is unnecessary, since you can just as easily use the str.split(" ") wherever you need the ‘something’ function. It’s clearer what your intention is, but that’s totally down to your own preference. :slight_smile:

splitter.lenght; change by splitter.length;

In addition, don’t under any circumstances call a variable Array unless your aim is to actually overwrite that bit of JavaScript. In this case, the effect is confined to the scope of the function, so won’t leak and break all the JS, but what you’re doing is overwriting JavScript’s Array builtin. Anything that depends on that (Array and the Array... static methods) will just break:

var Array = [];
// => undefined
// => 1
// => [ 1 ]

// -------------------------------

// This _should_ create an array of length 2:
// TypeError: Array is not a function

// This _should_ make an array like [1,2,3]
// TypeError: Array.from is not a function

// This _should_  be true:
// TypeError: Array.isArray is not a function

// This _should_ create an  array like [7]
// TypeError: Array.of is not a function 

If you’re really going to call an array array, then call it array - lowercase first letter. Capitalised names are for constructor functions (classes). Preferably actually call the variable a name that describes what it is, not just the generic name for that kind of collection. It is a collection of characters or letters, so call it characters or letters or whatever. Capitalised names are by, convention, for class constructors. JS will happily let you overwrite bits of the language (var undefined = 'Hello' used to be a good one).


Thanks for all the support. I realized that I made a typo and I have rewritten the code… I didnt really think about the Array part when I called my array an Array. Thanks again !