Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String operation with array of strings

I have a array of strings like the example below

["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"]

u can see all sentence in array can be split by . and I need to make logical algo pattern to create a sentence meaningful by taking the array in reverse and stitch back the words at the end. if u read it from last, as i.am.new.to.coding taking last spit value from each sentence makes a meaningful sentence at last. am trying to create such a code in javascript or jquery and am stuck with this for more than a day. since it is so tricky.

any script experts plz help to make this. I appreciate your help. TIA

like image 497
Khaleel Avatar asked Nov 21 '14 06:11

Khaleel


2 Answers

Seems straight forward, reverse the array, map it returning the last part after the period, then join with spaces

var arr = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"];

var s = arr.reverse().map(function(x) {
    return x.split('.').pop();
}).join(' ');

document.body.innerHTML = s;
like image 81
adeneo Avatar answered Nov 18 '22 18:11

adeneo


var a = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"];

var s = a.reduceRight(function(x,y){
    return x + '.' + y.split('.').pop();
});

document.body.textContent = s;
like image 2
1983 Avatar answered Nov 18 '22 20:11

1983