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 692ad89

Browse files
Create convertArray.js
1 parent 7e6790f commit 692ad89

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎Javascript/convertArray.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Write a method to replace all elements of an array by (i + 1) * arr[i], where i is the position of an element in the array.
3+
4+
Note: We are following a 0 based indexing.
5+
6+
References
7+
JavaScript: forEach method
8+
9+
Input -
10+
You are given an array arr.
11+
Output -
12+
Return the array after converting all the elements in the array
13+
14+
Sample input 1 -
15+
[1, 2, 2, 3, 1, 2]
16+
17+
Sample output 1 -
18+
[1, 4, 6, 12, 5, 12]
19+
*/
20+
function convertArray(arr) {
21+
22+
let result =[]
23+
arr.forEach((data, index) =>{
24+
result.push((index+1)* data);
25+
26+
})
27+
return result;
28+
}
29+
30+
let arr= [1,2,3,4,5];
31+
console.log(convertArray(arr))
32+
module.exports = convertArray;
33+

0 commit comments

Comments
(0)

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