-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
77 lines (66 loc) · 3.41 KB
/
script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Requesting the prompt syncronization with terminal function from 'prompt-sync' library
const prompt = require('prompt-sync')();
// Setting the code
// Introduction message
console.log(`======================= \nPRIME NUMBERS ALGORITHM \n=======================`);
// The isPrime function verifies prime numbers
const isPrime = (number : number) => {
if (number < 2) return false; // If number is less than 2, it is not prime
for (let looper: number = 2; looper <= Math.sqrt(number); looper++) { // Verify numbers until square root of number
if (number % looper === 0) return false; // If number is divided by loop number, it is not prime
}
return true; // If there is not divided, it is prime
};
// The mainFunction to run the code
function mainFunction() {
// Defining the variable
const limitNumber = Number(prompt("Enter the limit number: "));
const primeArray = [];
// Verifying for any errors as
if (limitNumber < 2) {
// in limit number being less than 2
console.log(`====================================================== \nREADING ERROR: minimum value is 2! \n======================================================`);
mainFunction();
return;
}
if (Number.isInteger(limitNumber) === false) {
// the limit value not being an integer
console.log(`====================================== \nREADING ERROR: value must be intenger! \n======================================`);
mainFunction();
return;
};
// Creating a loop to find each prime number until reaching the limit number
// It starts in 2 because 1 is not a prime number
for (let looper: number = 2; looper <= limitNumber; looper++) { // It will verify until the array is full
if (isPrime(looper)) { // If the function isPrime return true
primeArray.push(looper); // the number enters in array
};
};
// Showing the table with the prime numbers
console.log(`====================================== \nArray with the prime numbers up to ${limitNumber}: \n[${primeArray.join(', ')}] \n======================================`);
// Asking the user if he whants the operation to be repeated
repeatingMain();
};
// Function that asks the user if he whants the operation to be repeated using the prompt syncronization with terminal
function repeatingMain() {
const asking: string = String(prompt(`Would you like the operation to be repeated? Enter 'y' for Yes and 'n' for No: `)).toLowerCase();
// Assigning a character to each of the following options
switch (asking) {
// 'y' if you want the operation to repeat
case "y":
console.log(`====================== \nRepeating operation... \n======================`);
mainFunction();
break;
// 'n' if you want the operation to finish
case "n":
console.log(`=========================== \n Thank you for using \nthe prime number algorithm! \n \n By JonaxPlanta :] \n===========================`);
break;
// In this case, the function repeats if the user entered any other character instead of 'y' or 'n'
default:
console.log(`========================================= \nREADING ERROR: enter a correct character! \n=========================================`);
repeatingMain();
break;
}
};
// Starting code
mainFunction();