Posts

Underscore Module

// UNDERSCORE MODULE // import underscore var _ = require('underscore'); // An array of many numbers var myArray = [12,32,7,2,445,21,2,3,87,34,32,544,23,523]; // let's find out numbers greater than 20 // and store them in results array var results = _.filter(myArray, function(item){     return item > 20; }); // display result console.log(results); var mySecondArray = [1,2,3,4,5,6,7,8,9]; var mySecondResults = _.reject(mySecondArray, function(item){     return (item % 2 != 0); }); // print second array console.log(mySecondResults);

setTimeout

setTimeout(function(){     console.log('Hello!'); }, 2000);  

setIntervalGlobal

// setInterval wit global update var x = 2; setInterval(function(){     // print number of apples     console.log('I have ' + x + ' apples right now.');     // increment by 1     x = x + 1;     console.log('I added another apple.');     // show result     console.log('Now I have ' + x + ' apples.'); }, 1000);

setInterval

// setInterval setInterval(function(){     var x = 1;     console.log('I have ' + x + ' apples.');     x = x + 1; }, 1000);

FS Module

// FS MODULE var fs = require('fs'); // write a file using fs var myText = 'This is a sample statement'; fs.writeFileSync('anything.txt', myText); // read a file fs.readFile('./anything.txt', function(err, data){     if(err){         console.error('Error is ' + err);     } else {         console.log(data.toString());     } }); // delete a file fs.unlink('./anything.txt', function(err){     if(err){         console.error('Error message: ' + err);     } else {         console.log('The file was successfully deleted.');     } });

Closure

// CLOSURE function outerFunction(){         // outer variable     var x = 20;     function innerFunction(someArgument){         // inner variable         var y = 10;         console.log('Outer variable is ' + x + ' and argument is ' + someArgument);     }     outerFunction.innerFunction = innerFunction; } // call the outer function outerFunction.innerFunction(50);

Lesson 3 Chat

Today we'll do a little more on FS module and then move on to External Node Modules Are you both here??? Ok... lets begin... So where exactly to begin.. In the last session we saw how to read a file using FS, today we'll do how to: Read, Write, Delete those files ... Lets start.. console.log are used usually to show messages or information... console.error(); specially used for showing errors.. of course we can also use console.log for errors but that's not the standard way... These two lines are enough to write to a file. The first argument is the filename and the second one is the content of the file we want to write.. Let's run it... Note that right now, we have only one file in the "Lesson 3" folder... After writing, there'll be one more file with the name anything.txt NOTE: In Windows, the command is "node filename" In Linux, it is: "nodejs filename" Good question renu! the anything.txt file got auto-created.... Let's try to d...