JavaScript function to prepend string into all the values of array?


The problem statement says to perform a prepend string into all the values of array elements where the prepend string and array values will be given by the user as an input source.

The problem statement can be deeply visualized as given a string text specifically to prepend or attach at the beginning of each element of the array , the output should return the new resultant array with prepended string value in combination with the value of array elements.

What is an Array in JavaScript ?

If you are familiar with any other programming language like C, C++, or Java, you must have heard the term 'array.'

In programming, an array is a collection of similar data elements under one roof.

Now, an important question arises: if arrays are generally the same in all languages, then how does JavaScript make arrays more unique and usable?

Let's understand the overall working of arrays in JavaScript.

An array is an object that stores multiple elements. Since an array is also an object, it has some properties and methods that make working with arrays easier in JavaScript.

Following is the syntax to define Arrays in JavaScript : −

const arrayExample  = [ 2 , 3 , 5 ,6 ];
console.log(arrayExample);

Output

[2, 3, 5, 6]

What is a prepend String in JavaScript ?

Prepend a string in JavaScript actually means adding some specific string at the beginning of input value given by the user that will result in a newly obtained string array combination.

Algorithm

Step 1 − Declare a function with name prepend that takes string input to be prepended and string array as an input

Step 2 − Traverse every element of the string array using a for loop till the length of string elements such that every string element present in the string array needs to be prepended with the input using + operator .

Step 3 − Once a for loop is used to prepend every string array element , the resultant array with its length property is returned because the prepending new string array replaces the original string array given as an input by the user.

Example

function prepend ( str  , stringArray )
{
    for(let i = 0 ; i<stringArray.length ;i++)
    {
        stringArray[i] = `${str}` + stringArray[i];
       
    }
    
    return stringArray.length;
}

const str = "Hello";
const stringArray = ["naman" , "priya" , "rishi"];
prepend(str,stringArray);
console.log(stringArray);

Output

The output using above main code would look like this in console :

[ 'Hellonaman', 'Hellopriya', 'Hellorishi' ]

Time and Space Complexity

The time and space complexity is not difficult to understand in this problem statement because the core logic is performed by loop only which traverses till the length of the string array to prepend every string with an input text hence resulting in O (n) worst case time complexity.

Conclusion

This is how we can solve the above problem statement thinking logically and in the context of coding taking help of javascript additional operators and for loop in its most efficient use case .

Updated on: 21-Aug-2023

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements