electronics shop hackerrank solution javascript 1

electronics shop hackerrank solution javascript

// electronics shop hackerrank solution javascript
function getMoneySpent(keyboards, drives, b) {
    /*
     * Write your code here.
     */
    let sum = [];
    let max = 0;
    for(let i=0; i max){
                max = sum[k];
            }
        }
    }
    
    if(max !== 0 && max <=b){
        return max;
    }else{
        return -1;
    }
}

Here is what the above code is Doing:
1. We create an empty array called sum.
2. We loop through the keyboards array and the drives array.
3. We add the values of the keyboards array and the drives array and push the sum into the sum array.
4. We loop through the sum array and check if the value is less than or equal to b.
5. If the value is less than or equal to b, we check if the value is greater than the max value.
6. If the value is greater than the max value, we set the max value to the value.
7. If the max value is not equal to 0 and the max value is less than or equal to b, we return the max value.
8. If the max value is not less than or equal to b, we return -1.

Similar Posts