Why is this switch returning undefined?

<p> Traductor Pallarès - Català </p>
<input type="text" id="paraula">
<button id="boto"> Tradueix </button>
<p id="sortida"></p> 

<script>
document.getElementById("boto").addEventListener("click", resultat);
var paraula;
function resultat (){
       paraula=document.getElementById("paraula").value;
       var result;
       switch("paraula"){
             case "a bocafoscant":
             result="quan es comença a fer fosc";
             break;
             case "abidriar":
             result="veure una cosa des de lluny";
             break;
             case "adés":
             result="fa poc, fa un moment";
             break;
             case "aïna":
             result="qualsevol utensili de cuina";
             break;
             case "aixalda":
             result="espurna que salta del foc";
             break;
           }
     document.getElementById("sortida").innerHTML = result;
    }

</script>

In your eventListener, click only has a opening ", it also needs a closing ".

1 Like

Many thanks for your reply! i did this mistake here when posting it in codeacademy but i havent missed this " in the code. This is not the source of the problem…

Found it! Your switch uses a string “paraula” as input value whilst it should be the variable paraula ;).

<script>
    document.getElementById("boto").addEventListener("click", resultat);
    var paraula;
    function resultat (){
           paraula=document.getElementById("paraula").value;
           var result;
           switch(paraula){
                 case "a bocafoscant":
                 result="quan es comença a fer fosc";
                 break;
                 case "abidriar":
                 result="veure una cosa des de lluny";
                 break;
                 case "adés":
                 result="fa poc, fa un moment";
                 break;
                 case "aïna":
                 result="qualsevol utensili de cuina";
                 break;
                 case "aixalda":
                 result="espurna que salta del foc";
                 break;
               }
         document.getElementById("sortida").innerHTML = result;
        }

    </script>

you are right! it was using paraula as string instead of using it as a variable!thanks!
nevertheless, now i dont get undefined but it still wont work, it doesnt give me the corresponding word (if i look for abidriar it should return veure una cosa des de lluny)
I have put this line at the end of the code but not working either:
document.getElementById(“boto”).addEventListener(“click”, resultat);

sorry, its working!! MANY THANKS!!
this is the result:

var paraula;

function resultat() {
	
	paraula = document.getElementById("paraula").value;

	var result;

	switch (paraula){
		case "a bocafoscant": 
		result = "quan es comença a fer fosc";
		break;
		case "abidriar":
		result = "veure una cosa des de lluny";
		break;
		case "adés": 
		result = "fa poc, fa un moment";
		break;
		case "aïna": 
		result = "qualsevol utensili de cuina";
		break;
		case "aixalda":
		result = "espurna que salta del foc";
		break;
	}
document.getElementById("sortida").innerHTML= result;

}

document.getElementById(“boto”).addEventListener(“click”, resultat);