How to get the last two words from a string in JavaScript

1 Answer

0 votes
function get_last_two_words(s) {
    var arr = s.split(" ");
     
    return arr[arr.length - 2] + " " + arr[arr.length - 1];
}
 
var s = "javascript php c c++ python"; 
 
document.write(get_last_two_words(s));
 
 
 
/*
run:
 
c++ python 
 
*/

 



answered Sep 7, 2019 by avibootz

Related questions

1 answer 125 views
1 answer 95 views
1 answer 96 views
2 answers 191 views
2 answers 200 views
1 answer 123 views
1 answer 111 views
...