Hacker Rank Bon Appetit

Run Settings
LanguageJavaScript
Language Version
Run Command
'use strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', function(inputStdin) { inputString += inputStdin; }); process.stdin.on('end', function() { inputString = inputString.split('\n'); main(); }); function readLine() { return inputString[currentLine++]; } /* * Complete the 'bonAppetit' function below. * * The function accepts following parameters: * 1. INTEGER_ARRAY bill * 2. INTEGER k * 3. INTEGER b */ function bonAppetit(bill, k, b) { // Write your code here let res = 0 for(let i = 0 ; i < bill.length ; i++ ){ if(bill[i] == bill[k]){ continue } res += bill[i] } res /= 2 if(res==b) { console.log("Bon Appetit") }else if(b>res){ console.log(b - res) } else{ console.log(res - b) } console.log(bill, k, b) } function main() { const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' '); const n = parseInt(firstMultipleInput[0], 10); const k = parseInt(firstMultipleInput[1], 10); const bill = readLine().replace(/\s+$/g, '').split(' ').map(billTemp => parseInt(billTemp, 10)); const b = parseInt(readLine().trim(), 10); bonAppetit(bill, k, b); }
Editor Settings
Theme
Key bindings
Full width
Lines