admin管理员组

文章数量:1356227

I was just practising Javascript from Hackerrank. The question that I am working on is as follows:

Colleen is turning n years old! Therefore, she has n candles of various heights on her cake, and candle i has heighti. Because the taller candles tower over the shorter ones, Colleen can only blow out the tallest candles.

Given the heighti for each individual candle, find and print the number of candles she can successfully blow out.

Input Format

The first line contains a single integer, , denoting the number of candles on the cake. The second line contains space-separated integers, where each integer describes the height of candle .

Output Format

Print the number of candles Colleen blows out on a new line.

I executed the code below and the right answer was reflected in the console logs. However, when I tried to run the code on Hackerrank, it said that my answer was undefined. Why is that so?

function birthdayCakeCandles(ar) {
    var maxHeight = Math.max(...ar);
    var maxHeightCount = 0;  
    for(var i = 0; i < ar.length; i++){
        if (ar[i] == maxHeight){
            maxHeightCount = maxHeightCount + 1;
        }
    }
    console.log(maxHeightCount);
}

I was just practising Javascript from Hackerrank. The question that I am working on is as follows:

Colleen is turning n years old! Therefore, she has n candles of various heights on her cake, and candle i has heighti. Because the taller candles tower over the shorter ones, Colleen can only blow out the tallest candles.

Given the heighti for each individual candle, find and print the number of candles she can successfully blow out.

Input Format

The first line contains a single integer, , denoting the number of candles on the cake. The second line contains space-separated integers, where each integer describes the height of candle .

Output Format

Print the number of candles Colleen blows out on a new line.

I executed the code below and the right answer was reflected in the console logs. However, when I tried to run the code on Hackerrank, it said that my answer was undefined. Why is that so?

function birthdayCakeCandles(ar) {
    var maxHeight = Math.max(...ar);
    var maxHeightCount = 0;  
    for(var i = 0; i < ar.length; i++){
        if (ar[i] == maxHeight){
            maxHeightCount = maxHeightCount + 1;
        }
    }
    console.log(maxHeightCount);
}
Share Improve this question edited Sep 5, 2019 at 3:15 Unmitigated 89.8k12 gold badges99 silver badges104 bronze badges asked Sep 5, 2019 at 3:05 Samuel Samuel 393 silver badges8 bronze badges 2
  • try returning themaxHeightCount result – Joy Biswas Commented Sep 5, 2019 at 3:07
  • Perhaps you should return maxHeightCount at the end of the function. – Unmitigated Commented Sep 5, 2019 at 3:07
Add a ment  | 

15 Answers 15

Reset to default 3

function birthdayCakeCandles(arr) {

    const velas = arr.filter(value => Math.max(...arr) === value)
    return velas.length;
}

Trying returning the value:

`

function birthdayCakeCandles(ar) {
    var maxHeight = Math.max(...ar);
    var maxHeightCount = 0;  
    for(var i = 0; i < ar.length; i++){
        if (ar[i] == maxHeight){
            maxHeightCount = maxHeightCount + 1;
        }
    }
    console.log(maxHeightCount);
    return maxHeightCount;
}

`

function birthdayCakeCandles(candles) {
    var max_candles = Math.max(...candles) 
    var number_candles = 0
    for(var i=0; i<candles.length; i++){
        if(candles[i] == max_candles){
            number_candles = number_candles + 1   
        }
    }
    return number_candles;
}
function birthdayCakeCandles(candles) {
    // Write your code here
   let max = candles.reduce((a, b)=>Math.max(a, b));
   return candles.filter(x => x == max).length

}

Try this solution:

function birthdayCakeCandles(candles) {
  let tallestCount = 1;
  let i = 0;
  candles.sort((a, b) => b - a);
  while (i < candles.length) {
    if (candles[i] === candles[i + 1]) {
        tallestCount++
        i++;
    } else {
        break;
    }
  }

  return tallestCount;
}

Here is my solution which had passed all the test cases.

function birthdayCakeCandles(candles) {
    const bigCandle = Math.max(...candles);
    const count = candles.filter(x => x === bigCandle).length;
    return count;
}

Here is my solution with the Array Reduce method

function birthdayCakeCandles(candles) {
  let maxHeight = Math.max.apply(null, candles);
  let totalTallCandles = candles.reduce((cum, cur) => {
    if (cur == maxHeight) {
      cum += 1;
    }
    return cum;
  }, 0);
  return totalTallCandles;
}

let total = birthdayCakeCandles([3, 5, 2, 1, 6, 5, 6]);

alert(total);

will its an old thread and already answered it but I have anthor solution for you

function birthdayCakeCandles(candles) {
arr.sort().reverse();
    const tallCandle = arr[0]
    const arrIndex = arr.length -1
    let sum = 0
    for (i=0;i<=arrIndex;i++){
    if(arr[i] === tallCandle){
        sum += 1
    }
}
return sum
}

Python Version

def birthdayCakeCandles(candles):
    # Write your code here
    counter = {}
    for candle in candles:
        if candle in counter:
            counter[candle] += 1
        else:
            counter[candle] = 1
    max_number = max(candles)
    return counter.get(max_number)

function birthdayCakeCandles(candles) {
    // Write your code here
        let maxCandle = candles.sort().reverse()[0];
        return candles.filter(x => x === maxCandle).length;   
}

function birthdayCakeCandles(candles){ 
    // Write your code here
let tallestCandles = 1; //There is at least one(1) tallest candle
let sortedArr = candles.sort();
let index = sortedArr.length-1;

while (sortedArr[index]==sortedArr[index-1] && index>=0) {
tallestCandles++;
index--;
}
return tallestCandles;
}

easy solution

const birthdayCakeCandles = candles => {
    let max = 0;
    
    candles.forEach(candle => candle > max ? max = candle : max); //find max number

    const result = candles.filter(candle => candle === max); //filter the same number as max

    console.log(result.length);
}

birthdayCakeCandles([3,2,1,3]);
//result : 2

// solution 1
function birthdayCakeCandles(candles = [3, 2, 1, 3]) {
  const maxVal = Math.max(...candles);
  let result = 0;

  for (let a = 0; a < candles.length; a++) {
    if (candles[a] === maxVal) result++;
  }

  console.log(result);
  return result;
}
birthdayCakeCandles()

// solution 2
function birthdayCakeCandles2(candles = [3, 2, 1, 3]) {
  const maxVal = Math.max(...candles);
  const result = candles.filter((c) => c === maxVal).length;

  console.log(result);
  return result;
}
birthdayCakeCandles2()

This was my solution:

Slower performing algorithm:

Time plexity : O(2n)

var candles = [3, 2, 1, 3];
function birthdayCakeCandles(candles) {
  var maHeight = Math.max(...candles);
  var candle_count = 0;
  for (var i = 0; i < candles.length; i++) {
    if (candles[i] == maHeight) {
      candle_count = candle_count + 1;
    }
  }
  return candle_count;
}
console.log(birthdayCakeCandles(candles));
birthdayCakeCandles(candles);

Faster performing algorithm with a plexity of O(n):

var candles = [3, 2, 1, 3];

function birthdayCakeCandles(candles) {
  var maxHeight = 0;
  var count = 0;
  for (var i = 0; i < candles.length; i++) {
    if (candles[i] > maxHeight) {
      maxHeight = candles[i];
      count = 1;
    } else if (candles[i] === maxHeight) {
      count++;
    }
  }
  return count;
}

var count = birthdayCakeCandles(candles);
console.log(count); // output: 2

The faster algorithm will perform faster because we are only using a single loop to simultaneously find the maximum height and count the number of candles with that height.

function birthdayCakeCandles(arr) {
    const velas = arr.filter(value => Math.max(...arr) === value)
    return velas.length;
}

本文标签: javascriptHackerRankBirthday Cake CandlesStack Overflow