我正在嘗試創建一個電子郵件生成器(供個人使用),它需要輸入名和姓,然後使用我創建的數組中的域名列表。 從這個site得到了這個想法,併爲我所需要的調整了一下。電子郵件生成器(Javascript)
下面是示例代碼。
function permutate(form) {
var firstlastname = form.firstlastname.value.toLowerCase().trim();
var namesplit = firstlastname.split(" ");
var firstname = namesplit[0];
var lastname = namesplit[1];
var domain = ['gmail.com','yahoo.com','hotmail.com','msn.com','outlook.com','live.com','me.com','aol.com','mac.com'];
if (firstname==null || firstname==""){
document.getElementById("results").innerHTML= "At least enter a first name to get started!";
return false;
} else if (lastname==null || lastname==""){
document.getElementById("results").innerHTML= "Come on!, enter a last name!";
return false;
} else {}
for (var i=0; i<domain.length;i++)
var p1 = firstname + "@" + domain[i] + ",";
var p2 = firstname + lastname + "@" + domain[i] + ",";
var p3 = firstname + "." + lastname + "@" + domain[i] + ",";
var p4 = firstname.charAt(0) + lastname + "@" + domain[i] + ",";
var p5 = firstname.charAt(0) + "." + lastname + "@" + domain[i] + ",";
var p6 = firstname + lastname.charAt(0) + "@" + domain[i] + ",";
var p7 = firstname + "." + lastname.charAt(0) + "@" + domain[i] + ",";
var p8 = firstname.charAt(0) + lastname.charAt(0) + "@" + domain[i] + ",";
var p9 = firstname + "_" + lastname + "@" + domain[i] + ",";
var p10 = firstname.charAt(0) + "_" + lastname + "@" + domain[i] + ",";
var p11 = lastname + firstname + "@" + domain[i] + ",";
var p12 = lastname + "." + firstname + "@" + domain[i] + ",";
var p13 = lastname + firstname.charAt(0) + "@" + domain[i] + ",";
var p14 = lastname + "." + firstname.charAt(0) + "@" + domain[i];
var addresses= p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 + p11 + p12 + p13 + p14;
document.getElementById("results").innerHTML= addresses;
}
結果將只打印陣列上的最後一個值始終是 「mac.com」 所以這將是「VB @ mac.com,VB @ mac.com,VB @ mac.com .. 」
我想,它會顯示一個類似
[email protected],[email protected],[email protected]
[email protected],[email protected],[email protected]
[email protected],[email protected],[email protected]
等所有可能的組合..
我是不是做正確,或者是有什麼我需要添加或能不能做到?
我一直在敲我的腦袋,因爲我剛剛學習了編碼,需要專業人士的指導。
我正在考慮做很長的路,但我想我會有一個很長的代碼。
firstname + "@" + "gmail.com" + ",";
firstname + lastname + "@" + "yahoo.com" + ","
編輯:再次從頭開始,將根據以下評論繼續採用不同的方法。
您需要'{...}'圍繞'for'循環的內容... –
我推薦使用數組而不是編號變量。然後你可以使用'addresses.join(「,」)'來組合它們。 – 4castle
也許http://stackoverflow.com/questions/38471657/word-combination-in-an-array-using-javascript有點幫助。 –