i'm having a little confusion in string use for javascript and I don't understand how to do something. Without using an array, I want to find and print out the number of singular letters that string has ( for example if the string is "Peter picked a peck of pickled peppers a peck of pickled peppers Peter piper picked" the how would i be able to make the system print the number of P's in this string is whatever it is). Thank you to whoever can help.
Asked
Active
Viewed 181 times
-2
-
`without using arrays` ... do you mean at all? because `"Peter picked a peck of pickled peppers a peck of pickled peppers Peter piper picked".match(/p/ig).length` uses an array (in the result of match) but gives you the answer – Jaromanda X Nov 22 '15 at 22:27
-
check this : http://stackoverflow.com/questions/10710345/finding-all-indexes-of-a-specified-character-within-a-string – Shhade Slman Nov 22 '15 at 22:28
-
@ShhadeSlman - check question `Without using an array` – Jaromanda X Nov 22 '15 at 22:30
-
["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic): Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. – Leon Adler Nov 22 '15 at 22:58
2 Answers
1
You can use the charAt()
method to examine individual characters in a string.
"Peter".charAt(0) returns 'P'
If you want to access the character codes, you can use charCodeAt()
.
"Peter".charCodeAt(0) returns 80
See the MDN documentation on charAt

jdigital
- 11,926
- 4
- 34
- 51
-
is there any other method that you can think of because charCodeAt() though useful would not be applicable in my situation. i am only allowed to use: – Nathan Portnoy Nov 22 '15 at 22:36
-
String (String str) char charAt (int index) int compareTo (String str) String concat (String str) boolean equals (String str) boolean equalsIgnoreCase (String str) int length( ) String replace (char oldChar, char newChar) String substring (int offset, int endIndex). String toLowerCase ( ) String toUpperCase( ) int indexOf(String s) – Nathan Portnoy Nov 22 '15 at 22:37
-
1
I'm assuming this is for a school assignment after reading your comment on what you're allowed and not allowed to use. If that is the case, this solution will help you better understand how to accomplish that programmatically.
If you want to find the count of a specific character in a string such as the one you wrote, you could just traverse through the string and increment a variable each time one is found by using charAt
on the given string.
var myString = "Peter picked a peck of pickled peppers a peck of pickled peppers Peter piper picked";
function countChars(string){
var count = 0;
for(var i=0;i<myString.length;i++){
if(string.charAt(i)=="p" || string.charAt(i)=="P"){
count++;
}
}
return count;
}
console.log(countChars(myString));

rze
- 248
- 1
- 3
- 14
-
@NathanPortnoy that'll be pretty easy for you to change then since all of the functionality of the code I wrote would be intact. You would just need to change what you output in `console.log` as well as just removing one thing. However I'll leave you to figure out what to remove since it is a school assignment. Good luck with your assignment. – rze Nov 22 '15 at 22:58