找到两个数字当添加给目标数字时
Find Two numbers When added to give Target number
我写这段代码是为了在添加时找到两个数字并给出一个目标数字。
我已经解决了这个练习,但我想用上面的方法来做
在 if 语句中没有 return true,我不知道为什么。
function add(arr , target){
let result = [];
arr.map ( (item) => {
const num1 = target - item;
if (arr.includes(num1)) {
return result.concat(num1 , item);
}
else {
return "Unfortunaly there isnt answer";
}
});
}
add([3,4,5,6,10] , 16);
function add(arr , target){
let result = [];
for (let item of arr){
const num1 = target - item;
if (arr.includes(num1)) {
return result.concat(num1 , item);
}
};
return "Unfortunately there is no answer";
}
const result = add([3,4,5,6,10] , 16);
// will print [10, 6]
console.log(result)
我写这段代码是为了在添加时找到两个数字并给出一个目标数字。 我已经解决了这个练习,但我想用上面的方法来做 在 if 语句中没有 return true,我不知道为什么。
function add(arr , target){
let result = [];
arr.map ( (item) => {
const num1 = target - item;
if (arr.includes(num1)) {
return result.concat(num1 , item);
}
else {
return "Unfortunaly there isnt answer";
}
});
}
add([3,4,5,6,10] , 16);
function add(arr , target){
let result = [];
for (let item of arr){
const num1 = target - item;
if (arr.includes(num1)) {
return result.concat(num1 , item);
}
};
return "Unfortunately there is no answer";
}
const result = add([3,4,5,6,10] , 16);
// will print [10, 6]
console.log(result)