mercredi 12 février 2020

Coding a quiz in JAVASCRIPT that returns movies to watch on Netflix

I am trying to learn objects at the moment. So I have five different variables that are set to zero and will increment by 1 each time it is clicked on in the quiz. (I am creating a quiz to determine what movie or show you should watch on Neflix. )

var answer = {
    a:0,
    b:0,
    c:0,
    d:0,
    e:0
};


function selection (choice){
    answer[choice]++
    console.log(answer)
    console.log(choice)
};

However, my trouble is returning the object with the greatest amount, and once I do that then I return a movie name based on what object is returned. I tried to do two functions...

function game(){
    var biggest = 'a'
    for(var choice in answer)
        if(answer[choice]>answer[biggest]){
            biggest=choice
            console.log(biggest)
        }
        return biggest;

};

the function above is a lot more cleaner than the bottom one but doesn't have the movie titles.

function submitt () {

    for(obj in answer)
        if(a===2){
            return 'Hobbs and Shaw'
        }else if(a===1 && b===1){
            return 'Jumanji'
        }else if(a===1 && c===1){
            return 'The Invitation'
        }else if(a===1 && d===1){
            return 'Incredibles 2'
        }else if(a===1 && e===1){
            return 'Someone Great'
        };
        if(b===2){
            return 'The Good Place'
        }else if(b===1 && c===1){
            return 'Black Mirror'
        }else if(b===1 && d===1){
            return 'Liv and Maddie'
        }else if(b===1 && e==1){
            return 'Jane the Virgin'
    }
};

My question is, how can i define an object in my if statement, because the error I get is "a is not defined". How do I define the objects?

Aucun commentaire:

Enregistrer un commentaire