I want to create a JS function that adds a string ending to each member in an array. The expected function when called should return something like this:
addEnding(["clever", "meek", "hurried", "nice"], "ly")
Expected output:
["cleverly", "meekly", "hurriedly", "nicely"]
Thanks for all answers suggesting to use map
method, but I want to use a for
loop, here is what my code looks like:
const addEnding = (arr, str) => {
let result = '';
for (i = 0; i < arr.length; i++) {
result = arr[i]
result += str
}
return result
}
console.log(addEnding(["new", "pander", "scoop"], "er"))
I want to create a JS function that adds a string ending to each member in an array. The expected function when called should return something like this:
addEnding(["clever", "meek", "hurried", "nice"], "ly")
Expected output:
["cleverly", "meekly", "hurriedly", "nicely"]
Thanks for all answers suggesting to use map
method, but I want to use a for
loop, here is what my code looks like:
const addEnding = (arr, str) => {
let result = '';
for (i = 0; i < arr.length; i++) {
result = arr[i]
result += str
}
return result
}
console.log(addEnding(["new", "pander", "scoop"], "er"))
The issue is that it only returns the first element of an array, when I expect it to return all elements from array + str ending
Share Improve this question edited Apr 3, 2019 at 5:38 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Apr 3, 2019 at 3:34 tseekertseeker 799 bronze badges 2- Using map() method returns a new array, it does not modify the existing one. It is better to use for/forEach loops if you are looking to update the exisiting array. Check my answer as well. – hygull Commented Apr 3, 2019 at 4:30
-
In your case the little mistake you did is, you used result variable and updated that. Use
arr[i] = arr[i] + str
. – hygull Commented Apr 3, 2019 at 4:33
10 Answers
Reset to default 1You declared the result as a string and replace it over the iteration. Just change the data type to array
as you expected.
const addEnding = (arr, str) => {
let result = '' // You initiated a string as a result not an array
for(i=0; i<arr.length; i++) {
result = arr[i] // The string got replaced every time it interate
result += str
}
return result // The result is always the last item
}
console.log(addEnding(["new", "pander", "scoop"], "er"))
const arr =["new", "pander", "scoop"]
const str = 'er'
const addEnding = (arr, str) => {
let result = []; // Init an array as format output
for(i=0; i<arr.length; i++) {
result[i] = arr[i] + str // assign like this
}
return result
}
console.log(addEnding(arr, str))
use map function, which will iterate over your array and return a new array with the modified version of each.
Using string interpolation you can append easily the suffix
function addEnding(arr, end) {
return arr.map(word => `${word}${end}`);
}
console.log(addEnding(["clever", "meek", "hurried", "nice"], "ly"))
function addSuffix(arr, suff){
var newArr=[];
for (var i = 0; i < arr.length; i++)
newArr.push(arr[i] + suff);
return newArr;
}
Try to the following code
function addEnding(array, suffix) {
if (Array.isArray(array)) {
for (var i = 0; i < array.length; i++) {
array[i] = array[i] + suffix;
}
}
return array;
}
console.log(addEnding(["clever", "meek", "hurried", "nice"], "ly"));
You can use Array#map along with String#concat
function addEnding(words, suffix) {
return words.map(word => word.concat(suffix))
}
const output = addEnding(["clever", "meek", "hurried", "nice"], "ly");
console.log(output);
If you are looking to use for loop and want to do this in a functional way (the way you choose) then you can try like this.
I have executed the statements on Node REPL.
Note » Using map() method returns a new array, it does not modify the existing one. It is better to use for/forEach loops if you are looking to update the exisiting array.
Your mistake: In your case the little mistake you did is » you used result variable and updated that. Use
arr[i] = arr[i] + str
.
> const addEnding = (arr, str) => {
... arr.forEach((item, i) => {
..... arr[i] = item + str;
..... })
... return arr
... }
undefined
>
> addEnding(["clever", "meek", "hurried", "nice"], "ly")
[ 'cleverly', 'meekly', 'hurriedly', 'nicely' ]
>
> addEnding(["new", "pander", "scoop"], "er")
[ 'newer', 'panderer', 'scooper' ]
>
function wordConverter(arr, str) {
return arr.map(x => x.concat(str));
}
console.log(wordConverter(['clean', 'dye', 'dry', 'call'], 'ing'));
Use map
which will return a new array of words formed by concatenating words from original array and the suffix
function addEnding(array, suffix) {
// assuring input is array otherwise map will not work
if (Array.isArray(array)) {
return array.map(item => item + suffix)
}
}
console.log(addEnding(["clever", "meek", "hurried", "nice"], "ly"))
Use Array.prototype.map()
const addEnding = (array, suffix) => array.map(word => `${word}${suffix}`);
const result = addEnding(["clever", "meek", "hurried", "nice"], "ly");
console.log(result);
You can do it with the below function if you want to use the traditional for loop.
function addEnding(arr, end) {
for(let i=0; i<= arr.length-1; i++)
arr[i] += end;
return arr;
}