Skip to content

Instantly share code, notes, and snippets.

@tanyapowell
Created October 13, 2022 20:08
Show Gist options
  • Select an option

  • Save tanyapowell/bcb2533d47222930384a32ac8154dd1d to your computer and use it in GitHub Desktop.

Select an option

Save tanyapowell/bcb2533d47222930384a32ac8154dd1d to your computer and use it in GitHub Desktop.
Hackerrank solution from Thomas Cohort
'use strict';
const fs = require('fs');
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 'birthdayCakeCandles' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY candles as parameter.
*/
//can simplify it by creating a variable
//create a loop to go over each element
//create a variable for the first element of the array, using that variable to iterate over the other elements to see if their the same/bigger than the first element and assign it to the array
//When looping will check two things, 1)is the number bigger than the first element and so on,
// .sort((a,b) => a-b)
function birthdayCakeCandles(candles) {
let HighestNumber = candles.sort((a,b) => b - a)[0];
let counter = 1;
for (let i = 1; i < candles.length; i++) {
// if (candles[i] > HighestNumber) {
// HighestNumber = candles[i];
// };
if (HighestNumber === candles[i]) {
counter += 1;
}
};
return counter
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const candlesCount = parseInt(readLine().trim(), 10);
const candles = readLine().replace(/\s+$/g, '').split(' ').map(candlesTemp => parseInt(candlesTemp, 10));
const result = birthdayCakeCandles(candles);
ws.write(result + '\n');
ws.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment