Str to obj and number of occurrences

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;
};

try to solve a simple example with the code and then change the code as needed for more complex examples.

So if you have ‘Yo’, when you loop through it, what would you do to count the letters?

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.

What do you mean by checking if the property name exists? I have an if and else statement ready, but I need to check if(obj[0] === "undefined")?

Think hasOwnProperty method available to objects.

Might be getting ahead of myself here…

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;
};

Do you want to add i?

Also, you are only going to check if the object property “0” is equal to the string “true”?

Just messing around with what you’re telling me.

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;
};

What about other property names like 1, 2, 3, 4, 5, etc?

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.

I do not think you have your algorithm developed yet. You are just “testing” code. Write your algorithm first and then write your code.

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.

The above returns:

{ H: 0, e: 1, l: 3, o: 4 }

You are simply assigning the current value of i for each letter in the str.

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.

Do I need an if/else?

Using a for of loop, try to implement what I have suggested above.

I’m terrible at this:

function letterCount(str) {
    let obj = {};
    let count = 0;
    
    for (let i of str) {
        obj[i], [count++];
    }
    
    
    return obj;
};

Explain what you think this should do?

It was an absolute shot in the dark. I thought it would solve it. Not good with for…of loops either.

function letterCount(str) {
    let obj = {};
    let count = 0;
    
    for (let i of str) {
        if (obj[0]){
            count++
        }
    }
    
    
    return obj;
};