Answer the question
In order to leave comments, you need to log in
What is wrong with the js code?
/*Create a function that will take an array and a variable k ,
and return
true if the sum of any two members of the array is equal to k and false otherwise.*/
let n=0
let x=0
function func(arr,k){
for(let i=0;
i<arr.length;i++)
{
if(n===arr[i]){
x++
for(let i=0;
i<arr.length;i++)
{
if(n+arr[i]===k){
console.log(n)
console.log(arr[i])
return true
}}}}
if(x<arr.length){
n++
func(arr,k)
}
if(x>=arr.length){
return false
}
}
console.log(func([11,25,34,46,5],59))
Answer the question
In order to leave comments, you need to log in
Very interesting code, which will fall on the syntax check.
const hasPairSum = (nums, desiredValue) => {
if(nums.length < 2) {
return false;
}
const [firstNum, ...tailNums] = nums;
const hasDesiredValue = tailNums.some((num) => firstNum + num === desiredValue);
return hasDesiredValue || hasPairSum(tailNums, desiredValue);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question