2017-01-28 101 views
0

我正在嘗試創建一個電子郵件生成器(供個人使用),它需要輸入名和姓,然後使用我創建的數組中的域名列表。 從這個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" + "," 

編輯:再次從頭開始,將根據以下評論繼續採用不同的方法。

+0

您需要'{...}'圍繞'for'循環的內容... –

+1

我推薦使用數組而不是編號變量。然後你可以使用'addresses.join(「,」)'來組合它們。 – 4castle

+0

也許http://stackoverflow.com/questions/38471657/word-combination-in-an-array-using-javascript有點幫助。 –

回答

0

我試了一下,得到這個爲result。你可以在這裏看到的代碼:

HTML:

<form name="names"> 
    <input type="text" id="box1" name="firstlastname" placeholder="Luke Skywalker"><br> 
<br> 
    <input type="submit" id="button-make" value="Make Variations" onClick="permutate(this.form);return false;"> 
</form> 

<input type="submit" id="button-reset" value="Reset" onClick="reset('results');return false;"> 

<div id="results"></div> 

CSS:

#form { 
    margin-top: 20px; 
} 

input[type] { 
    font-size: 25px; 
    font-family: arial; 
} 

#box1 { 
    width: 317px; 
    padding: 10px 12px; 
    margin-bottom: 10px; 
    border: 1px solid rgb(200, 200, 200); 
    border-top-color: rgb(100, 100, 100); 
    border-radius: 5px; 
    color: #5A5A5A; 
} 

#button-reset { 
    position: relative; 
    display: inline-block; 
} 

#button-make, 
#button-reset { 
    float: left; 
} 

#button-reset { 
    margin: 0 0 0 10px; 
} 

#results { 
    position: relative; 
    height: 200px; 
    display: block; 
    overflow: auto; 
    margin-top: 45px; 
    padding: 15px; 
    border: 1px solid rgb(200, 200, 200); 
    /*border-bottom: 1px solid rgb(200,200,200);*/ 
    background: rgb(245, 245, 245); 
    color: rgb(50, 50, 50); 
    font-size: 20px; 
    line-height: 36px; 
    font-family: arial; 
} 

的javascript:

function permutate(form) { 

    var firstlastname = form.firstlastname.value.toLowerCase().trim(); 
    var namesplit = firstlastname.split(" "); 
    var firstname = namesplit[0]; 
    var lastname = namesplit[1]; 
    var arr = []; 
    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 = i + 1) { 
     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; 
     arr.push(addresses) 
    } 
    } 
    var innertext = document.getElementById("results"); 
    innertext.innerHTML = arr; 
} 

我改變了{}的其他部分和IF環和添加了一個包含結果的數組。然後我使用push函數來填充我的數組,並且最後一步是將innerHTML設置爲數組的內容

+0

謝謝。不能Upvote,但不勝感激。 – Jaren