dimanche 29 mars 2015

better way to write if else statement

First time writing Javascript. I just would like know if there is a shorter way of writing this:



<p id="demo"></p>

<script>
function myFunction() {
var letter = document.getElementById("myInput").value;
var text;

if (letter === "5544") {
text = "Abar, Marlon 1,800";

} else if (letter === "5545") {
text = "Pia, Darla 1,800";

} else if (letter === "5546") {
text = "Salazar, Alex 1,500";

//etc...

} else {
text = "Incorrect Account Number";
}
document.getElementById("demo").innerHTML = text;
}
</script>


Tried map but I couldn't get it to work.


Aucun commentaire:

Enregistrer un commentaire