Str to obj and number of occurrences

Str to obj and number of occurrences
0

#1

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

#2

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?


#3

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.


#4

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


#5

Think hasOwnProperty method available to objects.


#6

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

#7

Do you want to add i?

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


#8

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

#9

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


#10

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.


#11

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


#12

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.


#13

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.


#14

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.


#15

Do I need an if/else?


#16

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


#17

I’m terrible at this:

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

#18

Explain what you think this should do?


#19

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


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