2013-05-30 77 views
1

我在最後一個JavaScript類的學期,通過它,現在 我在暑假。我們使用的書是JavaScript第8版,Tom Negrino, Dori Smith。有一個名稱發生器的代碼。所有需要的東西都在 (第301頁)這本書中,它需要名和姓,但我想添加一箇中間名 來輸出另一個愚蠢的名字。這不是一項家庭作業,只是我想嘗試的東西 。如果您將姓氏或名字留空,您將收到「這是 不是有效的名稱」消息。所以我編寫了中間名,爲 輸入了26個字的中間名,但代碼除了第一個 和姓以外都沒有識別出任何內容。對我來說,它看起來應該可以工作,但仍然只有第一個 和姓氏工作。也許另一雙眼睛可以幫助我看看 我錯過了或沒有包括在內。Javascript「愚蠢的名字生成器」

感謝您的任何建議,CK

The HTML 
<!DOCTYPE html> 
<html> 
<head> 
<title>Silly Name Generator</title> 
<script src="script05.js"></script> 
</head> 
<body> 
<h1>What's your silly name?</h1> 
<table> 
<tr> 
    <td class="rtAlign">First Name:</td> 
    <td><input type="text" id="fName" size="30"></td> 
</tr> 
<tr> 
    <td class="rtAlign">Middle Name:</td> 
    <td><input type="text" id="mName" size="30"></td> 
</tr> 
<tr> 
    <td class="rtAlign">Last Name:</td> 
    <td><input type="text" id="lName" size="30"></td> 
</tr> 
<tr> 
    <td>&nbsp;</td> 
    <td><input type="submit" value="Submit" id="sillySubmit"> 
</tr> 
</table> 
<p id="msgField">&nbsp;</p> 
</body> 
</html> 

中的JavaScript

window.onload = initAll; 

function initAll() { 
document.getElementById("sillySubmit").onclick = function() { 
    document.getElementById("msgField").innerHTML = getSillyName(); 
    return false; 
} 
} 

function getSillyName() { 
var firstName = ["Runny", "Buttercup", "Dinky", "Stinky", "Crusty", 
"Greasy","Gidget", "Cheesypoof", "Lumpy", "Wacky", "Tiny", "Flunky", 
"Fluffy", "Zippy", "Doofus", "Gobsmacked", "Slimy", "Grimy", "Salamander", 
"Oily", "Burrito", "Bumpy", "Loopy", "Snotty", "Irving", "Egbert"]; 

var middleName =["Waffer", "Lilly","Rugrat","Sand", "Fuzzy","Kitty", 
"Puppy", "Snuggles","Rubber", "Stinky", "Lulu", "Lala", "Sparkle", "Glitter", 
"Silver", "Golden", "Rainbow", "Cloud", "Rain", "Stormy", "Wink", "Sugar", 
"Twinkle", "Star", "Halo", "Angel"]; 

var lastName1 = ["Snicker", "Buffalo", "Gross", "Bubble", "Sheep", 
"Corset", "Toilet", "Lizard", "Waffle", "Kumquat", "Burger", "Chimp", "Liver", 
"Gorilla", "Rhino", "Emu", "Pizza", "Toad", "Gerbil", "Pickle", "Tofu", 
"Chicken", "Potato", "Hamster", "Lemur", "Vermin"]; 
var lastName2 = ["face", "dip", "nose", "brain", "head", "breath", 
"pants", "shorts", "lips", "mouth", "muffin", "butt", "bottom", "elbow", 
"honker", "toes", "buns", "spew", "kisser", "fanny", "squirt", "chunks", 
"brains", "wit", "juice", "shower"]; 

var firstNm = document.getElementById("fName").value.toUpperCase(); 
var middleNm = document.getElementById("mName").value.toUpperCase(); 
var lastNm = document.getElementById("lName").value.toUpperCase(); 
var validName = true; 

if (firstNm == "") { 
    validName = false; 
} 
else { 
    var firstNum = firstNm.charCodeAt(0) - 65; 

    if (firstNum < 0 || firstNum > 25) { 
     validName = false; 
    } 
} 

if (!validName) { 
    document.getElementById("fName").focus(); 
    document.getElementById("fName").select(); 
    return "That's not a valid first name"; 
} 
if (middleNm == "") { 
    validName = false; 
} 
else { 
    var middleNum = middleNm.charCodeAt(0) - 65; 

    if (middleNum < 0 || middleNum > 25) { 
     validName = false; 
    } 
} 
if (!validName) { 
    document.getElementById("mName").focus(); 
    document.getElementById("mName").select(); 
    return "That's not a valid middle name"; 
} 

if (lastNm == "") { 
    validName = false; 
} 
else { 
    var lastNum1 = lastNm.charCodeAt(0) - 65; 
    var lastNum2 = lastNm.charCodeAt((lastNm.length-1)) - 65; 

    if (lastNum1 < 0 || lastNum1 > 25 || lastNum2 < 0 || lastNum2 > 25) { 
     validName = false; 
    } 
} 

if (!validName) { 
    document.getElementById("lName").focus(); 
    document.getElementById("lName").select(); 
    return "That's not a valid last name"; 
} 

return "Your silly name is " + firstName[firstNum] 
+ " " + middleName[middleNum] + " " + lastName1[lastNum1] 
+ lastName2[lastNum2]; 
} 
+1

爲什麼在世界上所有的代碼,而不是3個簡單的隨機調用... – Blindy

+0

它看起來很好我http://jsfiddle.net/arunpjohny/yfATj/2/ –

+0

@Bindy我想爲了學習*作爲練習留給讀者來完成* – Jasen

回答

1

你的HTML和JavaScript爲我工作。我的猜測是你的瀏覽器正在緩存你的舊JavaScript文件。嘗試清除瀏覽器的緩存或刷新而不使用緩存(通常是Ctrl-f5或Shift-f5)。