Skip to content
Advertisement

How to add a string to the start and the end of each element in a JavaScript array?

This is an assingment from SoloLearn.
The idea is to add a string to all elements in an array, where each element is separated by a dollar sign $. The output should be as below

JavaScript

The way I tried is wrong, since after each element should be only one string, but the output for my code is

JavaScript

My attemp

JavaScript

Advertisement

Answer

Write output.push('$' + this.words[i]) instead of output.push("$"+this.words[i]+"$") and at last push $ to the output array.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement