Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
stanford cs101 javascript error in week 5

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);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

all good, VLAZ had the correct solution, both if statements were missing a closing ).

thanks heaps guys

about 4 years ago · Juan Pablo Isaza Report

0

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$/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!