Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
9 changes: 9 additions & 0 deletions greet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const greeting = require('./lib/greeting.js');

// set name as user inputed command line argument following 'node' and filename
var name = process.argv[process.argv.length - 1];

greeting.welcome(name);
greeting.farewell(name);
Copy link
Copy Markdown

@kaylynyuh kaylynyuh Dec 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of process.argv! You could have also set var name = process.argv[2]

With node, the first element in the argv array on the global process object is literally 'node' followed by the filename you wish to run as the second element at the 1st index of the argv array, leaving process.argv[2] to represent your command line argument you wish to use for var name. For instance: node index.js

being located at and represented by process.argv[2] :)

17 changes: 17 additions & 0 deletions lib/greeting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

module.exports = exports = {};

exports.welcome = function(name) {
if (/[^a-z]/i.test(name)) throw new Error('non-alphabetical characters not allowed, or no name provided');
if (arguments.length === 0) throw new Error('no name provided');
var welcomeMessage = `Hello and welcome, ${name}! Glad you can be here!`;
console.log(welcomeMessage);
return welcomeMessage;
};

exports.farewell = function(name) {
var farewellMessage = `Glad you could stay for a while, ${name}! Bye and see you next time!`;
console.log(farewellMessage);
return farewellMessage;
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stoked to see some RegEx in there to take care of another possible error!

19 changes: 19 additions & 0 deletions test/greeting-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const greeting = require('../lib/greeting.js');

const assert = require('assert');

describe('Greeting', function() {
describe('#welcome', function() {
it('should print a greeting message customized with the user\'s inputted name.', function() {
var result = greeting.welcome('Caleb');
assert.ok(result === 'Hello and welcome, Caleb! Glad you can be here!', 'greeting not printed as expected');
});
it('should throw an error indicating a missing name or unexpected charater', function() {
assert.throws(function() {
greeting.welcome();
}, 'error not thrown');
});
});
});