Purpose: This allows you to choose a target number and then will identify two numbers from the array that sum to be the target number.
Question: How do I add a message when there are no two numbers in the array that sum to be the target number?
What I've tried: Tried doing else with a console.log message when result === 0, also tried when result === [], and result === [0]. Tried doing another if statement. I think I am getting the syntax and/or placement wrong.
Code:
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));