JAVA 9 takeWhile and doWhile

Trying to get my hands wet with Java 9. Looks like the streams have got another round of good method updates. Here in this gist we are trying to demonstrate the takeWhile and the dropWhile filter in the Java 9.

takeWhile method

Operates on a stream and in the code, we are trying to identify the songs that were from the year that is less than 2002. This condition qualifies only one record to be returned. The returned data is

2000 VENILAVE

dropWhile method

Aging this operates on a stream as well, and this one returns all the records whose album year is after 2002. So the we get back the following records.

1: https://gist.github.com/sathishjayapal/42b646b07f79b3e697909149ea27ae1f"https://gist.github.com/sathishjayapal/42b646b07f79b3e697909149ea27ae1f.js">

2002 VA VA VASANTHAME

2010 VANJOKOTIYA

2008 SEMMAFIGURE

2001 ARABIC KADLORAM

1995 ANJALI ANJALI

2010 PEN ALLA PEN ALLA

TED talk Friday., but wait is it.

I really enjoyed this commencement speech from David foster. This was from Tim Ferris news letter. The narration where the speech is talking about compassion. In the middle of the speech, I was thinking this is another left wing constructed speech talking about first world problems. I was wrong, and am glad that I heard the entire thing.

Free-code camp Inventory update solution

One other interesting problem with the inventory update challenge from freecodecamp. After this, it is going to be the modules for creating calculators and other challenges.


function updateInventoryWithArr(curInv, newInv) {
    var arr1 = curInv;
    var arr2 = newInv;
    var finalOutPutArr = [];
    var arr1Map = new Map();
    var arr2Map = new Map();
    var arr3Map = new Map();
    for (var j = 0; j < arr1.length; j++) {
        var data1 = arr1[j][1];
        var data2 = arr1[j][0];
        arr1Map.set(data1, data2);
        arr3Map.set(data1, data2);
    }

    for (var i = 0; i < arr2.length; i++) {
        var keyVal = arr2[i][0];
        var lookupKey = arr2[i][1];
        var keyData = arr1Map.get(lookupKey);
        var sumVal;
        if (typeof keyData != 'undefined') {

            sumVal = keyVal + keyData;
            arr3Map.set(arr2[i][1], sumVal);
            arr2Map.set(sumVal, arr2[i][1]);
        } else {
            arr3Map.set(arr2[i][1], keyVal);
            arr2Map.set(keyVal, arr2[i][1]);
        }

    }

    var dataInf = Array.from(arr3Map);
    for (var k = 0; k < dataInf.length; k++) {
        finalOutPutArr.push([dataInf[k][1], dataInf[k][0]]);
    }
    finalOutPutArr = finalOutPutArr.sort(function (a, b) {
        return a[1] > b[1]
    });
    return finalOutPutArr;
}

Bootstrap 4 Tutorial

One of a kind tutorial, packed with all that is needed to start Bootstrap 4. When we are debugging an issue for some server side issue, we will need to know a lot of nit-picky details of Bootstrap. But this was a quite a walk thro' of all the items that are needed to know and read the bootstrap code.

Freecode camp checkCashRegister problem.

I did not like the solution to this problem. But here it is., the question was about giving exact change back to user. The probiem with a $100 spent for a $3.26 purchase.


    var changeGiven = [];
    var total = 0;
    var quarters = 0;
    var pennies = 0;
    var nickels = 0;
    var dimes = 0;
    for (var totalCount = 0; totalCount < cid.length; totalCount++) {
        total = total + cid[totalCount][1];
        if (cid[totalCount][0] === "QUARTER") {
            quarters = Math.round((cid[totalCount][1] * 100 / 25), 2);
        } else if (cid[totalCount][0] === "PENNY") {
            pennies = Math.round((cid[totalCount][1] * 100 / 1), 2);
        } else if (cid[totalCount][0] === "NICKEL") {
            nickels = Math.round((cid[totalCount][1] * 100 / 5), 2);
        } else if (cid[totalCount][0] === "DIME") {
            dimes = Math.round((cid[totalCount][1] * 100 / 10), 2);
        }
    }
        return changeGiven
 

The above block solves most of the items that is needed to solve the problem. But when we are working on the $100 problem, where cashRegister had to return some definite values., here is the code snippet for that.


    var changeToGive = cash - price;
    runningChange = changeToGive;
    var runningChange;
    var twenties;
    if (total - changeToGive == 0) {
        return "Closed";
    } else if (total - changeToGive < 1) {
        return "Insufficient Funds";
    }

    if ((changeToGive < 1) && ((changeToGive * 100) % 25 == 0)) {
        changeGiven = [["QUARTER", changeToGive]];
        return changeGiven;
    }
    if (changeToGive > 1 && changeToGive > 20) {
        twenties = cid.find(function (element) {
            return element[0] == "TWENTY";
        });
        if (changeToGive > twenties[1]) {
            runningChange = parseFloat(
                Math.round((runningChange - twenties[1]) * 100) / 100).toFixed(
                2);
        }
        changeToGive = runningChange;
        if (runningChange > 0) {
            changeGiven.push([twenties[0], parseFloat(twenties[1].toFixed(2))]);
        }

    }
    var tens;
    if (changeToGive > 1 && changeToGive > 10) {
        tens = cid.find(function (element) {
            return element[0] == "TEN";
        });
        if (changeToGive > tens[1]) {
            runningChange = parseFloat(
                Math.round((runningChange - tens[1]) * 100) / 100).toFixed(2);
        }
        changeToGive = runningChange;
        if (runningChange > 0) {
            changeGiven.push([tens[0], parseFloat(tens[1].toFixed(2))]);
        }

    }
    var fives;
    if (runningChange > 1 && changeToGive > 5) {
        fives = cid.find(function (element) {
            return element[0] == "FIVE";
        });
        if (changeToGive > fives[1]) {
            console.log("Fives more" + fives[1]);
        }
        runningChange = parseFloat(
            Math.round((runningChange - fives[1]) * 100) / 100).toFixed(2);
        if (runningChange > 1) {
            changeGiven.push([fives[0], parseFloat(fives[1].toFixed(2))]);
        } else {
            runningChange = parseFloat(
                runningChange - Math.floor(runningChange / 5) * 5).toFixed(2);
            var temp = ((Math.floor(changeToGive / 5) * 5) / 5);
            changeToGive = runningChange;
            changeGiven.push(["FIVE", parseFloat((temp * 5).toFixed(2))]);
            console.log(changeGiven);
        }
    }
    var ones;
    if (runningChange > 1 && changeToGive > 1) {
        ones = cid.find(function (element) {
            return element[0] == "ONE";
        });
        changeGiven.push(
            ["ONE", parseFloat(Math.floor(runningChange).toFixed(2))]);
        runningChange = runningChange - Math.floor(runningChange);
        changeToGive = runningChange;
        console.log(changeGiven);
    }
    if (runningChange * 100 > 24 || ((runningChange * 100)) % 25 == 0) {
        changeGiven.push(["QUARTER",
            parseFloat(Math.floor(runningChange / 0.25) * .25.toFixed(2))]);
        runningChange = runningChange - Math.floor(runningChange / 0.25) * .25;
        changeToGive = runningChange;
        console.log(changeGiven);
    }
    if (runningChange * 100 > 10) {
        changeGiven.push(["DIME",
            parseFloat((Math.floor(runningChange / 0.10) * .10).toFixed(2))]);
        runningChange = runningChange - Math.floor(runningChange / 0.10) * .10;
        changeGiven.push(["PENNY", parseFloat(runningChange.toFixed(2))]);
        console.log(changeGiven);
    }
    
    

So first we calculated how much money we have to return from a cash register. Then we have three code blocks for the dollar denominations. Each block we check if the amount returned is greater than the money inside cashRegister. The tricky part was the calculation of the last .76 cents. We first found out how many quarters we have in that denomination. Then went to the nickel and then to dimes. Finally, the solution needed everything in common float blocks. So before adding to the final array, we made a sanity check to ensure all amounts are on a float.