Skip to content

Instantly share code, notes, and snippets.

@Shariar-Hasan
Last active September 7, 2023 21:37
Show Gist options
  • Save Shariar-Hasan/968d4e3c8176e66a9c6cc637e052c121 to your computer and use it in GitHub Desktop.
Save Shariar-Hasan/968d4e3c8176e66a9c6cc637e052c121 to your computer and use it in GitHub Desktop.
counting words from a string in Javascript
var words = "amake amar moto thakte dao. ami nijeke nijer moto ghuchiye niyechi";
var counter=0;
for (var i = 0; i< words.length ; i++)
{
if(words[i] == " " && words[i-1] != " " && i != 0 && i != words.length-1)
{
counter++;
}
}
counter++;
console.log("1 No Counter : words in that array is",counter);
// another way is :
const counter2 = words.trim().split(" ").length;
console.log("2 No Counter : words in that array is",counter2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment