-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodifyArray.js
43 lines (36 loc) · 897 Bytes
/
modifyArray.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
/*
Problem Description
Create an arrow function named modify() which takes an integer array as input and modify the array as follows -
If an element of the array is prime, then add one to the element
If an element of the array is not prime, then multiply the element by 2.
Return the modified array.
*/
let modify = (array) => {
for (let i =0 ;i < array.length ; i++)
{
let isPrimeNo = isPrime(array[i])
if (isPrimeNo === true)
{
array[i] = array[i] + 1 ;
}
else
{
array[i] = array[i] * 2 ;
}
}
return array;
}
function isPrime(n) {
if (n == 0 || n == 1) {
return false;
}
for (let i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
let array = [1, 2, 3, 4, 5]
console.log(modify(array))
module.exports = modify;