Last active
September 7, 2023 21:37
-
-
Save Shariar-Hasan/968d4e3c8176e66a9c6cc637e052c121 to your computer and use it in GitHub Desktop.
counting words from a string in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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