in Education by
I have an array that has multiple numbers in it. I want the function to return another array of maximum 3 numbers from the array that, when added together, equals another number. It can be 1 or 2 numbers too, but the first number must be the highest possible. e.g.: var array = [1,2,3,4,5,6,7,8,9,10]; var number: 25; desired output: [10,10,5] Or var array = [1,2,3,4,5,6,7,8,9,10]; var number: 11; desired output: [10,1] Or var array = [1,2,3,4,5,6,7,8,9,10]; var number: 5; desired output: [5] As you will probably see, i am building a small dart application that calculates your points. this is the last part where i need to show which combination of darts can get you to reach exactly 0 regardless of how many darts i have thrown. I can loop through my numbers and get them in an array but as soon as the combination needs to have a smaller number before another one, it cant compute. Here's the code: // the numbers to add var finishingNumbers = [60, 57, 54, 51, 50, 48, 45, 42, 40, 39, 38, 36, 34, 33, 32, 30, 28, 27, 26, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; // The number var output = 167; // The variable for the end result var dartsToFinishArr = []; // Call my function checkArray(); function checkArray() { for (var j = 0; j < finishingNumbers.length; j++) { var dartsToFinishArrTotal = 0; if (dartsToFinishArr.length > 0) { for (n = 0; n < dartsToFinishArr.length; n++) { dartsToFinishArrTotal += dartsToFinishArr[n]; } } if (output <= 180) { if (finishingNumbers[j] + dartsToFinishArrTotal == +($(".matchContainer .matchPlayerContainer .player.playerTurn .playerScore .value").text()) && dartsToFinishArr.length < 3) { dartsToFinishArr.push(finishingNumbers[j]); return; } else if (finishingNumbers[j] + dartsToFinishArrTotal < output && dartsToFinishArr.length < 3) { dartsToFinishArr.push(finishingNumbers[j]); checkArray(); return; } else if (dartsToFinishArrTotal != output) { return; } } } } this should give me: var dartsToFinishArr = [60, 57, 50] but it doesnt JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The logic below goes through all the combinations until it finds three numbers that meet your total. It also shortcuts to less than three numbers if they meet the total. // the numbers to add var finishingNumbers = [60, 57, 54, 51, 50, 48, 45, 42, 40, 39, 38, 36, 34, 33, 32, 30, 28, 27, 26, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; // The number var output = 167; console.log( findValues( finishingNumbers, output ) ); function findValues ( numbers, desiredTotal ) { numbers = numbers.slice(0).sort((a, b)=>b-a) //sort descending for ( let i = 0; i < numbers.length; i++ ) { if ( numbers[ i ] === desiredTotal ) return [ numbers[ i ] ]; // add in the numbers after the previous number at `i` for ( let j = i + i; j < numbers.length; j++ ) { if ( numbers[ i ] + numbers[ j ] === desiredTotal ) return [ numbers[ i ], numbers[ j ] ]; // add in the numbers after the previous number at `j` for ( let k = j + 1; k < numbers.length; k++ ) { if ( numbers[ i ] + numbers[ j ] + numbers[ k ] === desiredTotal ) return [ numbers[ i ], numbers[ j ], numbers[ k ] ]; } } } } Run code snippetExpand snippet

Related questions

0 votes
    I have an array that has multiple numbers in it. I want the function to return another array of maximum 3 ... { dartsToFinishArrTotal += dartsToFinishArr[n]; } } if (output...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Find the probability that a number selected from the number 1 to 25 is not a prime number when each of the ... likely to be selected. Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Find the probability that a number selected from the numbers 1 to 25 is not a prime number when each of the ... likely to be selected. Select the correct answer from above options...
asked Nov 18, 2021 in Education by JackTerrance
0 votes
    Given an array of integers, which can contain both +ve and -ve numbers. I've to maximize the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    This question's answers are a community effort. Edit existing answers to improve this post. It is not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Find the probability that a number selected at random from the numbers 1, 2, 3 . 35 is a: (i) Prime number (ii ... ) Multiple of 3 or 5 Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A fair die is tossed repeatedly until a six is obtained Let X denote the number of tosses required The probability that ... D. 125 216 Select the correct answer from above options...
asked Nov 16, 2021 in Education by JackTerrance
0 votes
    You are given that A and B are two events such that P(B)= 3/5, P(A | B) = 1/2 and P(A ∪ B) = 4/5, then P(A ... /10 B. 1/5 C. 1/2 D. 3/5 Select the correct answer from above options...
asked Nov 20, 2021 in Education by JackTerrance
0 votes
    In Example 99, the conditional probability that X≥6 given X>3 equals A. 125 216 B. 25 216 C. 5 36 D. 25 36 Select the correct answer from above options...
asked Nov 16, 2021 in Education by JackTerrance
0 votes
    This question exist only because of pure curiosity. Not a homework. Find the fastest way to find two ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
0 votes
    Two natural numbers x and y are chosen at random from the set {1,2,3,4,...3n}. find the probability that x2-y2 is ... D. 3n-1 2(5n-3) Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
0 votes
    If a number x is chosen at random from the numbers –3, –2, –1, 0, 1, 2, 3. What is probability that x2 ≤ 4? Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
...