mirror of
https://github.com/FranP-code/Show-multiplication-tables-CLI-Tool.git
synced 2025-10-12 23:52:32 +00:00
Project ended
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/node_modules
|
||||||
|
/package-lock.json
|
||||||
78
API/returnMultiplicationTables.js
Normal file
78
API/returnMultiplicationTables.js
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
const express = require('express')
|
||||||
|
const app = express()
|
||||||
|
|
||||||
|
const port = 3001
|
||||||
|
|
||||||
|
app.use(express.json())
|
||||||
|
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
|
||||||
|
const error = (err) => {
|
||||||
|
|
||||||
|
res.status('400').json({
|
||||||
|
|
||||||
|
'status': 'error',
|
||||||
|
'message': err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = req.body
|
||||||
|
|
||||||
|
if (!data) {
|
||||||
|
|
||||||
|
error(`Data not sended`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//!Template
|
||||||
|
/*
|
||||||
|
|
||||||
|
{
|
||||||
|
"numberToMultiply" = 5,
|
||||||
|
"initialNumber" = 0,
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const numberToMultiply = data.numberToMultiply
|
||||||
|
let initialNumber = data.initialNumber
|
||||||
|
let limit = data.limit
|
||||||
|
|
||||||
|
if (!data.hasOwnProperty("numberToMultiply")) {
|
||||||
|
|
||||||
|
error(`Number to multiply don't defined`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof(numberToMultiply) !== 'number') {
|
||||||
|
|
||||||
|
error(`Number to multiply is not a number`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.hasOwnProperty("initialNumber") || typeof(initialNumber) !== 'number' || initialNumber < 0) {
|
||||||
|
|
||||||
|
initialNumber = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.hasOwnProperty("limit") || typeof(limit) !== 'number' || limit < 0 || limit > 999) {
|
||||||
|
|
||||||
|
limit = 10
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = []
|
||||||
|
let i = initialNumber
|
||||||
|
|
||||||
|
while (i <= (initialNumber + limit)) {
|
||||||
|
|
||||||
|
result.push(`${numberToMultiply} x ${i} = ${numberToMultiply * i}`)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
|
||||||
|
'status': 'success',
|
||||||
|
'message': result
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.listen(port)
|
||||||
106
app.js
Normal file
106
app.js
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
const url = 'http://localhost:3001'
|
||||||
|
|
||||||
|
const axios = require('axios')
|
||||||
|
const colors = require('colors')
|
||||||
|
const inquirer = require('inquirer')
|
||||||
|
|
||||||
|
const getData = async (numberToMultiply, initialNumber, limit) => {
|
||||||
|
try {
|
||||||
|
|
||||||
|
const response = await axios({
|
||||||
|
method: 'get',
|
||||||
|
url: url,
|
||||||
|
|
||||||
|
data: {
|
||||||
|
"numberToMultiply": numberToMultiply,
|
||||||
|
"initialNumber": initialNumber,
|
||||||
|
"limit": limit
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return await response.data
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
return await error.response.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function errorMessage(message) {
|
||||||
|
|
||||||
|
console.log(message.brightRed)
|
||||||
|
}
|
||||||
|
|
||||||
|
function titleMessage(message) {
|
||||||
|
|
||||||
|
console.log()
|
||||||
|
console.log(message.bold.cyan)
|
||||||
|
console.log()
|
||||||
|
}
|
||||||
|
|
||||||
|
function commonMessage(message) {
|
||||||
|
|
||||||
|
console.log(message.brightGreen)
|
||||||
|
}
|
||||||
|
|
||||||
|
function thanksForUsingIt() {
|
||||||
|
|
||||||
|
console.log('Thanks for use this tool. Francisco Pessano.'.bold.brightYellow)
|
||||||
|
}
|
||||||
|
|
||||||
|
inquirer
|
||||||
|
.prompt([{
|
||||||
|
|
||||||
|
name: 'numberToMultiplySelection',
|
||||||
|
message: 'Put the number to find your multiplication table.',
|
||||||
|
type: 'number',
|
||||||
|
|
||||||
|
default: 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'initialNumberSelection',
|
||||||
|
message: 'Put the initial number of the table.',
|
||||||
|
type: 'number',
|
||||||
|
|
||||||
|
default: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'limitSelection',
|
||||||
|
message: 'Put a limit (max 999)',
|
||||||
|
type: 'number',
|
||||||
|
|
||||||
|
default: 10
|
||||||
|
}])
|
||||||
|
|
||||||
|
.then(async (answers) => {
|
||||||
|
|
||||||
|
const numberToMultiply = answers.numberToMultiplySelection
|
||||||
|
const initialNumber = answers.initialNumberSelection
|
||||||
|
const limit = answers.limitSelection
|
||||||
|
|
||||||
|
const data = await getData(numberToMultiply, initialNumber, limit)
|
||||||
|
// data.status
|
||||||
|
// data.message
|
||||||
|
|
||||||
|
if (data.status === 'error') {
|
||||||
|
|
||||||
|
errorMessage(data.message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.status !== 'success') {
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
titleMessage('Here are your results:')
|
||||||
|
|
||||||
|
data.message.forEach(count => {
|
||||||
|
|
||||||
|
commonMessage(` - ` + count)
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log()
|
||||||
|
thanksForUsingIt()
|
||||||
|
console.log()
|
||||||
|
})
|
||||||
21
package.json
Normal file
21
package.json
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "mostrar-tablas-de-multiplicar-cli",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "app.js",
|
||||||
|
"scripts": {
|
||||||
|
"api": "nodemon ./API/returnMultiplicationTables.js",
|
||||||
|
"game": "node app.js"
|
||||||
|
},
|
||||||
|
"author": "FranP-Code",
|
||||||
|
"license": "ISC",
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^2.0.15"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "^0.24.0",
|
||||||
|
"colors": "^1.4.0",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"inquirer": "^8.2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user