This is one of the projects on freecodecamp. I would like a review on my code. Thanks in advance.
Javascript:
var answers;
function formatSearchString() {
var searchString = document.getElementById("searchBar").value;
var words = searchString.split(" ");
searchString = words.join("_");
return searchString;
}
function getQueryData() {
var stringToSearch = formatSearchString();
var wikiUrl = "http://en.wikipedia.org/w/api.php?action=opensearch&search=" + stringToSearch + "&format=json&callback=wikiCallbackFunction";
$.ajax(wikiUrl, {
dataType: "jsonp",
success: function(wikiResponse) {
//alert(wikiResponse);
//alert(wikiResponse.length); Always 4.
//alert(wikiResponse[0]); Search String
//alert(wikiResponse[1]); Titles
//alert(wikiResponse[2]); Explanations
//alert(wikiResponse[3]); Links
answers = wikiResponse;
}
});
setTimeout(formatResults, 1500);
}
function getRandomArticle() {
var wikiUrl = "http://en.wikipedia.org/wiki/Special:Random";
window.location = wikiUrl;
}
function formatResults() {
$("#results").empty();
var i = 0;
for (; i < answers[1].length; i++) {
var newDiv = document.createElement("div");
newDiv.className = "searchResults";
var titleLink = document.createElement("a");
titleLink.setAttribute("target", "_blank");
titleLink.setAttribute("href", answers[3][i]);
titleLink.innerHTML = answers[1][i];
var desc = document.createTextNode(answers[2][i]);
var newLine = document.createElement("br");
newDiv.appendChild(titleLink);
var newLine = document.createElement("br");
newDiv.appendChild(newLine);
var newLine = document.createElement("br");
newDiv.appendChild(newLine);
newDiv.appendChild(desc);
var newLine = document.createElement("br");
newDiv.appendChild(newLine);
var newLine = document.createElement("br");
newDiv.appendChild(newLine);
document.getElementById("results").appendChild(newDiv);
}
}
document.getElementById("buttonForSearch").addEventListener("click", getQueryData);
document.getElementById("searchBar").addEventListener("keypress", function(e) {
if (e.which == 13) {
e.preventDefault();
getQueryData();
}
return false;
});
document.getElementById("randomArticle").addEventListener("click", getRandomArticle);
CSS:
body {
background: radial-gradient(circle, black, white);
height: 700px;
background-size: cover;
text-align: center;
}
#searchBar {
padding-left: 10px;
background: black;
color: white;
border-radius: 7px;
border: 2px solid black;
height: 25px;
}
a {
font-family: 'Tangerine', serif;
color: grey;
font-size: 45px;
}
#results {
height: 700px;
margin-left: 550px;
}
.searchResults {
padding-left: 10px;
padding-top: 10px;
background: black;
color: white;
height: auto;
width: 650px;
text-align: left;
margin-top: 10px;
font-size: 20px;
}
#buttonForSearch {
height: 30px;
width: 90px;
margin-left: 50px;
border: 2px solid black;
size: 14px;
border-radius: 7px;
color: white;
background: black;
}
#randomArticle {
height: 30px;
width: 150px;
margin-left: 50px;
border: 2px solid black;
size: 14px;
border-radius: 7px;
color: white;
background: black;
}
h1 {
margin-top: 50px;
font-family: 'Tangerine', serif;
font-size: 48px;
text-shadow: 4px 4px 4px #aaa;
}
#searchFields {
margin: 45px;
text-align: center;
}
HTML:
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Tangerine">
<body>
<script src="http://code.jquery.com/jquery-latest.min.js">
</script>
<script src='http://okfnlabs.org/wikipediajs/wikipedia.js'></script>
<h1>Wikipedia Search</h1>
<div id="searchFields">
<input type="text" placeholder="Type To Search" id="searchBar">
<input type="button" value="Search" id="buttonForSearch"><br /><br /><br/>
<input type="button" value="Random Article" id="randomArticle">
</div>
<div id="results"></div>
</body>
Full Code Here: http://codepen.io/jpninanjohn/pen/GZrzoG
var i=0;
with the idea of using a while loop. I then changed my mind to use a for loop. So I just used the already initialized variablei
\$\endgroup\$ – leoOrion Jun 8 '16 at 8:28