JSFiddle - React, Tailwind, and code Playground

by pratik24

JavaScript

var str = "a b c d e f";

var lastIndex = str.lastIndexOf(" ");
var numOfWords = str.split(" ").length;
console.log( numOfWords);
var newStr= str;
for(var i= 0; i < numOfWords ; i++){

newStr = newStr.slice(0, lastIndex);
    
console.log(newStr);
}            
console.log(str.lastIndexOf(" "));