Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the JavaScript code I’ve to convert the String to an array in order to reverse a String backwards. Can anyone tell me is there a direct way of doing this? 


 

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <meta http-equiv="X-UA-Compatible" content="ie=edge">

        <title>Document</title>

    </head>

    <body>

        String manipulation with 

        <pre id="target">w3resource</pre>

        <script src="script.js"></script>

    </body>

</html>

var word = document.getElementById('target');

var arr = Array.from(word.innerText);

console.log(arr)

var newArr = []

function rearrangement(arr1, arr2)

{

    for(var i = 9; i < arr1.length; i--)

        arr2.unshift(arr1.pop(i))

    return arr2

}

console.log(rearrangement(arr, newArr))

1 Answer

0 votes
by (19.7k points)

Instead of converting the String to an array, you can use the reverse() method like below:

Array.from("test").reverse();

Interested in Java? Check out this Java Certification by Intellipaat.    

Related questions

0 votes
1 answer
0 votes
1 answer
asked May 7, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Feb 19, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
4 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...