5

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

4 Answers 4

6

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;

Sign up to request clarification or add additional context in comments.

1 Comment

It works like charm for any input and any delimiter I actually needed. Hats off for ur valuable help. and I never thought it ll done in such a simple and understandable code. :) Thanks a lot adeneo
2

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;

Comments

1

This worked for me:

var array = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"]
var b = [];
for(i=array.length-1;i>=0;i--) {
    var a = array[i].split('.').pop()
    b += " "+a
    alert(a)
}
alert(b)

Comments

1

Another way:

arr = ["i.was.wen.the.coding", "i.am.wen.to", "i.am.new", "i.am", "i"];
arr = arr.reverse();
str = '';
for(i=0;i<arr.length;i++)
{
    data = arr[i].split('.');
    len = data.length;
    str = str + data[len-1] + " ";
}
console.log(str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.