Bonjour,

Voila je rencontre un petit problème avec mon code.

Je m'amuse (avec du codage relativement simple), à faire un question reponse, donc je fais tout mon code et puis ça marche, puis le lendemain je rajoute un "case" (dans le switch{d}) et ça marche plus, ça ne trouve plus la fonction "myFunction()"

<!DOCTYPE html>
<html>
<body>

  <p>Question / Reponse</p>

**  <button onclick="myFunction()">Click</button>around around  
<p id="demo"></p>around around  
<script type="text/javascript">aroundaround    
   function myFunction() {**

      var x;
      var d = Math.floor(Math.random() * 20) + 1;

      switch (d) {
      case 1:
        var answer = prompt("How to say 'une unite centrale' in english?","")
        var good = "a main frame"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 2:
        var answer = prompt("How to say 'un pave numerique' in english?","")
        var good = "a numeric keypad"
        var elgood = "a numpad"
        if (answer == good)
            x = "Bonne reponse, mais on peut aussi dire : " + "<strong>" + elgood + "</strong>"
        else if (answer == elgood)
            x = "Bonne reponse, mais on peut aussi dire : " + "<strong>" + good + "</strong>"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>, ou bien : <strong>" + elgood + "</strong>"
        break;
      case 3:
        var answer = prompt("How to say 'un tapis de souris' in english?","")
        var good = "a mouse pad"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;    
      case 4:
        var answer = prompt("What means CPU ?","")
        var good = "Central Processing Unit"
        var elgood = "un processeur"
        var elelgood = "central processing unit"
        if (answer == good)
            x = "Bonne reponse, mais on peut aussi dire en francais : " + "<strong>" + elgood + "</strong>"
        else if (answer == elgood)
            x = "Bonne reponse, mais on peut aussi dire : " + "<strong>" + good + "</strong>"
        else if (answer == elelgood)
            x = "Bonne reponse, mais on peut aussi dire en francais: " + "<strong>" + elgood + "</strong>"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>, ou bien en francais : <strong>" + elgood + "</strong>"
        break;
      case 5:
        var answer = prompt("How to say 'la carte mere' in english?","")
        var good = "the motherboard"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 6:
        var answer = prompt("How to say 'une barrette de memoire' in english?","")
        var good = "a memory module"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 7:
        var answer = prompt("What's a byt (in french)","")
        var good = "un octet"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 8:
        var answer = prompt("How to say 'un disque dur' in english?","")
        var good = "a hard drive"
        var elgood = "a hard disk"
        if (answer == good)
            x = "Bonne reponse, mais on peut aussi dire : " + "<strong>" + elgood + "</strong>"
        else if (answer == elgood)
            x = "Bonne reponse, mais on peut aussi dire : " + "<strong>" + good + "</strong>"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>, ou bien : <strong>" + elgood + "</strong>"
        break;
      case 9:
        var answer = prompt("How to say 'une cle USB' in english?","")
        var good = "a USB flash drive"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 10:
        var answer = prompt("How to say 'une cartouche d'encre' in english?","")
        var good = "an ink cartridge"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 11:
        var answer = prompt("How to say 'un graveur de CD' in english?","")
        var good = "a CD recorder"
        var elgood = "a CD burner"
        if (answer == good)
            x = "Bonne reponse, mais on peut aussi dire : " + "<strong>" + elgood + "</strong>"
        else if (answer == elgood)
            x = "Bonne reponse, mais on peut aussi dire : " + "<strong>" + good + "</strong>"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>, ou bien : <strong>" + elgood + "</strong>"
        break;
      case 12:
        var answer = prompt("How to say 'un peripherique' in english?","")
        var good = "a peripheral device"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 13:
        var answer = prompt("How to say 'un systeme d'exploitation' in english?","")
        var good = "an operating system"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 14:
        var answer = prompt("How to say 'un raccourci' in english?","")
        var good = "a shorcut"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 15:
        var answer = prompt("How to say 'la police de charactere' in english?","")
        var good = "the font"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 16:
        var answer = prompt("How to say 'un tableur' in english?","")
        var good = "a spreadsheet software"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 17:
        var answer = prompt("How to say 'un marque page' in english?","")
        var good = "a bookmark"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 18:
        var answer = prompt("How to say 'un founir d'acces internet' in english?","")
        var good = "an internet service provider"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 19:
        var answer = prompt("How to say 'haut débit' in english?","")
        var good = "broadband"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;
      case 20:
        var answer = prompt("How to say "J'adore internet" in english ?,"")
        var good = "I love the internet"
        if (answer == good)
            x = "Bonne reponse"
        else
            x = "Mauvaise reponse, la bonne reponse etait : " + "<strong>" + good + "</strong>"
        break;

      }

      document.getElementById("demo").innerHTML = x;
    }
  </script>

</body>
</html>

Ce que je veux

Ne plus avoir d'erreur

Ce que j'obtiens

"ReferenceError: myFunction is not defined"

1 réponse


Stead
Réponse acceptée

Il suffisait de relire ton poste et tu aurai probablement trouvé l'erreur en suivant la coloration:

1) function myFunction() { Ici dans ton code il y a deux astérisques (qui ne devrait pas s'y trouver)

2) var answer = prompt("How to say "J'adore internet" in english ?,"") Ici tu as visiblement un problème de quote.

--> prompt('How to say "J\'adore internet" in english ?',"") devrait fonctionner.
Voilà.