Skip to content

Commit

Permalink
Merge pull request #5840 from npminit-dev/main
Browse files Browse the repository at this point in the history
Reto #44 - javascript
  • Loading branch information
Roswell468 authored Nov 24, 2023
2 parents 5d6f955 + 182dd31 commit 2f67a4f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
11 changes: 5 additions & 6 deletions Retos/Reto #38 - LAS SUMAS [Media]/javascript/npminit-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ function getSumsLists(list, target) {
let sList = list.filter(num => num <= target).sort()
let result = []

function backTracking(sub, start) {
let sum = sub?.reduce((acc, curr) => acc += curr, 0) || 0
function backTracking(sub, start, sum) {
if(sum === target) result.push(sub)
for(let i = start; i < sList.length; i++)
if(sum + list[i] <= target) backTracking([...sub, list[i]], i + 1)
if(sum + list[i] <= target) backTracking([...sub, list[i]], i + 1, sum + list[i])
}

for(let i = 0; i < sList.length - 1; i++)
backTracking([list[i]], i + 1)
backTracking([list[i]], i + 1, list[i])
return result
}

Expand All @@ -35,6 +34,6 @@ console.log(getSumsLists([3, 2, 1, 6, 8, 4, 3, 10], 12))
console.log(getSumsLists([3, 6, 9, 12, 15], 30))
/* result = [[ 3, 6, 9, 12 ], [ 3, 12, 15 ], [ 6, 9, 15 ]] */
console.log(getSumsLists([15, 10, 5, 2, 8, 10], 50))
/* result = [[ 3, 6, 9, 12 ], [ 3, 12, 15 ], [ 6, 9, 15 ]] */
/* result = [[15, 10, 5, 2, 8, 10]] */
console.log(getSumsLists([1, 2, 3, 2, 1], 3))
/* result = [[ 3, 6, 9, 12 ], [ 3, 12, 15 ], [ 6, 9, 15 ]] */
/* result = [[ 1, 2 ], [ 1, 2 ], [ 2, 1 ], [ 3 ], [ 2, 1 ]] */
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const readline = require('readline')

let questCount = 1
let correct = 0
const ops = ['+', '-', '*', '/']

const createIF = (readline) => readline.createInterface({
input: process.stdin,
output: process.stdout
})

const getOperation = (ops, quesCount) => {
const op = ops[Math.round(Math.random() * (ops.length - 1))]
let left
let right
if(quesCount <= 5) {
left = Math.round(Math.random() * 9)
right = Math.round(Math.random() * 9)
} else if(quesCount > 5 && quesCount <= 10) {
left = Math.round(Math.random() * 90)
right = Math.round(Math.random() * 9)
} else if(quesCount > 10 && quesCount <= 15) {
left = Math.round(Math.random() * 99)
right = Math.round(Math.random() * 99)
} else {
left = Math.round(Math.random() * 999)
right = Math.round(Math.random() * 99)
}
return [
`${left}${op}${right}`,
eval(`Math.trunc(${left}${op}${right})`)
]
}

console.clear()
console.log(
` <--- MATH OPS GAME --->
All results must be rounded to the nearest integer
3 seconds contdown per question...`)

const question =() => {
return new Promise((res, rej) => {
let rl = createIF(readline)
let [opstring, expect] = getOperation(ops, questCount)
rl.question(
`- Question ${questCount}:
Which is the result of ${opstring}?\n`, (result) => {
if(parseInt(result) === expect) correct++
rl.close()
console.clear()
res('')
})
setTimeout(() => {
rl.close()
rej('')
}, 3000)
})
}

setTimeout(async () => {
console.clear()
while(questCount <= 20) {
try { await question() }
catch(_) { break }
questCount ++
}
console.clear()
console.log(`
GAME END!!!
Correct answers: ${correct}/20
${correct <= 10 ?
'Meh... better luck next time' :
correct > 10 && correct <= 15 ?
'Not bad, but you can improve' :
correct > 15 && correct <= 19 ?
'Great work!' : 'Perfect!!!'
}`)
process.exit(0)
}, 4000)

0 comments on commit 2f67a4f

Please sign in to comment.