Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda