Beruflich Dokumente
Kultur Dokumente
2015
Thematik: JavaScript
Protokollant: J.R.
Zu Beginn der Informatik-Stunde am 20. Januar 2015 stellte ein Schler des
Kurses eine vom ihm selbst ausgearbeitete JavaScript-Datei vor, welche sich
wie folgt darstellt:
Wird eine Eingabe gemacht, gibt die JavaScript-Datei einen Text aus, je
nachdem ob die geforderte Eingabe erfllt (richtig) oder nicht erfllt
(falsch) wurde.
<!DOCTYPE html>
<html>
<head>
<title> Geo Quiz </title>
</head>
<body>
<script>
function check(country, capital)
{
if (countries.indexOf(country) === capitals.indexOf(capital))
// array.indexOf(element) gibt die Position eines Elements zurck
{
return true;
}
else
{
return false;
}
}
function randomCountry()
{
return countries[Math.floor(Math.random() * countries.length)];
// Math.floor(Math.random() * 5) --> Zufllige Ganzzahl zwischen 0 und 4
};
var countries = ["deutschland", "frankreich", "england", "belgien",
"niederlande"],
capitals = ["berlin", "paris", "london", "brssel", "amsterdam"],
rounds = 0,
correct = 0;
do
{
var country = randomCountry();
if (check(country, prompt("Was ist die Hauptstadt von " + country +
"?")))
{
alert("Gut gemacht!");
correct++;
}
else
{
alert("Leider falsch.");
}
rounds++;
}
while(confirm("Nochmal spielen?"))
alert("Von " + rounds + " Versuchen waren " + correct + " richtig.");
</script>
</body>
</html>