3

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');
Noor
  • 41
  • 4
  • 1
    https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Bergi Jul 20 '16 at 03:35
  • What exactly is your problem? Where is your code? What have you tried? – Bergi Jul 20 '16 at 03:36
  • Those are not called permutations. Maybe the problem is easier than you think. Just spell out all the combinations you can think of manually. – Bergi Jul 20 '16 at 03:36
  • Sounds like you need to write some code. You could start off by writing "pseudo-code", which is a description of the algorithm. –  Jul 20 '16 at 04:20
  • @Bergi Manually I can think of - alexrobert(first name + last name), alexr(first name+ first word of last name), arobert(first word of first name + last name), alexsrobert (first name+ middle name's first word + last name). Let' say if I have to guess the email address of a person who's name is Alex Same Robert. I could probably try above combination + gmail.com(or any domain they belong to) – Noor Jul 20 '16 at 06:43
  • Please [edit] your question to include properly formatted code, then delete your comment – Bergi Jul 20 '16 at 06:48

4 Answers4

1

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();
Taztingo
  • 1,915
  • 2
  • 27
  • 42
1

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

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"].

Sunda
  • 227
  • 3
  • 3
0

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.

Community
  • 1
  • 1
Trevor Clarke
  • 1,482
  • 1
  • 11
  • 20