splice - for loop ignoring word length of 1 -


i'm working on question:

write function filterlongwords() takes array of words , integer , returns array of words longer i.

i have down, except reason, if there word 1 character long, not deleted. know i'm doing wrong? thank you.

function filterlongwords(i, array){ (var x = 0; x<array.length; x++){     if (array[x].length <= i){         array.splice(x,x);     } } console.log(array) }  var wordarray = ["i", "am", "longer", "than", "one"];  filterlongwords(2, wordarray); 

there couple problems here.

first, arguments splice (index, number of items), you'll want call array.splice(x,1)

second, when remove item array, array shortened x still advance (effectively skipping next item). can offset x--; after removing item array.

function filterlongwords(i, array){ (var x = 0; x<array.length; x++){     if (array[x].length <= i){         array.splice(x,1);         x--;     } } console.log(array) } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -