Posts

Dutch national flag problem in Javascript

Image
Dutch national flag problem and solution in Javascript Problem statement:   The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue such that balls of the same color are placed together. Now, let's consider an array with 3 distinct values say 0, 1 and 2. We won't be using any sort method and we need to sort this array in 0(n). Input Array :  let   arr  = [ 0 ,  2 ,  1 ,  0 ,  1 ,  2 ,  0 ,  2 ]; Expected Output: [ 0, 0, 0, 1, 1, 2, 2, 2 ] Solution Approach : When we see expected output, we can clearly see that sorted array is divided into 3 sections having values 0 , 1 and 2. So, let's divide the array in 3 sections: a) from 0th index to left boundary b) from left boundary to right boundary c) from right boundary to last index. Now we...

Javascript Arrays: Take a Ten Minute Walk

  Codewars Problem and solution with approach explained Objective: In this challenge, we will learn about arrays in javascript. Problem statement: You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones – every time you press the button it sends you an array of one-letter strings representing directions to walk (eg.    ['n', 's', 'w', 'e']). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise. Sample test cases: Test.expect(isValidWalk([ 'n...

Disemvowel Trolls || Codewars problem and solution in Javascript || Topic : Strings and RegEx

  Problem: Disemvowel Trolls Description : Trolls are attacking your comment section! A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat. Your task is to write a function that takes a string and return a new string with all vowels removed. For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".   Solution 1# function disemvowel(str) {   var str = str.replace(/a/gi,'').replace(/e/gi,'').replace(/i/gi,'').replace(/o/gi,'').replace(/u/gi,'');   return str; } Solution 2# (slightly more concise) function disemvowel(str) {   return str.replace(/[aeiou]/gi, ''); }

Descending Order || CodeWars Problem and solution in javascript.

  Problem : Descending Order Description : Your task is to make a function that can take any non-negative integer as a argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number. Examples : Input: 42145 Output: 54421 Input: 145263 Output: 654321 Input: 123456789 Output: 987654321     Solution 1# function descendingOrder(n){     var stringNumber = n.toString();   var arr = stringNumber.split('');   var result = arr.sort(function(a, b){return b - a});   var stringresult = result.join();   stringresult= stringresult.replace(/,/g,"");   return parseInt(stringresult) }   Solution 2# (slightly more concise) function descendingOrder(n){   return parseInt(String(n).split('').sort().reverse().join('')) }

Day 4: Count Objects (Hackerrank 10 days of Javascript) Problem and solution

  Objective:: In this challenge, we learn about iterating over objects. Task:: Complete the function in the editor. It has one parameter: an array, a  , of objects. Each object in the array has two integer properties denoted by  x and  y . The function must return a count of all such objects  o  in array a   that satisfy  o.x==o.y . Input Format:: The first line contains an integer denoting  n . Each of the  n  subsequent lines contains two space-separated integers describing the values of  x and  y . Constraints:: ·        5 <= n <= 10 ·        1 <=x, y<=100 Output Format Return a count of the total number of objects  o  such that o.x==o.y . Locked stub code in the editor prints the returned value to STDOUT. Sample Input 0 5 1 1 2 3 3 3 3 4 4 5 Sample Output 0 2 Explanation 0 There are...

HackerRank Problem: Halloween Sale !!!!

HackerRank Problem: Halloween Sale !!!! Problem:          You wish to buy video games from the famous online video game store Mist.  Usually, all games are sold at the same price,     dollars. However, they are planning to have the seasonal Halloween Sale next month in which you can buy games at a cheaper price. Specifically, the first game you buy during the sale will be sold at   dollars, but every subsequent game you buy will be sold at exactly     dollars less than the cost of the previous one you bought. This will continue until the cost becomes less than or equal to     dollars, after which every game you buy will cost     dollars each.  For example, if   ,     and   , then the following are the costs of the first     games you buy, in order:  You have     dollars in your Mist wallet. How many games can you buy during the Halloween Sale? Input ...