Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda