Steve Carroll
2025-01-27 22:04:42 UTC
Reply
Permalink'program' to 'countLetterFrequency' in a text string:
The program:
step 1: get (or create) something to store letter counts
step 2: 'loop' through the letters, one at a time
step 3: provide a way for each letter occurrence to be counted
step 4: 'return' the counts
Example string: 'hello world'
This is something you could do visually and end up with...
h = 1, e = 1, l = 3, o = 2, w = 1, r = 1, d = 1
... using a pencil and paper.
In JS, a good means of storing letter counts is the JS 'object'. Head to
your browser's dev tools 'Console' tab and run this, which uses 'let',
the '=' sign and 'curly braces':
let yourCar = {} // creates an empty object
How do we put things into this object?
One way is 'bracket notation'... in the console again:
yourCar['make'] = 'Toyota';
How to access this 'object'? Use it's name:
yourCar // outputs: { make: 'Toyota' }
How to access the 'value' of a 'property' belonging to an 'object':
yourCar['make'] // outputs: 'Toyota'
So... 'yourCar' is an object with one property, 'make', and that
property has a value of 'Toyota' (that's the lingo).
Elsewhere, along with 'bracket notation', 'if/else' and the 'for...of'
loop, we also covered '=' and '+='. Look at the following image and
think about 'curly braces', 'bracket notation', '=' and '+='
<Loading Image...
Can you figure out what's going on there just by looking? If you looked
closely (especially at the accumulating counts), the following may not
be so confusing:
function countLetterFrequency(text) {
const count = {} // empty object to store letter counts in
for (let letter of text) { // loop through text, one letter at a time
if (count[letter] === undefined) { // check if current letter is in 'count'
count[letter] = 1 // if not, set initial count to 1
} else {
count[letter] += 1 // if letter is in count, increase count by 1
}
}
return count // return 'count' object
}
countLetterFrequency('hello world')
// output: { h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1 }
That's code... one version of it, anyway... for our 'program'.
Here's another, using something called the 'ternary operator':
function countLetterFrequency3(text) {
const count = {}
for (let letter of text) count[letter] === undefined ? count[letter] = 1 : count[letter] += 1
return count
}
I'm not recommending the use of the ternary operator here, just showing
it could be used (but if/else is easier to read). Notice even the curly
braces of the 'for... of' loop have been omitted. There can be different
'looks' to JS you'll see 'out in the wild', don't let them deter you.
--
Reference page:
<vmekhl$74sp$***@fretwizzer.eternal-september.org>
Setup:
<vmgi9e$u5m6$***@fretwizzer.eternal-september.org>
Variables:
<vmh41h$u5m6$***@fretwizzer.eternal-september.org>
Functions:
<vmh7ln$u5m6$***@fretwizzer.eternal-september.org>
Arrays/looping:
<vmmi7n$3edp3$***@fretwizzer.eternal-september.org>
Calculator project - Part 1
<vmjd3a$2bdqi$***@fretwizzer.eternal-september.org>
Calculator project - Part 2
<vmrch2$14j7l$***@fretwizzer.eternal-september.org>
DOM - Part 1
<vmubfb$1pvlc$***@fretwizzer.eternal-september.org>
Text generator project - Part 1
<vmug55$1pvlc$***@fretwizzer.eternal-september.org>
Text generator project - Part 2
<vn66pb$36go$***@fretwizzer.eternal-september.org>
Text generator project - Part 3
<vn69if$56c0$***@fretwizzer.eternal-september.org>