Created
October 30, 2013 23:28
-
-
Save chikoski/7242071 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<input type="text" id="input"> | |
<button id="btn">名簿を検索</button> | |
<p id="output"></p> | |
<p>名簿:</p> | |
<ul id="list"></ul> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nameList = ["佐藤", "鈴木", "田中", "渡辺", "伊藤", "山本"]; | |
var list = document.getElementById("list"); | |
var input = document.getElementById("input"); | |
var output = document.getElementById("output"); | |
var btn = document.getElementById("btn"); | |
var showNameList = function(){ | |
list.innerHTML = "<li>" + nameList.join("</li><li>") + "</li>"; | |
}; | |
var indexOf = function(name){ | |
var i = 0; | |
while(i < nameList.length){ | |
if(nameList[i] == name){ | |
return i; | |
} | |
i = i + 1; | |
} | |
return -1; | |
}; | |
var findName = function(name){ | |
if(name === null){ | |
name = ""; | |
} | |
var position = indexOf(name); | |
if(position >= 0){ | |
output.textContent = name + "は名簿の" + position + "番目の要素です。"; | |
}else{ | |
output.textContent = name + "は名簿にありませんでした。"; | |
} | |
}; | |
showNameList(); | |
btn.onclick = function(){ | |
findName(input.value); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment