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?

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")?

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

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

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.

Do I need an if/else?

I’m terrible at this:

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

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

I don’t know how to target every other possible character in the string. And read the docs on hasOwnProperty and very confused on how to implement it.

The index in the string…okay I feel stupid. But do I need an if/else with this?

This is improper implementation, isn’t it?

function letterCount(str) {
    let obj = {};
    let count = 0;
    
    for (let i of str) {
        if(obj.hasOwnProperty()) {
            
        }
    }
    
    
    return obj;
};

That is exactly why I’m confused. Why am I looking for something in particular when I have a myriad of possibilities I need to worry about? And yes, I understand I am overcomplicating it. This is easily my biggest flaw.

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

EDIT: and if I’m not mistaken, I believe I’m looking for [i] in the string, and have to push it to the object.

Sorry, I will read through, but it wants:
The property is a string.

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}

Okay, didn’t even realize but why is the first not a string and the second one is…

Gotcha…will try to get after it.

I have this, but it’s not right. It is returning the empty object.

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

Trouble understand why I need an if/else, I can’t think of another instance. If it has a property, count, if not, don’t?

I need a third variable on top of obj and count?