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

101
Views
Cómo usar If in Loop para devolver el mensaje si no hay resultados

Propósito: Esto le permite elegir un número objetivo y luego identificará dos números de la matriz que suman para ser el número objetivo.

Pregunta: ¿Cómo agrego un mensaje cuando no hay dos números en la matriz que sumen para ser el número de destino?

Lo que probé: intenté hacer otra cosa con un mensaje de console.log cuando el resultado === 0, también intenté cuando el resultado === [] y el resultado === [0]. Intenté hacer otra declaración if. Creo que me estoy equivocando en la sintaxis y/o ubicación.

Código:

 const twoSum = (array, target) => { let result = []; for (i = 0; i < array.length; i++) { if(array[i] > target) { continue; } if(array.includes(target - array[i])) { result.push(array[i]); result.push(target - array[i]); break; } if(result.length < 1){ console.log("The array does not have two numbers that sum to equal your target value.") break; } } return result; }; let array = Array.from({length: 10000}, () => Math.floor(Math.random() * 10000)); const target = 2; console.log(twoSum(array, target));
about 4 years ago · Juan Pablo Isaza
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!