so i have been slowly working through cs101, first question of week 5 block 4 has me stumped.
so the idea is im supposed to be writing psuedo accurate javascript to determine the number of names starting with A and B in a 2000 entry csv of baby names, im 99% sure i have written the correct code but keep getting returned "unexpected token "{" " i have tried removing all curly brackets one by one which just produces errors obviously and have no idea what i have done wrong, please help.
table = new SimpleTable("baby-2010.csv");
count1 = 0; // A count
count2 = 0; // B count
for (row: table) {
if (row.getField("name").startsWith("A") {
count1 = count1 + 1;
}
if (row.getField("name").endsWith("B") {
count2 = count2 + 1;
}
}
print("A count:", count1);
print("B count:", count2);
all good, VLAZ had the correct solution, both if statements were missing a closing ).
thanks heaps guys
use indexOf and lastIndexOf instead of startsWith and endsWith
if (row.getField("name").indexOf("A") === 0) {
count1 = count1 + 1;
}
if (row.getField("name").lastIndexOf("B") === row.getField('name').length) {
count2 = count2 + 1;
}
You could also use a regex test for /^A/ or /B$/