Last active
October 5, 2015 20:18
-
-
Save SBejga/c742199592eb86427bda to your computer and use it in GitHub Desktop.
DHBW Mobile Web Dev - Practice Student Array Readline
This file contains 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
//import from node.js core to read from stdin | |
var readline = require('readline'); | |
//Configure Readline | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
//Init Array for Students | |
var studentArray = []; | |
//Student Model as Module | |
var Student = require('./student.js'); | |
//Just a Welcome Message... | |
console.log("Hello, my name is Siri. I will assist you to add students"); | |
/** | |
* Ask two questions via readline, print resulting array and restart again by calling itself | |
* two readlines are nested, because they are async. | |
*/ | |
var askUser = function () { | |
console.log("We will create new Student. Please answer two questions. Or just type 'exit'"); | |
rl.question("Enter Firstname: ", function(answer) { | |
if (checkExit(answer)) { | |
return; | |
} | |
var newStudent = new Student(); //no params, means they get value undefined | |
newStudent.first = answer; | |
rl.question("Enter Lastname: ", function(answer) { | |
if (checkExit(answer)) { | |
return; | |
} | |
newStudent.last = answer; | |
studentArray.push(newStudent); | |
//sort array | |
// provide function 'studentCompare' which can compare student objects | |
// refer to @ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | |
studentArray.sort(studentCompare) | |
//Print out | |
printArray(); | |
//And restart | |
askUser(); | |
}); | |
}); | |
}; | |
/** | |
* Compare two objects by string compare of its property 'last' | |
* which should be the lastName in this context. | |
* @param a | |
* @param b | |
* @returns {number} | |
*/ | |
var studentCompare = function (a, b) { | |
//a is less than b by some ordering criterion | |
if (a.last < b.last) { | |
return -1; | |
} | |
//a is greater than b by the ordering criterion | |
if (a.last > b.last) { | |
return 1; | |
} | |
// a must be equal to b | |
return 0; | |
} | |
/** | |
* check if answer parameter === 'exit' then exit readline | |
* @param answer | |
* @returns {boolean} | |
*/ | |
var checkExit = function(answer) { | |
if (answer === "exit" || answer === "quit") { | |
console.log("Goodbye. See you next time..."); | |
rl.close(); | |
return true; | |
} | |
return false; | |
}; | |
/** | |
* print contents of Array 'studentArray' to Console | |
*/ | |
var printArray = function () { | |
var str = '['; | |
for (var index in studentArray) { | |
if (index > 0) { | |
str += ", " | |
} | |
var curStudent = studentArray[index]; | |
str += curStudent.print(); | |
} | |
str += ']'; | |
console.log(str); | |
}; | |
//Start asking | |
askUser(); |
This file contains 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
//Constructor | |
// will be called with 'new' | |
var Student = function(first, last, mail) { | |
this.first = first; | |
this.last = last; | |
this.mail = mail; | |
}; | |
//Prototype | |
Student.prototype = { | |
print: function() { | |
return (this.first + " " + this.last); | |
}, | |
print2: function() { | |
return (this.last + ", " + this.first); | |
} | |
}; | |
module.exports = Student; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment