Last active
July 17, 2017 18:57
-
-
Save pareddy113/fe51bba0f96eb427233451bc595817cd to your computer and use it in GitHub Desktop.
learnyounode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Write a program that accepts one or more numbers as command-line arguments | |
and prints the sum of those numbers to the console (stdout).*/ | |
var array = process.argv; | |
var sum = 0; | |
for(var i = 2; i < array.length ; i++){ | |
sum+= Number(array[i]); | |
} | |
console.log(sum); | |
/* | |
Write a program that uses a single synchronous filesystem operation to | |
read a file and print the number of newlines (\n) it contains to the | |
console (stdout), similar to running cat file | wc -l. | |
*/ | |
var fs = require('fs'); | |
var path = process.argv[2]; | |
var readFile = fs.readFileSync(path); | |
var string = readFile.toString(); | |
var array = string.split("\n"); | |
console.log(array.length - 1); | |
var fs = require('fs'); | |
var numberOfNewLines = fs.readFileSync(process.argv[2],'utf8').split("\n").length; | |
console.log(numberOfNewLines-1); | |
/*Write a program that uses a single asynchronous filesystem operation to | |
read a file and print the number of newlines it contains to the console | |
(stdout), similar to running cat file | wc -l.*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment