diff --git a/projects/m1/028-prime-factors/js/index.js b/projects/m1/028-prime-factors/js/index.js index e69de29bb..8fe0edcd7 100644 --- a/projects/m1/028-prime-factors/js/index.js +++ b/projects/m1/028-prime-factors/js/index.js @@ -0,0 +1,61 @@ +const readline = require('readline/promises'); +const { stdin: input, stdout: output } = require('process'); + + + +async function main() { + const rl = readline.createInterface({ input, output }); + try{ + + await getInput(rl) + } + catch(e){ + console.log(e) + } + finally{ + rl.close(); + } + + } + main(); + + + +function getBase2(inputNumber,result=""){ + if(inputNumber === 0){ + return result; + } + const rest = inputNumber % 2; + const newInputNumber = Math.floor(inputNumber / 2); + result =`${rest}${result}`; + + return getBase2(newInputNumber,result); +} + + + +async function getInput(rl){ + + + const input = await rl.question(`Please enter the number: `) + const inputNumber = Number.parseInt(input,10); + //const inputNumber = input *1; + if(input.trim().length === 0){ + return; + } + + if(Number.isInteger(inputNumber)){ + console.log(`conversione in base 2 vale ${getBase2(inputNumber)}`) + } + else{ + console.log('Please enter a valid number'); + } + + + + + return getInput(rl,type,month); + +} + + diff --git a/projects/m1/029-decimal-to-binary/js/index.js b/projects/m1/029-decimal-to-binary/js/index.js index e69de29bb..335260eba 100644 --- a/projects/m1/029-decimal-to-binary/js/index.js +++ b/projects/m1/029-decimal-to-binary/js/index.js @@ -0,0 +1,59 @@ +const readline = require('readline/promises'); +const { stdin: input, stdout: output } = require('process'); + + + +async function main() { + const rl = readline.createInterface({ input, output }); + try{ + + await getInput(rl) + } + catch(e){ + console.log(e) + } + finally{ + rl.close(); + } + + } + main(); + + + +function getBase2(inputNumber,result=""){ + if(inputNumber === 0){ + return result; + } + const rest = inputNumber % 2; + const newInputNumber = Math.floor(inputNumber / 2); + result =`${rest}${result}`; + + return getBase2(newInputNumber,result); +} + + + +async function getInput(rl){ + + + const input = await rl.question(`Please enter the number: `) + const inputNumber = Number.parseInt(input,10); + //const inputNumber = input *1; + if(input.trim().length === 0){ + return; + } + + if(Number.isInteger(inputNumber)){ + console.log(`conversione in base 2 vale ${getBase2(inputNumber)}`) + } + else{ + console.log('Please enter a valid number'); + } + + + + + return getInput(rl); + +} \ No newline at end of file