Created
April 7, 2023 04:07
-
-
Save LuisitoRizado/8cb832ff683813b16adef0c26f60e71f to your computer and use it in GitHub Desktop.
This is a solution for the 'Electronics shop' Challenge by 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
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