Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/exercises/06_testing/shopping/index.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
27 lines (24 sloc)
1.04 KB
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
"use strict" | |
const readline = require("readline-sync"); | |
const list = require('./modules/shopping'); | |
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}"`) | |
list.add(item) | |
} | |
if (input.indexOf("list") == 0) { | |
const items = list.getAll() | |
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++ | |
} | |
} | |
} while (input !== "exit"); |