Commit 47dcde4d authored by Nabhan Abdulla P V's avatar Nabhan Abdulla P V 💬
Browse files

update post first session

parent 1b0205d3
......@@ -16,15 +16,15 @@ console.log(scores.length)
/*
* Looping over arrays
*
* 1st match income -> 1 x runs
* 2nd match income -> 2 x runs
* 3rd match income -> 3 x runs
*/
// let totalScores = 0;
// for (let i = 0; i < scores.length; i++) {
// console.log(scores[i] * (i + 1)); // 1st match -> Index = 0
// let currScore = scores[i];
// totalScores += currScore;
// }
// console.log(totalScores);
/*
* Manipulating arrays
*/
......@@ -32,7 +32,6 @@ console.log(scores.length)
// updating elements
// scores[0] = 21; // replaces 20
// scores[10] = 0; // throws error as index 11 isn't availabel
// console.log(scores)
......
......@@ -16,7 +16,8 @@ let outputArr = [];
for (let i = 0; i < arr.length; i++) {
// check if index is 0
if (i === 0) {
outputArr.push(arr[i]);
let currElement = arr[i];
outputArr.push(currElement);
} else {
let currElement = arr[i];
......@@ -25,6 +26,6 @@ for (let i = 0; i < arr.length; i++) {
outputArr.push(currValue);
}
};
}
console.log(outputArr); // should print [1, 3, 5, 7, 9]
\ No newline at end of file
......@@ -16,6 +16,7 @@ promise
// }
// let promiseAsync = longRunning(); // outputs a Promise object
// console.log(promiseAsync);
// console.log("hi")
// promiseAsync
......@@ -35,12 +36,18 @@ promise
// return "longRunning";
// }
// Handling resolved case - .then()
// async function main() {
// let promise = longRunning();
// let output = await promise;
// console.log(output);
// }
// Handling rejected case as well - .catch()
// async function main() {
// console.log("hi");
// try {
// let msg = await longRunning();
// console.log(msg);
// console.log("bye");
// } catch (err) {
// console.log(err);
// }
......@@ -68,13 +75,13 @@ promise
// return "Promise1";
// }
// async function promise2() {
// return "Promise2";
// async function promise2(msg) {
// return "Promise2: " + msg;
// }
// async function main() {
// let msg1 = await promise1();
// let msg2 = await promise2(); // line is executed only after promise on prev line is fulfilled
// let msg2 = await promise2(msg); // line is executed only after promise on prev line is fulfilled
// console.log(msg1);
// console.log(msg2);
......
......@@ -14,11 +14,11 @@ async function asyncFn2() {
async function main() {
try {
let promise1 = await asyncFn1();
console.log(promise1);
let msg1 = await asyncFn1();
console.log(msg1);
let promise2 = await asyncFn2();
console.log(promise2);
let msg2 = await asyncFn2();
console.log(msg2);
} catch (err) {
console.log(err);
}
......
......@@ -7,32 +7,27 @@ const fetch = require("node-fetch");
// What does "https://api.github.com/users" return? - Try visiting from browser
// Fetch Response object
fetch(`https://api.github.com/users`).then((res) => console.log(res));
async function main() {
let promise = fetch("https://api.github.com/users");
console.log(promise);
// Check if response was successful - status codes in [200, 299]
fetch(`https://api.github.com/users`).then((res) => console.log(res.ok));
// let res = await promise; // returns Response object
// console.log(res);
// Check if response was successful - status codes in [200, 299]
// console.log(res.ok);
// let data = await res.json(); // json() method as well returns a promsie
// console.log(data);
// console.log(data[0]);
// console.log(data[0].login);
}
main();
/*
* Parsing response data in JSON format to JS object
*/
// Using .then().catch()
// fetch(`https://api.github.com/users`)
// .then((res) => res.json())
// .then((data) => console.log(data))
// .catch((error) => console.log(error));
// Using async-await
async function main() {
try {
let res = await fetch(`https://api.github.com/users`);
let data = await res.json();
// print *array* of user *objects* returned
console.log(data);
} catch (err) {
console.log(err);
}
}
main();
......@@ -2,15 +2,6 @@
* Creating functions
*/
// Need for functions
let p1 = 10;
let p2 = 20;
console.log(p1 + p2);
let p3 = 5;
let p4 = 6;
console.log(p3 + p4);
// Function declaration syntax
// function add(p1, p2) {
// // Logic goes here
......@@ -23,12 +14,15 @@ console.log(p3 + p4);
/*
* Functions as first-class citizens
*/
// Passing functions around as values
// let addDuplicate = add;
// console.log(addDuplicate(10, 20))
// Function expression
let myAddFn = function (p1, p2) {
return p1 + p2;
};
console.log(myAddFn(10, 20));
// let myAddFn = function (p1, p2) {
// return p1 + p2;
// };
// console.log(myAddFn(10, 20));
/*
* Note -
......
......@@ -4,8 +4,8 @@
* - Callback function prints to the console output of the addition
*/
function add(p1, p2, callback) {
let res = p1 + p2;
callback(res);
let sum = p1 + p2;
callback(sum);
}
function cb(msg) {
......
......@@ -2,7 +2,7 @@
* for-loops vs map
*/
// scorecard of Kholi's batting performance over a 5-match ODI series
// scorecard of Kohli's batting performance over a 5-match ODI series
let runsScored = [20, 100, 53, 44, 21];
// calculate match income -> 10 x runs scored
......@@ -38,11 +38,7 @@ console.log(incomeArray);
// find match scores with atleast a 50
// fiftyScores = runsScored.filter(currScore => currScore >= 50); // callback function returns if element satisfied condition
// same as
// fiftyScores = runsScored.filter((currScore) => {
// return currScore >= 50;
// });
// console.log(fiftyScores);
// same as
// fiftyScores = runsScored.filter(function (currScore) {
......
......@@ -8,4 +8,10 @@ let names = ["Anil", "Dhoni", "Kohli", "Aaron", "Ajinkya"];
// store names starting with "A"
let namesStartingWithA;
console.log(namesStartingWithA); // should print [ 'Anil', 'Aaron', 'Ajinkya' ]
/*
* Hint:
* let currElem = "Anil";
* currElem[0] === "A"; // evaluates to true
*/
console.log(namesStartingWithA); // should print [ 'Anil', 'Aaron', 'Ajinkya' ]
\ No newline at end of file
......@@ -13,6 +13,7 @@ console.log(scorecard);
// objects defined using Object constructor
// let scorecardConstructor = new Object();
// console.log(scorecardConstructor);
// scorecardConstructor.runs = 329;
// scorecardConstructor.wicketsLost = 9;
......@@ -46,16 +47,17 @@ console.log(scorecard);
/*
* "this" keyword and functions as values
*/
// let scorecard = {
// runs: 329,
// wicketsLost: 9,
// team: "India",
// summary: this.team + " scored " + this.runs + " runs!",
// chant: function () {
// return "Go India, go!";
// },
// };
let scorecard = {
runs: 329,
wicketsLost: 9,
team: "India",
chant: function () {
return "Go " + this.team + ", go!";
},
};
// console.log("Team " + scorecard.team + " scored " + scorecard.runs);
// console.log(scorecard);
// console.log(scorecard.summary);
// console.log(scorecard.chant());
\ No newline at end of file
// let chant = scorecard.chant();
// console.log(chant);
\ No newline at end of file
......@@ -14,9 +14,7 @@
// }, 1000); // takes ~1s to complete
// };
// console.log("Start execution");
// longRunning(); // don't wait for this function to complete
// console.log("Continue execution");
// });
// console.log(promise);
......@@ -44,9 +42,7 @@
// }, 2000); // takes ~2s to complete
// };
// console.log("Start execution");
// longRunning1(); // don't wait for this function to complete
// console.log("Continue execution");
// });
// let promise2 = new Promise(function (resolve, reject) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment