-
Notifications
You must be signed in to change notification settings - Fork 2
* I'am finished the homework #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop-vachagan
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,36 @@ | ||
| var num = parseInt(prompt('Enter a natural number or "exit"')); | ||
| var num = prompt('Enter a natural number or "exit"'); | ||
|
|
||
| var c = 1; | ||
| var primeArr = [2]; | ||
| var i = 3; | ||
| while (num !== 'exit') { | ||
|
|
||
| function isPrime(k) { | ||
| for (var j = 2; j < k; j++) { | ||
| if (k % j === 0) { | ||
| return false; | ||
| var count = 1; | ||
|
||
| var primeArr = [2]; | ||
| var startPrimeNum = 3; | ||
|
|
||
| while (count < parseInt(num)) { | ||
| var isPrime = true; | ||
| for (j = 2; j < startPrimeNum; j++) { | ||
| if (startPrimeNum % j === 0) { | ||
| isPrime = false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop will do 998 iterations, if |
||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| while (c < num) { | ||
| if (isPrime(i)) { | ||
| primeArr[c] = i; | ||
| c++; | ||
| if (isPrime) { | ||
| primeArr[count] = startPrimeNum; | ||
| count++; | ||
| } | ||
| startPrimeNum++; | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
|
|
||
| console.log(primeArr); | ||
|
|
||
| for (let i = 0; i < primeArr.length - 1; i++) { | ||
| for (var j = 0; j < primeArr.length - 1; j++) { | ||
| if ((primeArr[j] % 10) > (primeArr[j + 1] % 10)) { | ||
| let c = primeArr[j]; | ||
| primeArr[j] = primeArr[j + 1]; | ||
| primeArr[j + 1] = c; | ||
| for (var i = 0; i < primeArr.length - 1; i++) { | ||
| for (var j = 0; j < primeArr.length - 1; j++) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see bubble sort implementation here, but both loops run from 0 to |
||
| if ((primeArr[j] % 10) > (primeArr[j + 1] % 10)) { | ||
| var sort = primeArr[j]; | ||
| primeArr[j] = primeArr[j + 1]; | ||
| primeArr[j + 1] = sort; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(primeArr); | ||
| num = prompt('Enter a natural number or "exit"'); | ||
| } | ||
|
|
||
| console.log(primeArr); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first glance it's not clear why this line is outside the loop. It's a bit confusing. If this line goes into top of
whileloop, the code will be clearer.