Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
public_content
JS Basics 2
Commits
47dcde4d
Commit
47dcde4d
authored
4 years ago
by
Nabhan Abdulla P V
💬
Browse files
Options
Download
Email Patches
Plain Diff
update post first session
parent
1b0205d3
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
72 additions
and
76 deletions
+72
-76
arrays/arrays.js
arrays/arrays.js
+5
-6
arrays/arraysActivity1Solution.js
arrays/arraysActivity1Solution.js
+3
-2
asyncAwait/asyncAwait.js
asyncAwait/asyncAwait.js
+12
-5
asyncAwait/asyncAwaitActivity1Solution.js
asyncAwait/asyncAwaitActivity1Solution.js
+4
-4
fetch/fetch.js
fetch/fetch.js
+17
-22
functions/functions.js
functions/functions.js
+7
-13
functions/functionsActivity1Solution.js
functions/functionsActivity1Solution.js
+2
-2
higherOrderFunctions/higherOrderFunctions.js
higherOrderFunctions/higherOrderFunctions.js
+2
-6
higherOrderFunctions/higherOrderFunctionsActivity1.js
higherOrderFunctions/higherOrderFunctionsActivity1.js
+7
-1
objects/objects.js
objects/objects.js
+13
-11
promises/promises.js
promises/promises.js
+0
-4
No files found.
arrays/arrays.js
View file @
47dcde4d
...
...
@@ -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)
...
...
This diff is collapsed.
Click to expand it.
arrays/arraysActivity1Solution.js
View file @
47dcde4d
...
...
@@ -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
This diff is collapsed.
Click to expand it.
asyncAwait/asyncAwait.js
View file @
47dcde4d
...
...
@@ -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);
...
...
This diff is collapsed.
Click to expand it.
asyncAwait/asyncAwaitActivity1Solution.js
View file @
47dcde4d
...
...
@@ -14,11 +14,11 @@ async function asyncFn2() {
async
function
main
()
{
try
{
let
promise
1
=
await
asyncFn1
();
console
.
log
(
promise
1
);
let
msg
1
=
await
asyncFn1
();
console
.
log
(
msg
1
);
let
promise
2
=
await
asyncFn2
();
console
.
log
(
promise
2
);
let
msg
2
=
await
asyncFn2
();
console
.
log
(
msg
2
);
}
catch
(
err
)
{
console
.
log
(
err
);
}
...
...
This diff is collapsed.
Click to expand it.
fetch/fetch.js
View file @
47dcde4d
...
...
@@ -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
();
This diff is collapsed.
Click to expand it.
functions/functions.js
View file @
47dcde4d
...
...
@@ -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 -
...
...
This diff is collapsed.
Click to expand it.
functions/functionsActivity1Solution.js
View file @
47dcde4d
...
...
@@ -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
)
{
...
...
This diff is collapsed.
Click to expand it.
higherOrderFunctions/higherOrderFunctions.js
View file @
47dcde4d
...
...
@@ -2,7 +2,7 @@
* for-loops vs map
*/
// scorecard of K
h
oli's batting performance over a 5-match ODI series
// scorecard of Ko
h
li'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) {
...
...
This diff is collapsed.
Click to expand it.
higherOrderFunctions/higherOrderFunctionsActivity1.js
View file @
47dcde4d
...
...
@@ -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
This diff is collapsed.
Click to expand it.
objects/objects.js
View file @
47dcde4d
...
...
@@ -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
This diff is collapsed.
Click to expand it.
promises/promises.js
View file @
47dcde4d
...
...
@@ -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) {
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment