Created
October 4, 2019 04:25
-
-
Save Thersis94/2f8b18a21a38253a0365c329d3f9636c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const express = require("express"); | |
const app = express(); | |
const morgan = require("morgan"); | |
app.use(morgan("dev")); | |
app.listen(8000, () => { | |
console.log("Express server is listening on port 8000!"); | |
}); | |
app.get("/sum", (req, res) => { | |
console.log(req.query); | |
const a = parseInt(req.query.a); | |
const b = parseInt(req.query.b); | |
if (!a && a != 0) { | |
return res.status(400).send("Please provide two numbers."); | |
} | |
if (!b && b != 0) { | |
return res.status(400).send("Please provide two numbers."); | |
} | |
const c = a + b; | |
res.send(`The sum of ${a} and ${b} is ${c.toString()}`); | |
}); | |
app.get("/cipher", (req, res) => { | |
const text = req.query.text; | |
const shift = parseInt(req.query.shift); | |
if(!text) { | |
return res.status(400).send("Make sure that you submit a some text"); | |
} | |
if (shift === NaN) { | |
return res.status(400).send("Please make sure that 'shift' is a number"); | |
} | |
const newTextArr = []; | |
for (let i = 0; i < text.length; i++) { | |
let char = text[i]; | |
char = char.charCodeAt(0) + shift; | |
char = String.fromCharCode(char); | |
newTextArr.push(char); | |
} | |
res.send(newTextArr.join("")); | |
}); | |
function hasDuplicates(array) { | |
return new Set(array).size !== array.length; | |
} | |
function getRandomArr(min, max) { | |
const winningArr = []; | |
for (let i = 0; i < 6; i++) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
winningArr.push(Math.floor(Math.random() * (max - min)) + min); | |
} | |
return winningArr; | |
} | |
app.get("/lotto", (req, res) => { | |
let arr = req.query.arr.map(Number) | |
const winningNumbers = getRandomArr(1, 20); | |
let score = 0; | |
if (hasDuplicates(arr)) { | |
return res.status(400).send("All numbers need to be unique"); | |
} | |
for (let i = 0; i < arr.length; i++) { | |
if(winningNumbers.includes(arr[i])) { | |
score += 1 | |
} | |
} | |
if(score === 0) { | |
res.send("Sorry, you lose."); | |
} | |
if(score === 4) { | |
res.send("Congratulations, you win a free ticket!") | |
} | |
if(score === 5) { | |
res.send("Congratulations! You win $100!") | |
} | |
if(score === 6) { | |
res.send("Wow! Unbelievable! You could have won the mega millions!") | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment