Skip to content Skip to sidebar Skip to footer

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 arr

Solution 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() {
    vararray = ['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();

Solution 2:

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

functioncombine(array) {
    functionc(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);

Solution 3:

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(' ');
  returnArray.from(newSet(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"].

Solution 4:

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.

Post a Comment for "Word Combination In An Array Using Javascript"