0

I have to count the occurrence between two lists of strings. I need this function to complete my mastermind game. The problem is that I manage to count the occurrences but they are duplicated because of course you can't delete an element from a list by browsing it, otherwise, it's out of range.

These are made for the helper on the mastermind game of course.

Thanks for any help :)

Good day

I have for example :

answer = ["red","black","blue","green"]
playerHits = ["blue","blue","blue","black"]

The output should be a list like this :

helper = [null, null]

What i've tried and of course not working :

let cpPlay= [[null,null],[null,null],[null,null],[null,null]];

let cpAnswer= [[null,null],[null,null],[null,null],[null,null]];

for(let i = 0 ; i < play.length ; i++){
    for(let j = 0 ; j < answer.length ; j++){
        console.log(cpPlay.length);
        console.log(cpAnswer.length);

        if((cpPlay[i][0] === cpAnswer[j][0]) && (cpPlay[i][1] === cpAnswer[j][1])){
            helper.push(true);
            cpPlay = cpPlay.slice(i)
            cpAnswer = cpAnswer.slice(j)
            console.log('win')

        }
        else if((cpPlay[i][0] === cpAnswer[j][0]) && (cpPlay[i][1] !== cpAnswer[j][1])){
            helper.push(null);
            cpPlay = cpPlay.slice(i)
            cpAnswer = cpAnswer.slice(j)
            console.log('almost')
        }

}
}

1 Answers1

0

I tried that :

And it's not working too :

const almostWin = () => {
    let colorLastVisitedIndex = [['blue',0],['green',0],['yellow',0],['violet',0],['red',0],['black',0]]
    let AnswerShort = splitForColor(short(answer))
    let PlayShort  = splitForColor(short(play))
    for(let i = 0 ; i < AnswerShort.length ; i++){
        if(PlayShort[i] === AnswerShort[i]){
            helper.push(true);
        }
        else{
            let lastIndex = findIndexOfColor(colorLastVisitedIndex, AnswerShort[i])
            for(let j = lastIndex ; j < PlayShort.length ; j++){
                if(PlayShort[j] === AnswerShort[i]){
                    helper.push(null);
                    colorLastVisitedIndex[findIndexOfColor(colorLastVisitedIndex, AnswerShort[i][0])] = j;
                }
            }
        }
    }
    return helper;
}