Tried a variety of different ways, but having a hard time putting it into code after the for loop. Any guidance is appreciated.
Instructions:
Write a function named letterCount that takes a string and returns an object with the letters and the number of their occurrences
Example:
If you pass "Yo" it should return {Y: 1, o: 1}
If you pass "Hello" it should return {"H": 1, "e": 1, "l": 2, "o": 1}
My code:
function letterCount(str) {
let obj = {};
for (let i = 0; i < str.length; i++) {
}
return obj;
};
Each letter will be a property in the object. You will check if the property name exists (You aleady should know how to do this). If it does not exist, assign a value of 1 to the property. If it does exists, then increment the existing property value by 1.
function letterCount(str) {
let obj = {};
let count = 0;
for (let i = 0; i < str.length; i++) {
if (obj[0] !== "true"){
obj[str[i]] + i;
}else{
}
}
return obj;
};
function letterCount(str) {
let obj = {};
let count = 0;
for (let i = 0; i < str.length; i++) {
if (obj.hasOwnProperty([0])){
obj[str[i]];
}else{
}
}
return obj;
};
Well, I’m not so sure yet. I thought maybe the loop could figure that out for me, but I’m still a little confused on what I need to do exactly. Not very good with objects I guess.
This was the challenge before it that I thought maybe I could work off of:
Instructions:
Write a function named letterMap that takes a string and returns an object of the letters and their positions in the string
Example:
If you pass "Yo" it should return {Y: 0, o: 1}
If you pass "Hello" it should return {H: 0, e: 1, l: 3, o: 4}
My code:
function letterMap(str) {
let obj = {};
for (let i = 0; i < str.length; i++) {
obj[str[i]] = i;
}
return obj;
};
Just seems to be I need another accumulator to be count or something.
No, this was the challenge before. It was the solution for what it asked. Just thought with the current challenge (this thread), I thought maybe I could somewhat work off of it.