3

So I am making a game that calculates love percentage based on the letters in two peoples names with the word love.

In the code below I am counting the number of times a letter appears in the value array then I want to push this value into the nums Array for each unique letter. However I am not sure how I can get it to not repeat for each time the letter appears in the value array. Also I can not sort the array or the game will not calculate properly.

so for example the below value array should push 3 for s only once into the nums array.

var value =["s", "a", "m", "a", "n", "t", "h", "a", "w", "h", "i", "t", "e", "l", "o", "v", "e", "s", "J", "a", "c", "k", "h", "a", "r", "r", "i", "s"]; 


function trueLoveGame(value){

    var nums=[];

    for(var i =0; i < value.length;i++){
        var count = 0;
        for(var a = 0; a < value.length;a++){
            if(value[i] === value[a]){  
                count++;
            }
            else{

            }
        }   
    }
}
3
  • 2
    see if the letter's in the already before pushing it, then. or you should be using the letters as the array keys instead, e.g. letters['s'] = 3, so you don't have to worry about uniques - using the letters as keys will automatically make each letter unique, because you can't have duplicate keys. Commented Apr 20, 2013 at 15:39
  • Im not sure what that means? Commented Apr 20, 2013 at 15:43
  • what is the output you are expecting ? an object or array ? Commented Apr 20, 2013 at 15:43

2 Answers 2

3

So as it wasn't clear exactly what was required. This should answer both possibilities that I think you could be asking.

var userArray = ["s", "a", "m", "a", "n", "t", "h", "a", "w", "h", "i", "t", "e", "l", "o", "v", "e", "s", "J", "a", "c", "k", "h", "a", "r", "r", "i", "s"];

function unique(array) {
    var unique = [];

    array.forEach(function (value) {
        if (unique.indexOf(value) === -1) {
            unique.push(value);
        }
    });

    return unique;
}

function count(array) {
    var obj = {};

    array.forEach(function (value) {
        obj[value] = (obj[value] || 0) + 1;
    });

    return obj;
}


console.log(userArray);
console.log(unique(userArray));
console.log(count(userArray));

on jsfiddle

Sign up to request clarification or add additional context in comments.

2 Comments

The way the game works is it takes the letters of two peoples names. For example: sara love martin, then is counts the number of times the letter appears and adds it to a new array. The first and last values of that array will be added and you will continue to add the numbers inward till moving them to a new array and take those values and do the same until there is only two numbers
those numbers are the percentage of love
2

I am not sure whether you need to get key value pairs. You can try something as given below to get an object of letter-count pairs.

function trueLoveGame(value){


    var nums={};

    for(var i =0; i < value.length;i++){
       var letter = value[i];
       nums[letter] =   nums[letter] || 0;
       nums[letter]++;
    }

    return nums;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.