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 156 views
1 answer 121 views
1 answer 122 views
2 answers 244 views
2 answers 233 views
1 answer 153 views
1 answer 137 views
...