I am parsing through a csv file. My goal is to look for the names that starts with "A", "B" and count the occurrences. The problem with the following code is it never completes the while loop.
HTML:
<form>
<input type="button" id="csv1ButtonRun" value="Run" style="background-color: lightgray"/>
</form>
Scripts included:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="app.js"></script>
<script src="jquery.csv-0.71.js"></script>
JavaScript:
var babyCsv = " Jacob, 1, boy, 2010, Isabella, 1, girl, 2010, Ethan, 2, boy, 2010, Sophia, 2, girl, 2010, Michael, 3, boy, 2010, Emma, 3, girl, 2010, Jayden, 4, boy, 2010, Olivia, 4, girl, 2010, William, 5, boy, 2010, Ava, 5, girl, 2010, Alexander, 6, boy, 2010, Emily, 6, girl, 2010, Noah, 7, boy, 2010, Abigail, 7, girl, 2010, Daniel, 8, boy, 2010, Madison, 8, girl, 2010, Aiden, 9, boy, 2010, Chloe, 9, girl, 2010, Anthony, 10, boy, 2010, Mia, 10, girl, 2010, Joshua, 11, boy, 2010, Addison, 11, girl, 2010, Mason, 12, boy, 2010, Elizabeth, 12, girl, 2010, Christopher, 13, boy, 2010, Ella, 13, girl, 2010, Andrew, 14, boy, 2010, Natalie, 14, girl, 2010, David, 15, boy, 2010, Samantha, 15, girl, 2010, Matthew, 16, boy, 2010";
var data = $.csv.toArray(babyCsv);
$("#csv1ButtonRun").click(function() {
var arrayLength = data.length / 4;
var count1 = 0;
var count2 = 0;
var i = 0;
while (i <= arrayLength) {
var name = data[i * 4];
if (name.substring(0, 2) == " A") {
count1 = count1 + 1;
}
else if (name.substring(0, 2) == " B") {
count2 = count2 + 1;
};
i++;
};
document.write(count1 + count2);
});
document.write. Useconsole.log()if you want to just see "a result" (it'll log into the dev console. every browser has one), or use DOM building methods (var div = document.createElement('div'); div.innerHTML = "..."; document.body.appendChild(div)and their ilk, or jQuery) to get results injected into the pagesrc="path/tojquery.js"