Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

Javascript function not activating in the if-statement

$
0
0

I'm trying to code a html site that with Javascript should work basically as akinator (bot who guesses a character thanks to the user who answers yes or no to the questions) and I wanted to activate with a button different function for each value of a counter variable (a).

let personaggi = [    EnzoFerrari = {        consider: true,        nome: "Enzo Ferrari",        campo: "Ingegneria",        vivo: false,        nobel: 0    },    AlbertEinstein = {        consider: true,        nome: "Albert Einstein",        campo: "Scienza",        vivo: false,        nobel: 1    },    SamanthaCristoforetti= {        consider: true,        nome: "Samantha Cristoforetti",        campo: "Scienza",        vivo: true,        nobel: 0    }]let dmdGenerali = ["Il tuo personaggio è dell'ambito della ","Il tuo personaggio è ancora vivo?","Il tuo personaggio ha vinto dei nobel?",]let testo = document.getElementById("output")let steam = ["Scienza", "Tecnologia", "Ingegneria", "Arte", "Matematica"]let vero = document.getElementById("vero")let falso = document.getElementById("falso")let a=0testo.innerText = dmdGenerali[0] + steam[a] +"?"function ambitoCorretto(){    a=5    testo.innerText = dmdGenerali[1]    return a}function ambitoSbagliato(){    a++    testo.innerText = dmdGenerali[0] + steam[a] +"?"    return a}function vivoCorretto(){    a=6    testo.innerText = dmdGenerali[2]    return a}function vivoSbagliato(){    a=6    testo.innerText = dmdGenerali[2]    return a}if (a<5){    vero.onclick = ambitoCorretto    falso.onclick = ambitoSbagliato}if(a==5){    vero.onclick = vivoCorretto    falso.onclick = vivoSbagliato}

I tried using different ways to increment another counter variable, like using one to mark the end of the first type of question, but nothing worked.I keep getting the first question (dmdGenerali[0]) instead of the third (dmdGenerali[2]) after I answer "No" to the second question, whereas if I answer "Yes" (Sì) to the second question nothing happens/it keeps asking the second one.


Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>