Skip to content
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

solved #14: return array of all prime number from 0->n #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions solutions/14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const isPrime = require('../solutions/8.js');
const primeArray = (n, array = []) => {
if (n > 1) {
if (isPrime(n) === true) array.unshift(n); //unshift = push, but reverse order
return primeArray(n-1, array);
}
return array;
};
module.exports = primeArray;
6 changes: 6 additions & 0 deletions solutions/21.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const findMax = (linkedlistNODE, head, max = linkedlistNODE.value) => {
if (!linkedlistNODE.next || linkedlistNODE.next === head) return max;
if (linkedlistNODE.value > max) max = linkedlistNODE.value;
return findMax(linkedlistNODE.next, head, max);
};
module.exports = findMax;
11 changes: 11 additions & 0 deletions test/14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const primeArray = require('../solutions/14.js');
const test = (testNumber, testArray = []) => {
if (JSON.stringify(primeArray(testNumber)) === JSON.stringify(testArray)) {
console.log('Yay, it works.');
} else {
console.log('Something is wrong...');
}
};
test(5, [2,3]); //wrong result
test(10, [2,3,5,7]); //correct result

42 changes: 42 additions & 0 deletions test/21.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//const findMax = require('../solutions/21.js');
const findMax = (linkedlistNODE, headNODE, max = linkedlistNODE.value) => {
if (!linkedlistNODE.next || linkedlistNODE.next === headNODE) return max;
if (linkedlistNODE.value > max) max = linkedlistNODE.value;
console.log(max);
return findMax(linkedlistNODE.next, headNODE,max);
};
//module.exports = findMax;

const test = (testLLNODE, result) => {
findMax(testLLNODE, testLLNODE) === result ? console.log('Correct') : console.log('Check again');
console.log(findMax(testLLNODE, testLLNODE));
};


//LinkedList case 1:
const d = {value: 9};
const c = {value: 4, next:d};
const b = {value: 2, next:c};
const a = {value:-1, next:b};
d.next = a;

test(a,9); //Correct

/*
//LinkedList case 2:
const theAwesomeNode = {
value:1, next:{
value:-2, next:{
value:7, next:{
value:4, next: null}}}};

test(theAwesomeNode, 7); //Correct

//LinkedList case 3:
const singularLL = {value:5, next:null};


test(singularLL, 5); //Correct
test(singularLL, 3); //Check again

*/