Function that transposes two strings from top to bottom instead of left to right within an array

My current challenge involves transposing two strings vertically instead of horizontally using javascript. I'm contemplating whether to employ the map() and slice() methods for this task. The input will be an array containing two strings, and my goal is to create a function that arranges these strings from top to bottom.

For example, if given ['Hello', 'World'], the output should be:

H W
e o
l r
l l
o d

Here are some important points to consider:

- There should always be one space between each pair of characters. - The case of the characters should remain unchanged, no need to convert to upper or lower case. - If one string is longer than the other, a space should be inserted in place of the missing character.

Answer №1

Updated as I overlooked the condition where the strings may differ in length:

let exampleArray = ['Hi','There'];

function reorganize(array) {
  let finalResult = '';
  let index = 0;
  while(array[0][index] || array[1][index]) {
    finalResult += array[0][index] || ' ';
    finalResult += ' ';                            // add space if desired
    finalResult += array[1][index] || ' ';
    finalResult += '\n';
    index++;
  }
  return finalResult.slice(0, -1);
}

reorganize(exampleArray); // results in:
                              "H T
                               i h
                                r
                                 e"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jq to filter and update specific items in a JSON array based on parent properties

I'm currently exploring the use of jq to selectively update a specific property within an object nested in an array, while preserving the rest of the array unchanged. The new property value should reference a property from the parent object. For inst ...

Are you utilizing the User Extensions/Plug in feature with ExtJS 4?

In the process of developing a web application, I have decided to include a password strength meter feature. While there are numerous examples available, such as this one, I am encountering difficulties implementing the solution using Sencha Architect. My ...

Having trouble retrieving response headers in Angular 5

After sending a post request to a server, I receive a response with two crucial headers for the client: username and access-token. The Chrome debug tool's Network Tab displays the data from the response like this: https://i.sstatic.net/XN9iv.png In ...

What is the reason for the lack of arguments being passed to this Express middleware function?

I have been developing a middleware that requires the use of `bodyParser` to function, however I do not want to directly incorporate it as a dependency in my application. Instead, I aim to create a package that includes this requirement and exports a middl ...

Tips for correctly setting values for an Array of struct within another struct:

Currently, I am facing an issue with declaring an array inside a structure and struggling to call it correctly. Any guidance or assistance would be highly appreciated. I attempted two different approaches: the first one resulted in a compilation error, wh ...

Adjusting image width using jQuery

I have been experimenting with creating a Webgl hover effect for an image. The effect works well, but now I am trying to specify a width for the image within jQuery. new hoverEffect({ parent: document.querySelector('.ticket'), intensity1: 0. ...

Filtering strings in React using the .includes() method results in an empty array

Whenever I run this sample code in a sandbox environment, it functions properly. However, when implemented in my functional component, it fails to work. I have trimmed down the code to include only the essential parts for demonstration purposes. The state ...

Using Express js, invoke a function object from a route

Here's a snippet of my code: router.route('/user') .post(function(req, res, next){ queryDB(arg1, arg2, prepareRes) }) .get(function(req, res, next){ queryDB(arg3, arg4, prepareRes) }); var prepareRes = function(err, data){ if ...

Is it possible to programmatically set focus on a DOM element using JavaScript or jQuery?

My code dynamically loads select boxes based on user selections. I have a specific instruction to select an option, which looks like this: //returns a string of <option>s by ID $('#subtopic_id').load('ajax.php?f=newSubtopic&am ...

What is preventing me from retrieving the parameter in the controller?

I could use some assistance with implementing pagination for displaying data. However, I am encountering issues in retrieving the parameter in the Controller Method. To provide more context, I have created a demo on CodePen which can be viewed at http://c ...

Receive emails: using the require() function with ES Module

After following the react-email documentation, I encountered an error when trying to run the yarn email command: $ email dev --port 3001 ***\node_modules\ora\index.js:65 if (process.platform === 'win32') { ...

Running npm commands, such as create-react-app, without an internet connection can be a

Currently, I am working in an offline environment without access to the internet. My system has node JS installed. However, whenever I attempt to execute the npm create-react-app command, I encounter an error. Is there a workaround that would allow me to ...

Shift a Div to the left prior to stretching it out

I am trying to achieve a smooth leftward movement and expansion animation for a div. Currently, I am experimenting with class switching on click events to transition between the desired states. However, I am facing difficulty in making the element first mo ...

Elements from Firebase failing to appear in Angular Grid

I'm struggling to populate items within this grid sourced from Firebase. I was able to make it work with a custom Service that returned a fixed array. I can confirm that the data is being received by the browser as I can log it out in JSON format lik ...

Find out if all attributes of the object are identical

I am trying to create the boolean variable hasMultipleCoverageLines in order to determine whether there are multiple unique values for coverageLineName within the coverageLines items. Is there a more efficient way to write this logic without explicitly c ...

What is the best way to link this to a function in AngularIO's Observable::subscribe method?

Many examples use the Observable.subscribe() function in AngularIO. However, I have only seen anonymous functions being used like this: bar().subscribe(data => this.data = data, ...); When I try to use a function from the same class like this: update ...

Removing a post in Meteor-React using a submission modal is not possible as the post is not defined

After creating an additional submit modal for user confirmation when deleting a post from the collection, I'm facing difficulty in targeting the post. I also have a productivity query - should I include the DeletePost component in each post component ...

Creating an HTML list based on the content of a textarea input

Looking for some guidance in creating a dynamic list using JavaScript. The goal is to have user input data into a textarea, and upon clicking the submit button, generate a list in HTML. HTML: <body> <div class="container"> <di ...

Verifying the Page Load Status in the Browser

I've been spending a lot of time lately trying to find code that will allow me to trigger an action after the browser finishes loading a page. After much searching, I finally came across this piece of code on <script> var everythingLoaded = s ...

How can you develop a custom language for Monaco Editor in an Angular project?

I am attempting to develop a custom language with auto-complete (intellisenses), but I am facing challenges. Can anyone provide assistance in achieving this? Code https://stackblitz.com/edit/angular-7-master-emjqsr?file=src/app/app.module.ts ...