Nie przypisuje się wartość w pętli

0

Cześć,

jestem początkujący w js (kiedyś coś robiłem, ale to upadło przez brak praktyki). Chciałem napisać prosty program w JS, który będzie mi generował maile na podstawie wylosowanego wcześniej imienia i nazwiska. Nie chciałbym jednak, aby ten mail się powtarzał jak wylosuje się takie same imię i nazwisko oraz domena i ma wtedy dodawać kolejną liczbę za nazwiskiem począwszy od 1. Przykładowo:

  1. [email protected]
  2. [email protected]

Tak wygląda mój kod:

const gender = ['m' ,'k'];
const maleNames = ['Bartosz', 'Bartłomiej', 'Krzysztof', 'Tomasz'];
const femaleNames = ['Katarzyna', 'Joanna', 'Barbara'];
const lastNames = ['Nowak', 'Kowalski', 'Waniorek'];
const domen = ['gmail.com', 'onet.pl', 'wp.pl', 'o2.pl'];
const people = [];
const mail = [];
const minAge = 15; // minimalny wiek
const mailNumber = 0;


function randChoice(arr) {
    const arrLength = arr.length
    //console.log('arrLength: ', arrLength)
    const randomNumber = Math.floor(Math.random() * arrLength)
    //console.log('randomNumber: ', randomNumber)
    const randomElem = arr[randomNumber]
    //console.log('randomElem: ', randomElem)
    return randomElem
}

function randName(arr) {
    if (arr == 'k') {
        const choiceName = randChoice(femaleNames);
        //console.log('choiceName: ', choiceName);
        return choiceName;
    }
    if (arr == 'm') {
        const choiceName = randChoice(maleNames);
        //console.log('choiceName: ', choiceName);
        return choiceName;
    }
    else {
        return 'błąd płci'
    }
}

function randAge() {
    const randAge = Math.floor(Math.random() * 50 + minAge);
    //console.log('randomNumber: ', randAge);
    return randAge
}

function mailVeryfication(choiceName, choiceLastName, domen, mail, mailNumber) {
    const choiceDomen = randChoice(domen);
    const choiceMail = choiceName.toLowerCase() + '.' + choiceLastName.toLowerCase() + '@' + choiceDomen;
    console.log('mailNumber 1:', mailNumber);
    //console.log('mail in function: ', mail);
    if (mail.indexOf(choiceMail) == -1) {
        const choiceMailWithNumber = [choiceMail, mailNumber]
        console.log('choiceMailWithNumber 2: ', choiceMailWithNumber);
        return choiceMailWithNumber
    }
    else {
        const mailNumberNew = mailNumber + 1;
        const choiceMail = choiceName.toLowerCase() + '.' + choiceLastName.toLowerCase() + mailNumberNew + '@' + choiceDomen;
        const choiceMailWithNumber = [choiceMail, mailNumberNew]
        console.log('choiceMailWithNumber 3: ', choiceMailWithNumber);
        return choiceMailWithNumber
    }
}

for (let i=0; i<20; i++) {
    console.log('--------------');
    const choiceGender = randChoice(gender);
    console.log('choiceGender: ', choiceGender);

    const choiceName = randName(choiceGender);
    console.log('choiceName: ', choiceName);
    
    const choiceLastName = randChoice(lastNames);
    console.log('choiceLastName: ', choiceLastName);

    const choiceAge = randAge();
    console.log('choiceAge: ', choiceAge);

    console.log('mailNumber 0:', mailNumber);
    const choiceMailAllInfo = mailVeryfication(choiceName, choiceLastName, domen, mail, mailNumber);
    
    const choiceMail = choiceMailAllInfo[0];
    console.log('mailVeryfication: ', choiceMail);

    const mailNumber = choiceMailAll[1];
    console.log('mailNumber 4: ', choiceMailAllInfo[1]);

    mail.push(choiceMail);

    people.push({
        name: choiceName,
        lastName: choiceLastName,
        gender: choiceGender,
        age: choiceAge,
        mail: choiceMail,
    });

}
console.log('people: ', people);
console.log('mail: ', mail);

jednak dostaje taki błąd, jakby ta wartość nie istniała, ale jest. Jak to poprawić oraz dlaczego tak jest?

screenshot-20200825125411.png

1

Ale ten JS poryty. Nie jestem ekspertem, ale usuń const w linii const mailNumber = choiceMailAll[1];
BTW const służy do definiowania stałych a nie zmiennych. Główną cechą stałych jest to że się nie zmieniają (w przeciwieństwie do zmiennych). Więc jeszcze linie const mailNumber = 0; będziesz musiał zmienić na let mailNumber = 0; oraz pewnie kilka innych linii

0

Dziękuję :-) Działa :-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1