Return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false.
let arr = [2,2,2,2,4,1]
function SumMultiplier(arr) {
let sum = arr.reduce((acc, item) => acc +item,0)
let trueStr = "true"
let falseStr = "false"
let double = sum*2
let arrLength = arr.length
let i = 0
let j = 1
while(i < arrLength){
let multi = arr[i] * arr[j]
if(double < multi) {
j++
}else{
return trueStr
}
}
return falseStr
}
i found double the sum of all elemets in array, but cant solve multiplaction each element in array.
3
Answers
You can find tho largest element in the array, multiply them and then compare result with the doubled sum.
Assuming that you don’t care if the number multiplies by itself, try using nested loops, it’s easier. This way you check every element with the other ones including itself.
Something like:
as @tkausl comented
after @Nina Scholz coment modify sort() to sort((e1, e2) => e1 – e2)
@tatactic coment => sort((e1, e2) => e2 – e1)