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:
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?