Skip to content
Permalink
Browse files
reset code
  • Loading branch information
aa7401 committed Jun 12, 2017
1 parent 7914eee commit 3a2c555ac96726be5641d382646093e94d5cc480
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
@@ -2,13 +2,15 @@
'use strict'

const list = require('./modules/shopping')

debugger
list.add('bread')
list.add('butter')
debugger
list.add('cheese')
list.add('bread')
list.add('bread')
list.add('butter')
debugger
try {
list.decrement('bread', 2)
list.decrement('butter', 4)
@@ -2,25 +2,26 @@

const readline = require("readline-sync");

var list = require('./modules/shopping');
const list = require('./modules/shopping');

do {
var input = String(readline.question("enter command: ")).trim()
if (input.indexOf('add ') === 0) {
do
{
var input = String(readline.question("enter command: ")).trim()
if (input.indexOf('add ') == 0) {
let space = input.indexOf(' ');
let item = input.substring(space).trim();
/* Let's echo the item details back to the user before adding it. Notice the use of Template Literals (https://goo.gl/Yjxa5o), a part of ECMA6. This allows you to include placeholders for variables without the need for concatenation. The string must be enclosed by backticks (`) */
console.log(`adding "${item}"`)
/* Let's echo the item details back to the user before adding it. Notice the use of Template Literals (https://goo.gl/Yjxa5o), a part of ECMA6. This allows you to include placeholders for variables without the need for concatenation. The string must be enclosed by backticks (`) */
console.log(`adding "${item}"`)
list.add(item)
}
if (input.indexOf("list") === 0) {
}
if (input.indexOf("list") == 0) {
const items = list.getAll()
let i = 1
let i = 1
for (var value of items) {
/* process.stdout.write prints to the terminal without adding a newline character at the end. the \t character inserts a tab and the \n character inserts a newline */
process.stdout.write(`${i}. ${value.title}`)
process.stdout.write(`\t${value.qty}\n`)
i++
process.stdout.write(`${i}. ${value.title}`)
process.stdout.write(`\t${value.qty}\n`)
i++
}
}
} while (input !== "exit");
@@ -6,18 +6,21 @@
*/

/** this global 'map' object will be used to store the items and quantities. It stores each item against a named key. */
var data = new Map()
var data = new Map();

/** add a new item to the todo list. Notice that we are using the new 'Arrow Function' syntax from the ECMA6 specification. */
exports.add = item => {
exports.add = item => {
/* check if the item is already in the list */
if (data.get(item) === undefined) {
if (data.get(item) == undefined)
{
/* if the item is not found it is added to the list with its quantity set to 1 */
data.set(item, {title: item, qty: 1})
} else {
}
else
{
/* the item is already in the list so it is retrieved and the quantity is incremented */
let i = data.get(item)
i.qty ++
i.qty++
/* the new data is then stored in the map */
data.set(item, i)
}
@@ -26,7 +29,7 @@ exports.add = item => {


/** calculates and returns the number of items in the list */
exports.count = () => {
exports.count = () => {
/* the _Map_ object has a _size_ property that returns the number of items */
return data.size
}
@@ -38,11 +41,11 @@ exports.clear = () => {

/** Returns an array containing all todo items */
exports.getAll = () => {
const dataArray = []
for (const value of data.values()) {
dataArray.push(value)
let data_array = []
for (var value of data.values()) {
data_array.push(value)
}
return dataArray
return data_array
}

/**
@@ -52,12 +55,19 @@ exports.getAll = () => {
* @throws {InvalidItem} item not in list.
*/
exports.getItem = item => {

if (data.get(item) === undefined) {
throw new Error('item not in list')
}
return data.get(item)
}

/** Removes item with the corresponding name. */
exports.removeItem = item => {

console.log('removeItem')
if (data.get(item) === undefined) {
throw new Error('item not in list')
}
data.delete(item)
}

/** Decrements the quantity of an item in the list. */
@@ -5,10 +5,10 @@
"main": "index.js",
"scripts": {
"app": "node index.js",
"debug": "node debug.js",
"lint": "node_modules/.bin/eslint modules/",
"test": "node_modules/.bin/jasmine-node spec --color --verbose --autotest --watch .",
"coverage": "./node_modules/.bin/istanbul cover -x **spec/** -x **index.js** -x **debug.js** jasmine-node spec",
"acceptance": "node_modules/.bin/jasmine-node test/ --verbose"
"coverage": "./node_modules/.bin/istanbul cover -x **spec/** -x **index.js** -x **debug.js** ./node_modules/.bin/jasmine-node spec",
"doc": "node_modules/.bin/jsdoc modules/"
},
"keywords": [
"list",
@@ -22,11 +22,9 @@
},
"devDependencies": {
"eslint": "^3.1.0",
"frisby": "^0.8.5",
"istanbul": "^0.4.0",
"jasmine": "^2.6.0",
"jasmine-console-reporter": "^1.2.7",
"jasmine-node": "^1.14.5",
"jsdoc": "^3.4.0"
"jsdoc": "^3.4.0",
"node-inspector": "^0.12.8"
}
}

0 comments on commit 3a2c555

Please sign in to comment.