Skip to content

Instantly share code, notes, and snippets.

@jonurry
Last active July 7, 2023 10:44
Show Gist options
  • Save jonurry/ef3d28f16ba1e6ec6693a0777af52ad9 to your computer and use it in GitHub Desktop.
Save jonurry/ef3d28f16ba1e6ec6693a0777af52ad9 to your computer and use it in GitHub Desktop.
3.3 Bean Counting (Eloquent JavaScript Solutions)
function countBs(s) {
var count = 0;
for (var i = 0; i < s.length; i += 1) {
if (s.charAt(i) === "B")
count += 1;
}
return count;
}
function countChar(s, c) {
var count = 0;
for (var i = 0; i < s.length; i += 1) {
if (s.charAt(i) === c)
count += 1;
}
return count;
}
console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → 4
@jonurry
Copy link
Author

jonurry commented Feb 16, 2018

3.3. Bean counting

You can get the Nth character, or letter, from a string by writing "string"[N]. The returned value will be a string containing only one character (for example, "b"). The first character has position zero, which causes the last one to be found at position string.length - 1. In other words, a two-character string has length 2, and its characters have positions 0 and 1.

Write a function countBs that takes a string as its only argument and returns a number that indicates how many uppercase “B” characters there are in the string.

Next, write a function called countChar that behaves like countBs, except it takes a second argument that indicates the character that is to be counted (rather than counting only uppercase “B” characters). Rewrite countBs to make use of this new function.

@jonurry
Copy link
Author

jonurry commented Feb 16, 2018

Hints

Your function will need a loop that looks at every character in the string. It can run an index from zero to one below its length (< string.length). If the character at the current position is the same as the one the function is looking for, it adds 1 to a counter variable. Once the loop has finished, the counter can be returned.

Take care to make all the bindings used in the function local to the function by using the let keyword.

Copy link

ghost commented Jun 18, 2020

thanks man, needed that. especially the countBs. Cuz in the solution it was already rewritten.

@umpavi
Copy link

umpavi commented Feb 17, 2021

hey! not sure if you will see this but may i ask why the counter has to be written outside of the for loop? i got it wrong cause my counter was inside the for loop :(

@umpavi
Copy link

umpavi commented Feb 17, 2021

^ referring to var count = 0;

@jonurry
Copy link
Author

jonurry commented Feb 17, 2021

Hey @umpavi
If you declare the count variable within the for loop then it will reset to zero with every iteration of the loop.
By declaring count outside the for loop and setting the initial value to zero it can be incremented within the loop.
It's all about scope.
Variables declared within a code block (e.g. the for loop) have a different scope to those declared outside the block.

@umpavi
Copy link

umpavi commented Feb 18, 2021

Thank you so much for that clarification. I understand it now and I really appreciate it! I'm new to JS and programming in general, and I don't really know people I can ask for help when I'm stuck so it's really nice to see people out there who are willing to help! Thank you and have a great day!

@CaoLamBaoKhanh
Copy link

function countChar(s, c) {
var count = 0;
for (var i = 0; i < s.length; i += 1) {
if (s.charAt(i) === c)
count += 1;
}
return count;
}
function countBs(s){
return countChar(s,"B");
}
console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment