Created
October 16, 2021 11:40
-
-
Save Stefanacef/4a7a968618945c86148229e8ea6afd8c to your computer and use it in GitHub Desktop.
Solution for "Electronics Shop" in Hackerrank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const b= 10; | |
const keyboards=[3, 1]; | |
const drives=[5, 2, 8] | |
function getMoneySpent(keyboards, drives, b) { | |
let arr=[]; | |
for(let i=0;i<keyboards.length;i++){ | |
for(let j=0;j<drives.length;j++){ | |
if(keyboards[i]+drives[j]<=b) | |
arr.push(keyboards[i]+drives[j]) | |
} | |
} | |
arr.sort((a,b)=>a-b) | |
if(arr.length==0){ | |
return (-1) | |
}else{ | |
return Math.max(...arr) | |
} | |
} | |
console.log(getMoneySpent(keyboards, drives, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment