five
five

Reputation: 367

JavaScript - create a string by combining other strings at different positions

Suppose I have 3 strings: s1 = "ab", s2 = "cd", s3 = "ef".

The goal is to create another string by combining s1, s2, and s3. The catch is that the user can decide the positions of those 3 strings. So, for instance:

s1 - position 3;

s2 - position 2;

s3 - position 1

Result: efcdab.

My question is, what is the best way to solve this problem? My solution was to create 3 objects that will each hold the string position and value as properties, add the objects into and array and then sort the array using the position property of each object, but I just have the feeling that there is a better solution. Thanks in advance!

Upvotes: 0

Views: 135

Answers (5)

Matt
Matt

Reputation: 1082

I'm not sure how you are having them pick the order, but with this all you have to do is fill in the options for them to choose and it does the rest of the work. This can be made prettier by a lot, but that is up to you and how you take user interaction.

var selections = ["ab", "cd", "ef"];
var str = "";

while(selections.length){
    var choice = prompt("Type part of the string from below that you want to add first:\n " + "    " + selections);
    var index = selections.indexOf(choice);

    if(index !== -1){
        str += choice;
        selections.splice(index, 1);
    }
}

alert("You typed: " + str);

Upvotes: 0

le_m
le_m

Reputation: 20228

If your strings and positions are given as an array of {value, position} objects, you can use Array.reduce to order and concatenate them to an ordered string in linear time:

let strings = [
  {value: "ab", position: 3},
  {value: "cd", position: 2},
  {value: "ef", position: 1}
];

let string = strings.reduce((sorted, next) => {
  sorted[next.position - 1] = next.value;
  return sorted;
}, []).join("");

console.log(string); // "efcdab"

Upvotes: 0

Samarth
Samarth

Reputation: 773

Just giving it a try where you strPos is the user defined object for ordering the strings

var s1 = 'ab';
var s2 = 'cd';
var s3 = 'ef';
var strs = {s1:s1,s2:s2,s3:s3};
var strPos = {1:'s1',2:'s3',3:'s2'};
var fin = '';
for(var i=1;i<=3;i++){
  fin += strs[strPos[i]];
}
console.log(fin);

@Five from your comments the answer can be changed to follow your object structure as below

var definedOrderedList = [{
  value: 'ab',
  position: 2
}, {
  value: 'cd',
  position: 1
}, {
  value: 'ef',
  position: 3
}];
var strArr = [];
for (var o in definedOrderedList) {
  strArr[definedOrderedList[o].position] = definedOrderedList[o].value;
}
var finalString = strArr.join('');
console.log(finalString);

Upvotes: 1

Pankaj Shukla
Pankaj Shukla

Reputation: 2672

Based on how you described your object structure, looks like you will have an array like this(chosen one possible random ordering):

var arr = [ { value: "ab", position: 2 },  { value: "cd", position: 1 },  { value: "ef", position: 3 } ];

Then you can sort the array based on the position and then concatenate the strings.

var arr = [ { value: "ab", position: 2 },  { value: "cd", position: 1 },  { value: "ef", position: 3 } ];
arr.sort((a,b) => a.position - b.position);

var ans="";
for(var i=0;i<arr.length;i++) {
  ans += arr[i].value;
}
console.log(ans);

Upvotes: 0

Neetigya
Neetigya

Reputation: 121

You can use two objects. One that has string and values, lets say values like

{
  s1: 'String1',
  s2: 'String2',
  s3: 'String3'
}

and one lets say position object like

{
  p1: 'store user entry for position1',
  p2: 'store user entry for position2',
  p3: 'store user entry for position3'
}

and access first object like this values[position['p1']] add to values[position['p2']] and so on

Upvotes: 0

Related Questions