LU05.L03 - while-loop pur

Auftrag

Lösung

// Autor: Volkan Demir
// Assignment: LU05.A03.js - while loops & arrays
// Date: 28.10.25
// Desc: Managing a row of numbers, stored in an array
 
const zahlen = [1, 6, 7, 2, 8, 4, 9, 69, 3]; // Establishing the array
 
let zlen = zahlen.length;
let i = 0; // Startwert des Schleifenzählers
let sum = 0;
let text = ""; // Meldung wird hier zusammengebaut
 
 
console.clear();
console.log("Zahlenarray: " + zahlen); // Just for Info
console.log("----------------------------------");
 
while (i < zlen) { // Soll laufen, solange i kleiner ist als die Länge des Arrays (Start bei 0)
    text = "Summe von " + sum + " + " + zahlen[i] + " = ";
    sum += zahlen[i];
    text += sum;
   console.log(text);
    i++; // Hochzählen nicht vergessen, sonst habe ich wieder eine Endlosschleife produziert.
}
 
// Challenge
i = 0; // i muss resettet werden auf Startwert
let prod = 1; // Achtung! Neutrales Element der Multiplikation ist 1
while (i < zlen) { // Soll laufen, solange i kleiner ist als die Länge des Arrays (Start bei 0)
    text = "Produkt von " + prod + " * " + zahlen[i] + " = ";
    prod *= zahlen[i];
    text += prod;
    console.log(text);
    i++; // Hochzählen nicht vergessen, sonst habe ich wieder eine Endlosschleife produziert.
}

Volkan Demir