-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (72 loc) · 2.41 KB
/
index.js
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
78
79
80
81
82
83
84
85
86
87
"use strict";
const INVALID_INT = 2;
const INVALID_FUNC = 3;
const INVALID_ARR_OF_INTS = 4;
// set up listener for "exit" events to provide helpful error messages
process.on( "exit", code => {
switch ( code ) {
case INVALID_INT:
console.log( "Please provide valid positive integer greater than 1." );
break;
case INVALID_FUNC:
console.log( "Please provide valid function." );
break;
case INVALID_ARR_OF_INTS:
console.log( "Please provide valid array of integers" );
break;
}
});
// assign third cli argument to scoped variable and convert to number
const numInput = +process.argv[2];
// validate that numInput is a positive integer
if ( !Number.isInteger( numInput ) || numInput <= 1 ) process.exit( INVALID_INT );
/*
* DEFINE FUNCTIONS
*/
function findPrimeInts ( input ) {
// use the sieve of Eratosthenes to find all prime numbers equal to or less than input.
let potentialPrimeInts = [];
for ( let i = 2; i <= input; i++ ) {
potentialPrimeInts.push( { value: i, isPrime: true } );
}
let primeInts = [];
for ( let i = 0; i < potentialPrimeInts.length; i++ ) {
if ( potentialPrimeInts[i].isPrime ) {
let primeValue = potentialPrimeInts[i].value;
primeInts.push( primeValue );
for ( let j = ( primeValue + i ); j < potentialPrimeInts.length; j += primeValue ) {
potentialPrimeInts[j].isPrime = false;
}
}
}
return primeInts;
}
function isFuncAdditive ( func, ints ) {
// basic (even simplistic - would ideally use lodash or underscore methods) validation for function and integers
if ( typeof func !== "function" ) process.exit( INVALID_FUNC );
if ( typeof ints[0] !== "number" ) process.exit( INVALID_ARR_OF_INTS );
// nested loop to test secret function against every combination of two items in `ints`
for ( let i = 0; i < ints.length-1; i++ ) {
for ( let j = i+1; j < ints.length; j++ ) {
let result1 = func( ints[i] );
let result2 = func( ints[j] );
let result3 = func( ints[i] + ints[j] );
if ( ( result1 + result2 ) !== result3 ) {
return false;
}
}
}
return true;
}
/*
* Secret Function - Replace with any function named "secret" which takes a number as the input and returns a number.
*/
function secret ( x ) {
return x * 2;
}
/*
* RUN FUNCTIONS
*/
let primeIntegers = findPrimeInts( numInput );
let funcIsAdditive = isFuncAdditive( secret, primeIntegers );
console.log( `The secret function is ${funcIsAdditive ? "indeed" : " not "} additive.` );