• Das Erstellen neuer Accounts wurde ausgesetzt. Bei berechtigtem Interesse bitte Kontaktaufnahme über die üblichen Wege. Beste Grüße der Admin

[FRAGE] Array durchsuchen

lll

New member
HALLO, ich würde gerne ein Array durchsuchen mithilfe von while und if/else.. ist das möglich, wenn ja wie?

bin relativ neu dabei.. und folgendes habe ich mir überlegt.. das einzige Problem sehe ich momentan darin, dass keine Ausgabe kommt, sobald die Eingabe nicht im Array gespeichert ist. Ok es kommt ein, aber nur konkret auf diesen Fall bezogen. Sind in dem Array mehr Werte gespeichert, müsste der Code in Zeile 16 angepasst werden. Wie kann ich das umgehen: bzw. wie kann ich mein Programm allgemeiner schreiben? Einer Tipps, Anregungen oder derartiges? Das wäre sehr nett.. Gibt es sowas wie: 'sobald keine der eingegebenen Werte im Array gespeichert ist, dann..' etwa: if ( c != a[d 0 - d a.length]) .. siehe Zeile 16 .. Danke schonmal

Code:
1      let a = new Array (2,3,4,5,6,7);
2      let b = a.length;
3      let d = 0;
4      let c = prompt ("Bitte geben Sie ein Zahl ein: ", "");
5
6      if(c==a[d]){
7		document.write(c + ' ist im Array gespeichert.');
8	}
9	else if(c!=a[d]){
10		while(d<b){
11			d++;
12			if(c==(a[d])){
13				document.write(c + ' ist im Array gespeichert.');			
14			}
15		}
16		if(c!=a[0] && c!=a[1] && c!=a[2] && c!=a[3] && c!=a[4] && c!=a[5]){
17			document.write('haha');
18		}
19	}
 
Zuletzt bearbeitet von einem Moderator:
Entweder soll ausgegeben werden 'Die Eingabe ist im Arrays gespeichert' oder eben 'Die Eingabe ist nicht im Array gespeichert' :)
 
ich würde gerne ein Array durchsuchen mithilfe von while und if/else..
Warum so spezifisch?

Code:
let storage = [2,3,4,5,6,7];
let value = parseInt(prompt ("Bitte geben Sie ein Zahl ein: ", ""), 10);
if (storage.indexOf(value) !== -1){
	alert(value + ' ist im Array gespeichert.');
}
else{
	alert('haha');
}

Zu document.write:
http://www.w3.org/html/wg/drafts/html/CR/webappapis.html#dynamic-markup-insertion schrieb:
This method has very idiosyncratic behavior. In some cases, this method can affect the state of the HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document (e.g. if the string written is the string "<plaintext>" or "<!--"). In other cases, the call can clear the current page first, as if document.open( ) had been called. In yet more cases, the method is simply ignored, or throws an exception. To make matters worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.
 
Zurück
Oben