diff --git a/mine-practice/app.js b/mine-practice/app.js index 4b4a9680..5dad16e2 100644 --- a/mine-practice/app.js +++ b/mine-practice/app.js @@ -1,46 +1,6 @@ -//#6 Modules and require -//for dividing code into logical modules i.e. separate files. -//our applications can be divided into logical files. +//#7 +var stuff = require('./stuff');//what is exported is returned and stored in counter variable. -var counter = require('./count');//what is exported is returned and stored in counter variable. - -console.log(counter(['shaun','crystal','ryu'])); -//#4 -// var time = 0; -// var timer = setInterval(function(){ -// time += 2; -// console.log(time + ' seconds have passed'); -// if (time > 5){ -// clearInterval(timer); -// } -// },2000); - - -// console.log(__dirname); -// console.log(__filename); - - -//#5 Function Expressions -function callFunction(fun){ - fun(); -} -//normal function statement - -function sayHi(){ - console.log('Hi'); -} - -sayHi(); - -//function expression is assigning a variable to an anonymous function - -var sayBye = function(){ - console.log('bye'); - -}; - -sayBye(); - -callFunction(sayBye); - -callFunction(sayHi); \ No newline at end of file +console.log(stuff.counter(['shaun','crystal','ryu'])); +console.log(stuff.adder(4,5)); +console.log(stuff.adder(stuff.pi,5)); diff --git a/mine-practice/count.js b/mine-practice/count.js deleted file mode 100644 index 5b894cd0..00000000 --- a/mine-practice/count.js +++ /dev/null @@ -1,9 +0,0 @@ -var counter = function(arr){ - return 'There are ' + arr.length + ' elements in this array'; -}; - -//need to explicitly say what part of this module we want available outside this module - -module.exports = counter; - -// console.log(counter(['shaun','crystal','ryu'])); \ No newline at end of file diff --git a/mine-practice/stuff.js b/mine-practice/stuff.js new file mode 100644 index 00000000..0207ac4d --- /dev/null +++ b/mine-practice/stuff.js @@ -0,0 +1,34 @@ +var counter = function(arr){ + return 'There are ' + arr.length + ' elements in this array'; +}; +var adder = function(a,b){ + return `The sum of the 2 numbers is ${a+b}`; +}; + +var pi = 3.142; +// //need to explicitly say what part of this module we want available outside this module +// //module.exports is an empty object, which we can use dot notation to add properties to it + +module.exports.counter = counter; +module.exports.adder = adder; +module.exports.pi = pi; + +// console.log(counter(['shaun','crystal','ryu'])); + +//ANOTHER WAY IS TO ASSIGN THE VALUES/FUNCTIONS TO MODULE.EXPORTS.PROPERTIES + +// module.exports.counter = function(arr){ +// return 'There are ' + arr.length + ' elements in this array'; +// }; +// module.exports.adder = function(a,b){ +// return `The sum of the 2 numbers is ${a+b}`; +// }; + +// module.exports.pi = 3.142; + +//A THIRD APPROACH IS TO ASSIGN MODULE.EXPORTS TO A JAVASCRIPT OBJECT +// module.exports = { +// counter: counter, +// adder: adder, +// pi: pi +}; \ No newline at end of file