Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LuisitoRizado/8cb832ff683813b16adef0c26f60e71f to your computer and use it in GitHub Desktop.
Save LuisitoRizado/8cb832ff683813b16adef0c26f60e71f to your computer and use it in GitHub Desktop.
This is a solution for the 'Electronics shop' Challenge by Hackerrank
function getMoneySpent(keyboards, drives, b) {
/*
* Write your code here.
*/
//Given an keyboards array an a drives array
//We've gotta find how many possibles
//'Combinations' are given a budget
//Example:
//keyboards = [40,25,30,60]
//drives = [10,12,14,7,18]
//budget = 74
//Possibles ways:
// 40+10
// 40 +12
let mostExpensive = 0;
keyboards.forEach(keyboard =>{
//Compare every drives
drives.forEach(drive=>{
if((drive+keyboard<=b)&& (drive+keyboard > mostExpensive)){
mostExpensive = drive+keyboard
}
})
})
if(mostExpensive == 0){
return -1
}
else{
return mostExpensive;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment