<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David</title>
    <description>The latest articles on DEV Community by David (@kingdraxx).</description>
    <link>https://dev.to/kingdraxx</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F352676%2F9c02c812-3289-420e-91e9-a7d6b9a597e6.png</url>
      <title>DEV Community: David</title>
      <link>https://dev.to/kingdraxx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kingdraxx"/>
    <language>en</language>
    <item>
      <title>Solving the HackerRank"Grading Students" Problem in TypeScript</title>
      <dc:creator>David</dc:creator>
      <pubDate>Fri, 04 Oct 2024 22:58:59 +0000</pubDate>
      <link>https://dev.to/kingdraxx/solving-the-hackerrankgrading-students-problem-in-typescript-4h2m</link>
      <guid>https://dev.to/kingdraxx/solving-the-hackerrankgrading-students-problem-in-typescript-4h2m</guid>
      <description>&lt;p&gt;I recently encountered a simple but interesting problem, the "Grading Students" challenge, and wanted to share how I solved it using JavaScript. The goal of this problem is to apply specific rounding rules to students' grades based on certain conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Breakdown&lt;/strong&gt;&lt;br&gt;
The task is to round student grades according to the following rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Any grade less than 38 remains unchanged because it’s a failing grade.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the difference between a grade and the next multiple of 5 is less than 3, the grade should be rounded up to the nearest multiple of 5.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the difference is 3 or greater, the grade remains unchanged.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Let's take a quick example to see how this works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A grade of &lt;code&gt;84&lt;/code&gt; will round to &lt;code&gt;85&lt;/code&gt; (because the next multiple of 5 is 85, and the difference is less than 3).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A grade of &lt;code&gt;29&lt;/code&gt; will remain the same (because it’s below 38 and considered a failing grade).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A grade of &lt;code&gt;57&lt;/code&gt; will remain &lt;code&gt;57&lt;/code&gt; (because the next multiple of 5 is 60, and the difference is greater than 3).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Approach to Solve the Problem&lt;/strong&gt;&lt;br&gt;
To solve this problem, the steps are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Loop through each grade&lt;/em&gt;: Iterate over the list of grades.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Check if the grade is less than 38&lt;/em&gt;: If so, skip to the next grade because no rounding is required for failing grades.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Find the next multiple of 5&lt;/em&gt;: For each grade, calculate the next multiple of 5.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Check the difference&lt;/em&gt;: If the difference between the next multiple of 5 and the current grade is less than 3, round the grade up to that multiple of 5.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Return the modified list of grades&lt;/em&gt;: After processing all the grades, return the updated list.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation&lt;/strong&gt;&lt;br&gt;
Here’s the code I wrote to solve this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function gradingStudents(grades) {
  // Loop through all the grades
  for (let i = 0; i &amp;lt; grades.length; i++) {
    // Calculate the next multiple of 5
    let nextMultipleOfFive = Math.ceil(grades[i] / 5) * 5;

    // Skip grades less than 38 (failing grades)
    if (grades[i] &amp;lt; 38) {
      continue;
    }

    // Round the grade if the difference is less than 3
    if (nextMultipleOfFive - grades[i] &amp;lt; 3) {
      grades[i] = nextMultipleOfFive;
    }
  }

  return grades;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-by-Step Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Loop through the grades&lt;/strong&gt;:&lt;br&gt;
The first step is to loop through the list of grades using a simple &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Calculate the next multiple of 5&lt;/strong&gt;:&lt;br&gt;
For each grade, I used the formula &lt;code&gt;Math.ceil(grade / 5) * 5&lt;/code&gt; to find the next multiple of 5. For example:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For a grade of &lt;code&gt;84&lt;/code&gt;, the next multiple of 5 is &lt;code&gt;85&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For a grade of &lt;code&gt;73&lt;/code&gt;, the next multiple of 5 is &lt;code&gt;75&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Check if the grade is less than 38&lt;/strong&gt;:&lt;br&gt;
Grades below 38 are considered failing, so they do not get rounded. If the grade is less than 38, we simply &lt;code&gt;continue&lt;/code&gt;to the next grade.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compare the grade with the next multiple of 5&lt;/strong&gt;:&lt;br&gt;
If the difference between the grade and the next multiple of 5 is less than 3, we update the grade to that multiple. For instance:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If the grade is &lt;code&gt;84&lt;/code&gt;, and the next multiple is &lt;code&gt;85&lt;/code&gt;, the difference is 1, so we round &lt;code&gt;84&lt;/code&gt; to &lt;code&gt;85&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the grade is &lt;code&gt;73&lt;/code&gt;, and the next multiple is &lt;code&gt;75&lt;/code&gt;, the difference is &lt;code&gt;2&lt;/code&gt;, so we round &lt;code&gt;73&lt;/code&gt; to &lt;code&gt;75&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Return the modified grades&lt;/strong&gt;:
After processing all the grades, we return the updated array of grades.&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>How I Solved the HackerRank "Picking Numbers" Problem in TypeScript</title>
      <dc:creator>David</dc:creator>
      <pubDate>Thu, 03 Oct 2024 21:49:21 +0000</pubDate>
      <link>https://dev.to/kingdraxx/how-i-solved-the-hackerrank-picking-numbers-problem-in-typescript-3jo9</link>
      <guid>https://dev.to/kingdraxx/how-i-solved-the-hackerrank-picking-numbers-problem-in-typescript-3jo9</guid>
      <description>&lt;p&gt;I recently tackled the "Picking Numbers" problem, a fun challenge where we need to find the longest subarray where the absolute difference between any two elements is less than or equal to 1. Here's how I solved this problem using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Breakdown&lt;/strong&gt;&lt;br&gt;
Given an array of integers, the task is to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Find the subarray where the absolute difference between any two elements is less than or equal to 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the length of the longest such subarray.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Approach&lt;/strong&gt;&lt;br&gt;
The problem boils down to finding subarrays that only contain two adjacent values, like numbers &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;x + 1&lt;/code&gt;. For instance, if you have an array &lt;code&gt;[1, 2, 2, 3]&lt;/code&gt;, both &lt;code&gt;[1, 2, 2]&lt;/code&gt; and &lt;code&gt;[2, 2, 3]&lt;/code&gt; are valid subarrays since the difference between any two elements in these subarrays is at most 1.&lt;/p&gt;

&lt;p&gt;To solve this, I took the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Count the frequency of each number&lt;/em&gt; in the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Check consecutive pairs&lt;/em&gt; of numbers and calculate their total frequency, which represents the length of the longest subarray for those two numbers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Track the maximum length&lt;/em&gt; of the valid subarrays across all possible pairs of consecutive numbers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;TypeScript Solution&lt;/strong&gt;&lt;br&gt;
Here’s the solution I came up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function pickingNumbers(a: number[]): number {
    let frequency = new Array(100).fill(0);

    // Step 1: Count the frequency of each element in the array
    for (let i = 0; i &amp;lt; a.length; i++) {
        frequency[a[i]]++;
    }

    let maxLength = 0;

    // Step 2: Check pairs of consecutive numbers (x and x+1)
    for (let i = 1; i &amp;lt; frequency.length; i++) {
        maxLength = Math.max(maxLength, frequency[i] + frequency[i - 1]);
    }

    return maxLength;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-by-Step Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Initialize Frequency Array:&lt;br&gt;
I used an array &lt;code&gt;frequency&lt;/code&gt;with 100 elements, initialized to zero. Since the problem guarantees that the integers in the array are between 1 and 100, this array allows us to count the occurrences of each number efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Count Frequencies:&lt;br&gt;
For each element in the input array &lt;code&gt;a&lt;/code&gt;, we increment the corresponding index in the &lt;code&gt;frequency&lt;/code&gt;array. For example, if &lt;code&gt;a[i] = 5&lt;/code&gt;, we increase &lt;code&gt;frequency[5]&lt;/code&gt; by 1. This will give us the number of occurrences of each number in the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check Consecutive Numbers:&lt;br&gt;
Once we have the frequency counts, we need to find the longest subarray where the absolute difference between any two numbers is less than or equal to 1. This can be done by adding the frequency of each number &lt;code&gt;i&lt;/code&gt; and the frequency of its neighbor &lt;code&gt;i-1&lt;/code&gt; (i.e., &lt;code&gt;frequency[i] + frequency[i-1]&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Track the Maximum Length:&lt;br&gt;
As we loop through the frequency array, we keep updating the &lt;code&gt;maxLength&lt;/code&gt; with the largest value found for consecutive numbers. This gives us the length of the longest valid subarray.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How I Solved the CodeWars "Cat and Dog Years to Human Years" Problem in JS</title>
      <dc:creator>David</dc:creator>
      <pubDate>Wed, 02 Oct 2024 19:39:50 +0000</pubDate>
      <link>https://dev.to/kingdraxx/how-i-solved-the-codewars-cat-and-dog-years-to-human-years-problem-in-js-49bk</link>
      <guid>https://dev.to/kingdraxx/how-i-solved-the-codewars-cat-and-dog-years-to-human-years-problem-in-js-49bk</guid>
      <description>&lt;p&gt;I recently tackled a fun challenge that involves converting cat and dog years into human years. The problem is simple but presents a unique way of handling calculations with conditions based on age ranges.&lt;/p&gt;

&lt;p&gt;Here’s the breakdown of how I approached and solved this challenge.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Breakdown&lt;/strong&gt;&lt;br&gt;
We are provided with two numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;catYears&lt;/code&gt;: Represents the age of a cat in cat years.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;dogYears&lt;/code&gt;: Represents the age of a dog in dog years.&lt;br&gt;
Our task is to convert these years into "human years" based on the following rules:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;The first two years of a cat's or dog's life are special:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first year counts as 15 human years.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second year counts as 9 human years.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;After the first two years, each additional year counts as:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;4 human years for cats.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;5 human years for dogs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution Approach&lt;/strong&gt;&lt;br&gt;
I used a combination of if and else if statements to handle the different cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If the animal's age is below 15 years, it translates to 0 human years.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it is between 15 and 24, that means the animal has lived one human year.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the animal has surpassed 24 years, we calculate how many years are left after those first 24 years and then convert those remaining years into human years (4 per year for cats and 5 per year for dogs).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;JavaScript Code&lt;/strong&gt;&lt;br&gt;
Here’s the solution I came up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var ownedCatAndDog = function(catYears, dogYears) {
  let humanCatYears;

  // Converting cat years to human years
  if (catYears &amp;lt; 15) {
    humanCatYears = 0;
  } else if (catYears &amp;lt; 24) {
    humanCatYears = 1;
  } else {
    humanCatYears = 2 + Math.floor((catYears - 24) / 4);
  }

  let humanDogYears;

  // Converting dog years to human years
  if (dogYears &amp;lt; 15) {
    humanDogYears = 0;
  } else if (dogYears &amp;lt; 24) {
    humanDogYears = 1;
  } else {
    humanDogYears = 2 + Math.floor((dogYears - 24) / 5);
  }

  return [humanCatYears, humanDogYears];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-by-Step Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initialize Variables:&lt;br&gt;
I started by creating two variables: humanCatYears and humanDogYears. These will hold the equivalent human ages for the cat and dog.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Handle Cat Years:&lt;br&gt;
For the cat:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If its age is less than 15 years (the equivalent of the first human year), it’s considered to be 0 human years.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If its age is between 15 and 24 (representing the second human year), it’s considered 1 human year.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If its age is more than 24, then it’s calculated as 2 + (remaining years) / 4, where the 2 accounts for the first two years, and each additional year counts as 4 human years.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Handle Dog Years:
Similarly for the dog:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If the dog's age is below 15 years, it’s 0 human years.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it is between 15 and 24, that’s 1 human year.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it’s above 24, we compute 2 + (remaining years) / 5, where the 2 is for the first two years, and each additional year is 5 human years.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Return the Result:
Finally, I returned an array containing the human equivalents for both the cat and the dog.&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>How I Solved the HackerRank "Birthday Cake Candles" Problem in JS</title>
      <dc:creator>David</dc:creator>
      <pubDate>Wed, 02 Oct 2024 19:02:14 +0000</pubDate>
      <link>https://dev.to/kingdraxx/how-i-solved-the-hackerrank-birthday-cake-candles-problem-in-js-3pc4</link>
      <guid>https://dev.to/kingdraxx/how-i-solved-the-hackerrank-birthday-cake-candles-problem-in-js-3pc4</guid>
      <description>&lt;p&gt;I recently encountered a fun problem called "Birthday Cake Candles" on a coding challenge platform. The goal is straightforward: given an array representing the heights of candles, I need to figure out how many tallest candles there are. Here's how I approached the problem using JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Breakdown&lt;/strong&gt;&lt;br&gt;
We are given an array, ar, where each element represents the height of a candle. The task is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Find the tallest candle(s).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Count how many candles have that tallest height.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution Approach&lt;/strong&gt;&lt;br&gt;
I took a two-step approach to solving this problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, I looped through the array to find the height of the tallest candle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, I looped again to count how many candles are of that tallest height.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s the JavaScript solution I came up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function birthdayCakeCandles(ar) {
    let tallestCandle = 0;
    let tallestCandlesCount = 0;

    // Step 1: Find the height of the tallest candle
    for (let i = 0; i &amp;lt; ar.length; i++) {
        if (ar[i] &amp;gt; tallestCandle) {
            tallestCandle = ar[i];
        }
    }

    // Step 2: Count how many candles have the tallest height
    for (let j = 0; j &amp;lt; ar.length; j++) {
        if (ar[j] === tallestCandle) {
            tallestCandlesCount++;
        }
    }

    return tallestCandlesCount;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-by-Step Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize Variables:
I started by declaring two variables:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tallestCandle&lt;/code&gt;: Holds the height of the tallest candle we find.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tallestCandlesCount&lt;/code&gt;: Keeps track of how many candles match the tallest height.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Find the Tallest Candle:
I looped through the ar array with a for loop. As I iterate through each candle height:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If the current candle is taller than the value stored in tallestCandle, I update tallestCandle to reflect this new height.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Count the Tallest Candles:&lt;br&gt;
After determining the height of the tallest candle, I loop through the array again. For each candle that matches the tallestCandle height, I increment the tallestCandlesCount variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the Result:&lt;br&gt;
Finally, after counting all the tallest candles, I return the value of tallestCandlesCount.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>HackerRank JS: Solving the Staircase Problem in JavaScript</title>
      <dc:creator>David</dc:creator>
      <pubDate>Mon, 30 Sep 2024 23:07:47 +0000</pubDate>
      <link>https://dev.to/kingdraxx/hackerrang-js-solving-the-staircase-problem-in-javascript-1h6c</link>
      <guid>https://dev.to/kingdraxx/hackerrang-js-solving-the-staircase-problem-in-javascript-1h6c</guid>
      <description>&lt;p&gt;Recently, I tackled a fun coding challenge: building a right-aligned staircase with a given height n, using JavaScript. I wanted to share how I approached and solved this problem, hoping it might help others tackle similar challenges.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The task&lt;/em&gt;: Print a staircase of height n where the # symbol represents each step. The tricky part is that the steps are right-aligned, and the rest of the row is filled with spaces.&lt;/p&gt;

&lt;p&gt;For example, given n = 4, we need the output to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   #
  ##
 ###
####
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, it might look like a simple nested loop problem, but the alignment requires careful attention to how we place spaces and # symbols.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thought Process:&lt;/strong&gt;&lt;br&gt;
Before jumping into code, I broke the problem into smaller, manageable tasks:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Print n rows&lt;/em&gt;: Each row should represent a step in the staircase.&lt;br&gt;
Each row has exactly n characters: Some of them are spaces, and the rest are #.&lt;br&gt;
Spaces should come before #: As the row number increases, fewer spaces are needed, and more # symbols are added.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Plan&lt;/strong&gt;:&lt;br&gt;
For every row i (starting from 0):&lt;br&gt;
Add spaces for the first n - i - 1 characters (to ensure right alignment).&lt;br&gt;
Then, fill the rest of the row with # characters.&lt;br&gt;
The logic is that as i (the row number) increases, the number of spaces decreases, and the number of # symbols increases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution Code&lt;/strong&gt;:&lt;br&gt;
Here’s the code I wrote to implement this plan:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function staircase(n) {
    // Write your code here
      var height = n;

    for (let i=0; i &amp;lt; height; i++) {
        let row= "";
        for (let j=0; j &amp;lt; height; j++) {
            if (i + j &amp;gt; height - 2) {
                row += "#";
            } else {
                row +=  " ";
            }
        }
        console.log(row)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Initialize the height: The first thing we do is store the value of n in the height variable. This makes the code easier to read and understand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Outer loop: This loop runs n times, representing the rows of the staircase. Each iteration corresponds to a row in the final output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inner loop: This loop runs n times as well, controlling how many characters will be in each row.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Condition to add # or space: The condition if (i + j &amp;gt; height - 2) checks whether a # should be printed. If this condition is met, we append a # to the row; otherwise, we add a space. This ensures that for each row, spaces appear first and then the # symbols.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print the row: Once a full row is constructed (a mix of spaces and # symbols), it is printed to the console.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
