vendingm
Version:
This is a Vending Machine type module. If you want to change or add stuff to the Vending machine list then do "VM.machine.push({name : 'string', price : number})". That will add an item. To change an item do "VM.machine[INSERT_THE_ARRAY_NUMBER].name/price
47 lines (45 loc) • 1.46 kB
JavaScript
const chalk = require('chalk')
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
let VM = {
machine: [
{
name: 'example',
price: 100
}
],
money: 100000000,
func: () => {
VM.machine.forEach((el, i) => {
console.log(chalk.whiteBright(`${i + 1}: ${el.name}, ${el.price}`))
});
rl.question(chalk.whiteBright(`Pick one`), (choice) => {
if (choice <= 0) {
} else {
choice--;
}
const tell = (name, price) => {
VM.money -= price;
console.log(chalk.greenBright(`You have bought ${name}. Your balance ${VM.money}`))
rl.close();
}
VM.machine.forEach((el, i) => i == choice ? VM.money >= el.price ? tell(el.name, el.price) : console.log(chalk.redBright(`Not enough money`)) : 0);
})
},
random : (limit, tf) => {
Number(limit);
if (tf == true) {
return Math.floor(Math.random() * limit)
} else {
if (tf == false) {
return Math.random() * limit
} else {
console.log(chalk.redBright(`Err: Use false or true.`))
}
}
},
}
module.exports = VM