76 lines
3.0 KiB
HTML
76 lines
3.0 KiB
HTML
<!doctype html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Übung 21: Pokémon — Evolution</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<div class="container py-5">
|
|
<div class="alert alert-primary">
|
|
<h2>Übung 21: Pokémon — Evolution</h2>
|
|
<p>
|
|
Kennst du Pokémon? Allen Pokémon-Spielen ist gemein, dass es Pokémon (Phantasiewesen) gibt, die über
|
|
verschiedene Evolutionsstufen verfügen. Ein Pokémon kann sich bis zu zweimal weiterentwickeln. D. h. es gibt
|
|
maximal drei Stufen: seine Anfangsstufe und bis zu zwei Folgestufen. Jede dieser Stufen trägt einen neuen
|
|
Pokémon-Namen — keiner dieser Namen kann doppelt vorkommen. Einen guten Überblick über die Stufen bei
|
|
Pokémon GO findest du auf <a href="https://pokemondb.net/evolution">https://pokemondb.net/evolution</a>.
|
|
</p>
|
|
<ol class="list">
|
|
<li>
|
|
Schreibe eine Funktion <code>stagesFor</code>, der du eine Pokémon-Evolutionsstufe übergibst und die dir
|
|
alle dazugehörigen Evolutionsstufen als Array zurückliefert. Als Daten verwendest du exemplarisch die
|
|
Evolutionsstufen von Pidgey, Vulpix und Dratini.
|
|
</li>
|
|
<li>
|
|
Entwickle die Funktionen <code>stagesAfter</code> und <code>stagesBefore</code>, die dir nur die Stufen
|
|
vor bzw. nach der angefragten Stufe zurückliefern.
|
|
</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
<script>
|
|
'use strict';
|
|
|
|
const evolutionStages = [
|
|
['Pidgey', 'Pidgeotto', 'Pidgeot'],
|
|
['Vulpix', 'Ninetales'],
|
|
['Dratini', 'Dragonair', 'Dragonite'],
|
|
];
|
|
|
|
const stagesFor = (pokemon) => {
|
|
const stages = evolutionStages.find((stages) => stages.includes(pokemon));
|
|
|
|
if (!stages) {
|
|
console.warn('stages not found');
|
|
return [];
|
|
}
|
|
|
|
return stages; // wenn unfedined leeres array zurückgeben
|
|
};
|
|
|
|
const stagesAfter = (pokemon) => {
|
|
const stages = stagesFor(pokemon);
|
|
|
|
return stages.slice(stages.indexOf(pokemon) + 1);
|
|
};
|
|
|
|
const stagesBefore = (pokemon) => {
|
|
const stages = stagesFor(pokemon);
|
|
return stages.slice(0, stages.indexOf(pokemon));
|
|
};
|
|
|
|
console.log(stagesFor('Vulpix')); // => [ 'Vulpix', 'Ninetales' ]
|
|
console.log(stagesAfter('Anton')); // =>
|
|
console.log(stagesAfter('Dratini')); // => [ 'Dragonair', 'Dragonite' ]
|
|
console.log(stagesBefore('Pidgeot')); // => [ 'Pidgey', 'Pidgeotto' ]
|
|
console.log(stagesBefore('Dragonair')); // => [ 'Dratini' ]
|
|
console.log(stagesBefore('Pidgey')); // => [ ]
|
|
console.log(stagesBefore('Pidgeot')); // => ['Pidgey', 'Pidgeotto']
|
|
</script>
|
|
</body>
|
|
</html>
|