Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5211598

Browse files
Create modifyArray.js
1 parent c1117c1 commit 5211598

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎Javascript/modifyArray.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Problem Description
3+
Create an arrow function named modify() which takes an integer array as input and modify the array as follows -
4+
5+
If an element of the array is prime, then add one to the element
6+
7+
If an element of the array is not prime, then multiply the element by 2.
8+
9+
10+
Return the modified array.
11+
12+
13+
*/
14+
let modify = (array) => {
15+
for (let i =0 ;i < array.length ; i++)
16+
{
17+
let isPrimeNo = isPrime(array[i])
18+
if (isPrimeNo === true)
19+
{
20+
array[i] = array[i] + 1 ;
21+
}
22+
else
23+
{
24+
array[i] = array[i] * 2 ;
25+
}
26+
}
27+
return array;
28+
}
29+
30+
function isPrime(n) {
31+
if (n == 0 || n == 1) {
32+
return false;
33+
}
34+
for (let i = 2; i * i <= n; i++) {
35+
if (n % i == 0) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
let array = [1, 2, 3, 4, 5]
42+
console.log(modify(array))
43+
module.exports = modify;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /