티스토리 뷰
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
Birthday Cake Candles | HackerRank
Determine the number of candles that are blown out.
www.hackerrank.com
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
developer.mozilla.org
https://www.w3schools.com/js/js_math.asp
JavaScript Math Object
JavaScript Math Object The JavaScript Math object allows you to perform mathematical tasks on numbers. Example Math.PI; // returns 3.141592653589793 Try it Yourself » Math.round() Math.round(x) returns the value of x rounded to its
www.w3schools.com
'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
- hackerrank javascript solution
- code refactoring
- rest parameter
- math.max
- repeat()
- easy javascript algorithm
- C++
- 프로그래머스 알고리즘문제
- substring()
- 프로그래머스
- math.abs
- Object type casting
- javascript
- hackerrank javascript
- equals()
- ... in Javascript
- string class in java
- 알고리즘
- hackerrank solution
- Collection Framework
- hackerrank
- easy algorithm
- algorithm
- 프로그래머스 알고리즘
- compareTo()
- Javascript Algorithm
- spring boot application
- HackerRank Algorithm
- HashMap
- java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 |