Noor
Noor

Reputation: 41

word combination in an array using javascript

Let's says I've an array['Alex', 'Sam', 'Robert']

I'd like to combine them something like:

Take first array[0] and append with array[2] which will be AlexRobert

first letter of array[0] which is A and append with array[2] that is Robert which will be ARobert

Take array[0] which is Alex and append with first letter of array[2] that is R which will be AlexR

Take first array[0] append with first letter of array[1] along with array[2] which will become AlexSRobert.

Basically the whole idea is when someone enter first name, middle name & last name I should be able to make combination and guess email ids. For example- Juan F. Nathaniel the array form will be like ['Juan', 'F', 'Nathaniel']

I want the combination of first, middle and last name like jaunn, jnathaniel, jaunfnathaniel

I'm beginner and here is what I've written:

var nameCombination = function(name){

  var counting = name.split(" ");

  for (var i=0; i<counting.length; i++){
      console.log(counting[i] + counting[i+1]);
      console.log(counting[i].split("",1) + counting[i+1]);
      }

}

nameCombination('Alex Sam Robert');

Upvotes: 3

Views: 1202

Answers (4)

Sunda
Sunda

Reputation: 227

This problem can be solved using recursive approach.

var combinations = function(names, i, n){

  if(i == n){
    return [];
  }
  last_names = combinations(names, i + 1, n);

  name_combinations = last_names.map(function(last_name){ 
    return [ 
      last_name,
      names[i] + last_name,
      names[i] + last_name[0],
      names[i][0] + last_name,
      names[i][0] + last_name[0]
    ]
  });
  name_combinations = [].concat.apply([], name_combinations);
  name_combinations.push(names[i]);
  return name_combinations;
};


var nameCombinations = function(name){
  var name_array = name.split(' ');
  return Array.from(new Set(combinations(name_array, 0, name_array.length)));
};

nameCombinations('first last');

above function can generate all the desired combinations for a given name.

for example: nameCombinations('first last') will return ["last", "firstlast", "firstl", "flast", "fl", "first"].

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386620

You could use an iterative and recursive approach for variable length of parts an their length.

function combine(array) {
    function c(part, index) {
        array[index].forEach(function (a) {
            var p = part.concat(a);
            if (p.length === array.length) {
                r.push(p.join(''));
                return;
            }
            c(p, index + 1);
        });
    }

    var r = [];

    c([], 0);
    return r;
}

var input= ['Johann', 'Sebastian', 'Bach'],
    array = input.map(function (a) { return ['', a[0], a]; });
    result = combine(array);

console.log(result);

Upvotes: 1

Trevor Clarke
Trevor Clarke

Reputation: 1478

Ok without writing out every combination I will do the first few to give you the idea:

assuming

array[0] is the person's first name 
array[1] is the person's middle name 
array[2] is the person's last name

Firstname+Lastname:

var output = array[0] + array [2];

Firstname+Middlename:

var output1 = array[0] + array[1];

then then you could display the output using innerHTML:

Javascript:

document.getElementById("output").innerHTML = output + '<br>' + output1;

HTML:

<div id="output"></div>

Keep in mind you would need to keep doing that for the rest of the combinations.

Now for the combinations where you need to get the first letter of the variable you need to use charAt which I found from this stack overflow answer.

You would do the same thing as before, except instead you need to use charAt and do something like so:

Firstname+FirstLetterOfLastName:

var output2 = array[0] + array[2].charAt(0);

And you can output it using the same method as before.

If your still confused leave a comment and I will try and answer your questions.

Upvotes: 0

Taztingo
Taztingo

Reputation: 1945

I'm assuming you needed a function to do this? Here is a function to handle grabbing pieces of each index of the array. I'll leave it up to you to figure out what type of data you need...

   var test = function() {
    var array = ['Alex', 'Sam', 'Robert'];

    var conditions = [{
            index: 0,
            length: array[0].length
        },
        {
            index: 1,
            length: 1
        },
        {
            index: 2,
            length: array[2].length
        }]

        alert(combine(array, conditions));
}

var combine = function(array, conditions) {
    var output = "";
    for(index in conditions) {
    var condition = conditions[index];
        var index = condition['index'];
        var length = condition['length'];
        output += array[index].substring(0, length);
    }
    return output;
}

test();

Upvotes: 1

Related Questions