For loops in JavaScript functions


For loops in JavaScript functions


const animals = ["dog", "cat", "tree frog"];

function pluralize(array) {
for (var i = 0; i <= array.length; i++) {
array[i] += "s"
return (array)
}
}
console.log(pluralize(animals));



My goal is to create a function that adds the s to the end of words. However when I ran my code s was only added to the first word in the array. Why was the s not added to the other words in the array?





Because you immediately return in your loop. Move the return out of the loop to the end of the function.
– ASDFGerte
Jul 1 at 9:13


return


return





Also you should iterate to i < array.length. Otherwise you will operate on array[array.length], which is after the last element.
– ASDFGerte
Jul 1 at 9:20



i < array.length


array[array.length]




6 Answers
6




var animals = ["dog", "cat", "tree frog"];
function pluralize(array){
for(var i=0;i<=array.length;i++){
array[i] += "s" ;
}
return (array);
}



You are returning in for loop.This is not correct



Use return statement after loop completed.
Also loop shoud iterate from 0 to less than array.length, instead of less than and equal to.


function pluralize(array){
for(var i=0;i<array.length;i++){
array[i] += "s" ;
}
return (array);
}



Please change i<=array.length to i<array.length.


i<=array.length


i<array.length



Otherwise,it will cause infinite loop.


for(var i=0;i<array.length;i++){
array[i] += "s" ;

}
return (array)//take it outside of for loop.
}




const animals = ["dog", "cat", "tree frog"];

function pluralize(array){
for(var i=0;i < array.length;i++){
array[i] += "s"
}
return array;
}

console.log(pluralize(animals));



First of all, your return will be executed on the first loop and exited the loop, which is not what you intended, and hence moving out from loop and place at the end of function.


return



Second, i <= array.length will hit array index outOfBound issues. Reason is array.length == 4 and array[4] += "s" is wrong as you don't have element at array[4].


i <= array.length


array index outOfBound


array.length == 4


array[4] += "s"


array[4]



It's recommended to change the parameter name in your pluralize function from array to something else like arr, purely to improve readability because a very similar word like Array is a keyword/reserve word


pluralize


array


arr


Array



There are multiple problems with your program.


i<=array.length;


for loop




const animals = ["dog", "cat", "tree frog"];
function pluralize(array){
for(var i=0;i<array.length;i++){
array[i] += "s";
}
return (array);
}
console.log(pluralize(animals));



The above program will return appropriate result.





I think, tree is not an animal but tree frog is.
– Isaac
Jul 1 at 9:31





Ok i thought you forgot comma, have changed.
– Sushant Magoo
Jul 1 at 9:35



Since you seem to use ES6/20xx, a simple map would do. If you want to mutate the array, use pluralizeMutating.


map


pluralizeMutating




const pluralize = array => array.map(v => `${v}s`);
const pluralizeMutating = array => array.forEach( (v, i) => array[i] += "s" );
let animals = ["dog", "cat", "tree frog"];
console.log(`Pluralized: [${pluralize(animals)}]`);
console.log(`animals Array is not touched: [${animals}]`);
pluralizeMutating(animals);
console.log(`pluralizeMutating ran - animals Array is now: [${animals}]`);





That’s not mutating. Mutating would be: array => array.forEach( ( x, i ) => array[ i ] = pluralize( x ) )
– Ben West
Jul 1 at 11:49


array => array.forEach( ( x, i ) => array[ i ] = pluralize( x ) )





That wouldn't work either, but you're right (I was distracted and had to leave editing the answer). Fixed the code.
– KooiInc
Jul 1 at 15:38






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter