Monday, January 24, 2022

fibonacci series of first 10 element

                 int f1 = 0, f2 = 1;

int f3 = 0;

System.out.print(f1+" ");

System.out.print(f2+" ");

for (int i = 1; i <=8; i++) {

f3 = f1 + f2;

System.out.print(f3+" ");

f1 = f2;

f2 = f3;

}

Sunday, January 23, 2022

frequency of integer

 package arrayoperations;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class MainApp {

public static void main(String[] args) {

int[] a = { 1, 2, 2, 3, 4, 4, 4 };

Map<Integer, Integer> frequency = new HashMap();

for (int i = 0; i < a.length; i++) {

if (frequency.containsKey(a[i])) {

frequency.put(a[i], frequency.get(a[i]) + 1);

} else {

frequency.put(a[i], 1);

}

}

Set<Integer> keySet = frequency.keySet();

for(Integer i: keySet) {

System.out.println("item: "+ i+ " frequency: "+frequency.get(i));

}

}

}

Tuesday, January 11, 2022

fetch() web API to load data from server

 fetch

    ('https://crossorigin.me/https://www.metaweather.com/api/location/2487956/')

    .then(result => {

        console.log(result);

        return result.json(); //fetch () will return a promise and converting to json object

    })

     .then(data=>console.log(data))

    .catch(error => {

        console.log(error);

    });

here https://crossorigin.me is used to prevent same origin policy.

Wednesday, January 5, 2022

Ajax

 


Ajax is used to load data from the server asynchoronously  without reloading the entire page 
using http get request . Ajax also used to send data using http post request. 

Tuesday, January 4, 2022

How to consume promise when returning a resolved value

 const ids = new Promise((resolve, reject) => { //promise 1

    setTimeout(() => {

        resolve([100, 200, 300, 400, 500]);

    }, 1500);

});

const getReceipe = recId => {  //promise 2

    return new Promise((resolve, reject) => {

        setTimeout(id => {

            const receipe = {

                title: 'potato',

                publisher: 'jonas'

            };

            resolve(`${id}:${receipe.title}`);

        }, 1500, recId);

    });

};


async function getReceipeAW() {

    const allIds = await ids; // here ids is the promise . await keyword is used to wait 

                                          //until the promise resolved

    console.log(allIds);

    const receipe = await getReceipe(allIds[2]);

    console.log(receipe);

    return receipe;

}

getReceipeAW().then(result=>{   // here getReceipeAW() will return a promise then

                                                      //  we need to consume it.

    console.log(`${result} is the best food ever`);

})

Async and await

 --async and await is used to consume promises , not to produce them .

--async and await makes easier to consume promise over then and catch .

--await can only be used inside async function.

--an async function is always returns a promise automatically.


Example:

const ids = new Promise((resolve, reject) => { //promise 1

    setTimeout(() => {

        resolve([100, 200, 300, 400, 500]);

    }, 1500);

});

const getReceipe = recId => {  //promise 2

    return new Promise((resolve, reject) => {

        setTimeout(id => {

            const receipe = {

                title: 'potato',

                publisher: 'jonas'

            };

            resolve(`${id}:${receipe.title}`);

        }, 1500, recId);

    });

};

//Consuming promises

//Async & await

async function getReceipeAW() {  //here async is the keyword

    const allIds = await ids; // here ids is the promise . await keyword is used to wait 

                                          //until the promise resolved

    console.log(allIds);

    const receipe = await getReceipe(allIds[2]);

    console.log(receipe);

}

getReceipeAW();



Sunday, January 2, 2022

A promise can return another promise

 Let's see the below example,

const ids = new Promise((resolve, reject) => {

    setTimeout(() => {

        resolve([100, 200, 300, 400, 500]);

    }, 1500);

});

const getReceipe = recId => {

    return new Promise((resolve, reject) => {

        setTimeout(id => {

            const receipe = {

                title: 'potato',

                publisher: 'jonas'

            };

            resolve(`${id}:${receipe.title}`);

        }, 1500, recId);

    });

};

ids.then(ids => {

        console.log(ids);

        return getReceipe(ids[2]);//returning another promise object

    })

    .then(receipe => {

        console.log(receipe);

    })

    .catch(error => {

        console.log(error);

    })

Saturday, January 1, 2022

Promises in javascript

 Promises is an object that keeps track of some event is happened or not .

--Determines what happens after the event has happened.

Promise have different states -


Example :                                                                                                        
const ids = new Promise((resolve, reject) => {
setTimeout(() => {
                                                                                         resolve([100, 200, 300, 400, 500]);
                                         }, 1500);
                 });
//Consuming promises
                                    ids.then(ids=>{//ids will store all the result
   console.log(ids); 
})

                                                 .catch(error=>{//catch is used if error has occured
   console.log(error); 
});





    





Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...