티스토리 뷰
Problem
You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.
For example, if your niece is turning 4years old, and the cake will have 4candles of height 4,4,1,3 she will be able to blow out 2 candles successfully since the tallest candles are of height 4 and there are 2 such candles.
Solution in JavaScript
const arr = [2, 7, 4, 6, 7];
function birthdayCakeCandles(arr) {
//Math.max returns maximum value in the array.
const max = Math.max(...arr);
//filter() methods creates a new array with all elements
//that pass the test implemented by the provided function
const filtered = arr.filter( elem => elem === max);
const result = filtered.length;
console.log(result); //2
}
birthdayCakeCandles(arr);
Reference
https://www.hackerrank.com/challenges/birthday-cake-candles/problem
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
https://www.w3schools.com/js/js_math.asp
'Algorithms' 카테고리의 다른 글
Grading Students (0) | 2020.08.22 |
---|---|
Time Conversion (0) | 2020.08.22 |
Mini-Max Sum (0) | 2020.08.04 |
Staircase (0) | 2020.08.04 |
Plus Minus (0) | 2020.07.29 |
- Total
- Today
- Yesterday
- string class in java
- Object type casting
- code refactoring
- hackerrank solution
- 프로그래머스 알고리즘
- hackerrank javascript
- algorithm
- HashMap
- repeat()
- math.max
- C++
- compareTo()
- easy algorithm
- java
- Javascript Algorithm
- easy javascript algorithm
- hackerrank javascript solution
- 프로그래머스
- ... in Javascript
- 알고리즘
- spring boot application
- Collection Framework
- math.abs
- HackerRank Algorithm
- hackerrank
- rest parameter
- 프로그래머스 알고리즘문제
- javascript
- equals()
- substring()
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |