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));