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

228
Views
¿Cómo funciona JS forEach con operadores ternarios y si?

Estoy tratando de entender alrededor de 2 horas lo que estoy haciendo mal en la tercera y cuarta línea de código. La consola muestra SyntaxError: Unexpected token if o SyntaxError: missing ) after argument list . La primera y la segunda línea funcionan como se esperaba.

 let arr = ['Custy Stomer', 'C. Oostomar', 'CUS Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur']; let checkArr = (arr) => arr.forEach(el => console.log(typeof el !== 'string')) let checkArr = (arr) => arr.forEach(el => if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`)} ) let checkArr = ((arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null; )}) checkArr(arr);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Línea 3:

Envuelva el cuerpo de devolución de llamada forEach entre paréntesis

 let checkArr = (arr) => arr.forEach(el => { if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`); } })

Línea 4:

Eliminar punto y coma

 let checkArr = (arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null ) }

Recomendado:

 const checkArr = (arr) => { arr.forEach(el => { if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`); } }); }
about 4 years ago · Juan Pablo Isaza Report

0

Hola, definiste tu función con un extra (

Solución para el 3er caso:

 let arr = ['Custy Stomer', 'C. Oostomar', 'CUS Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur']; let checkArr = (arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null ) } checkArr(arr);

about 4 years ago · Juan Pablo Isaza Report

0

Dos problemas.

  • La definición de la segunda función debe escribirse dentro de {}
  • Declaración de sed, elimine la semicolumna no deseada después de nulo.

 let arr = ['Custy Stomer', 'C. Oostomar', 'CUS Tomer', 3432434, 'Custo Mer', 'Custopher Ustomer', 3432435, 'Kasti Yastimeur']; let checkArr1 = (arr) => arr.forEach(el => console.log(typeof el !== 'string')) let checkArr2 = (arr) => arr.forEach(el => { if (typeof el !== 'string') { console.log(`Type error: ${el} should be a string!`) } }) let checkArr3 = ((arr) => { arr.forEach(el => typeof el !== 'string' ? console.log(`Type error: ${el} should be a string!`) : null) }) checkArr1(arr); checkArr2(arr); checkArr3(arr);

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!