Skip to content

Commit ba4bf05

Browse files
committed
first commit
0 parents  commit ba4bf05

File tree

10 files changed

+2088
-0
lines changed

10 files changed

+2088
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Setup
2+
npm install
3+
4+
## Test
5+
npm test
6+
7+
## Description
8+
This repo is designed to give students a better understanding of how to use callbacks to deal with async operations in Javascript. Complete each of the exercises in the exercises directory and test your code!

exercises/parse.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const fs = require("fs");
2+
/* ASYNC-TEXT-PARSE
3+
In this exercise, you need to utilize the provided functions as callbacks so that the
4+
tests pass. The input file contains space-separated numbers on multiple lines of a
5+
.txt file. Just by filling in the appropriate callbacks, you should be able to
6+
generate an output.txt file of booleans reflecting whether the sum of the numbers on
7+
a line add up to more or less than one hundred. You must also invoke the callback
8+
passed to initiateParse so that it runs after you have written your output file.
9+
10+
EXAMPLE:
11+
12+
input.txt
13+
50 43 10
14+
10 20 30
15+
50 40 0
16+
20 10 0
17+
20 40 30
18+
10 65 20
19+
43 60 20
20+
123 0 40
21+
122 44 0
22+
1 2 3
23+
24+
output.txt
25+
true
26+
false
27+
false
28+
false
29+
false
30+
false
31+
true
32+
true
33+
true
34+
false
35+
36+
/* ---------------------------- */
37+
38+
// replace references to this function
39+
function PLACEHOLDER() {};
40+
41+
// call the callback to initiateParse so that it only runs after parsing is complete
42+
function initiateParse(callback) {
43+
fs.readFile(__dirname + '/../input.txt', 'utf8', PLACEHOLDER);
44+
45+
function writeOutput(err, data) {
46+
let output = '';
47+
for (let i = 0; i < data.length; i++) {
48+
output += data[i];
49+
if (i !== data.length - 1) {
50+
output += '\n';
51+
}
52+
}
53+
54+
fs.writeFile(__dirname + '/../output.txt', output, function(err) {
55+
if (err) throw err;
56+
});
57+
};
58+
59+
function calculateLines(data, cb) {
60+
let filteredData = data.map(function(el) {
61+
let values = el.split(' ');
62+
let isGreaterThanHundred = false;
63+
let runningTotal = 0;
64+
for (let i = 0; i < values.length; i++) {
65+
runningTotal += parseInt(values[i]);
66+
}
67+
if (runningTotal > 100) {
68+
isGreaterThanHundred = true;
69+
}
70+
return isGreaterThanHundred;
71+
});
72+
setTimeout(function() {
73+
cb(null, filteredData);
74+
}, 100);
75+
};
76+
77+
function splitLines(err, data) {
78+
if (err) throw err;
79+
let input = data.toString().split('\n');
80+
calculateLines(input, PLACEHOLDER);
81+
};
82+
83+
}
84+
85+
module.exports = initiateParse;

exercises/request.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var fs = require('fs');
2+
var http = require('http');
3+
4+
5+
// The get request in this function retrieves data on Harry Potter characters from an API.
6+
// The parsedData received from this API will be an array containing character objects.
7+
// Each character object has several properties. The only properties relevant to us are
8+
// the 'house' property and the 'name' property. You should use fs.readFile to get the
9+
// name of a house from house.txt, and then construct an array containing the names of all
10+
// characters wtih a house property matching the house found in house.txt. Pass this array
11+
// into the getStudents callback to pass the tests.
12+
function getStudents(callback) {
13+
http.get({
14+
host: 'hp-api.herokuapp.com',
15+
path: '/api/characters/students'
16+
}, function(response) {
17+
// Continuously update stream with data
18+
var body = '';
19+
response.on('data', function(d) {
20+
body += d;
21+
});
22+
response.on('end', function() {
23+
// Data reception is done, do whatever with it!
24+
var parsedData = JSON.parse(body);
25+
var pathToHouseFile = __dirname + '/../house.txt';
26+
27+
// CODE HERE
28+
// fs.readFile(pathToHouseFile, 'utf8'
29+
});
30+
});
31+
}
32+
33+
module.exports = getStudents;

exercises/sentence.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// DO NOT EDIT async function that gets the first word and passes it to its callback
2+
function getFirstWord(callback) {
3+
setTimeout(function() {
4+
callback('Async ');
5+
}, 90);
6+
};
7+
8+
// DO NOT EDIT async function that gets the second word and passes it to its callback
9+
function getSecondWord(callback) {
10+
setTimeout(function() {
11+
callback('is ');
12+
}, 30);
13+
};
14+
15+
// DO NOT EDIT async function that gets the third word and passes it to its callback
16+
function getThirdWord(callback) {
17+
setTimeout(function() {
18+
callback('awesome.');
19+
}, 60);
20+
};
21+
22+
// use the above functions to complete the sentence and pass it to this function's callback
23+
function getSentence(callback) {
24+
// CODE HERE
25+
}
26+
27+
module.exports = getSentence;

house.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ravenclaw

input.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
50 43 10
2+
10 20 30
3+
50 40 0
4+
20 10 0
5+
20 40 30
6+
10 65 20
7+
43 60 20
8+
123 0 40
9+
122 44 0
10+
1 2 3

0 commit comments

Comments
 (0)