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 will create 2 pointers : left (starting from 0

Developing Amazon price checker in Javascript

 Amazon price checker in Javascript using Nightmare


Disclaimer: This post is for educational purposes only. We do not encourage or promote any sort of web scraping.


Objective:

Have you ever thought of checking the price of your favorite product on any e-commerce site using your code. In this post we are going to achieve that. We will check the price of our selected product on amazon with the  help of Nightmare module.

Refer the link for more details on Nightmare module : https://www.npmjs.com/package/nightmare


Solution Design: 

let's proceed with our very first step, i.e to shortlist the product and get the Amazon URL. For this project we have selected the "OnePlus  8" with the URL : https://www.amazon.in/Test-Exclusive-547/dp/B078BNQ318/ 

Step 2:

We will get the element for the price of the product from the amazon URL. For this open the developer tools in the browser and inspect the price element. For reference, check the below screenshot.

Step 3:

We have selected "priceblock_ourprice" as the price element. Similarly we can fetch any other element that we wish to collect from the amazon page. Just for example we have captured "productTitle" for the product title as well.


Step 4:

Install the "Nightmare" module with the command:

npm i nightmare

we will require that in our code :

const nightmare = require('nightmare')()


Step 5 : Let's move to the coding part now:


const nightmare = require('nightmare')()

const args = process.argv.slice(2) // fetch the URL of the product from the node process arguments 

const url = args[0]


checkPrice()


async function checkPrice() {

  try {

    const resultString = await nightmare.goto(url)

                                       .wait("#productTitle")

                                       .evaluate(() =>document.getElementById("productTitle").innerText+" price:"+document.getElementById("priceblock_ourprice").innerText )

                                       .end()


    console.log("Name : "+ resultString)


  } catch (e) {

    throw e;

  }

}


Step 6: To run the code and pass the URL use below command:

node amazon-mytracker.js https://www.amazon.in/Test-Exclusive-547/dp/B078BNQ318/

For testing, i have tested the code with two different input (product URLs).

Below screenshot shows the input and output of our code:


In the code we have used only the product title and product price information in the response, the code can be customized as per the requirement.

In the next part, we will add the functionality of cron job and email  to convert this price checker into Amazon price tracker.

Comments

  1. Astounding modern solution to modern era problem. ❣️

    ReplyDelete

Post a Comment

Popular posts from this blog

Ice Cream Parlor : Hackerrank Problem and Solution

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

Descending Order || CodeWars Problem and solution in javascript.