Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 774
Noor Avatar asked Feb 21 '26 22:02

Noor


2 Answers

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();
like image 53
Taztingo Avatar answered Feb 23 '26 12:02

Taztingo


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);
like image 36
Nina Scholz Avatar answered Feb 23 '26 13:02

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!