How to remove the last N words from a string in Node.js

1 Answer

0 votes
function removeTheLastNWordsFromString(str, total) {
	return str.split(' ').slice(0, -total).join(' ');
}

let str = 'javascript typescript node.js c c++ c# python';
const N = 3;

str = removeTheLastNWordsFromString(str, N);

console.log(str);


  
  
  
  
/*
run:

javascript typescript node.js c
  
*/

 



answered Jun 11, 2022 by avibootz
...