0

I have an array A = [1,3] - which stores positions of correct answers from another array B = ["answer1","answer2","answer3","answer4"]. I need to get an array C which will be consist from the correct answers, something like this ["answer1","answer3"].

Example: If array A consist from 1 element (for example A = [2]), then array C should look like C = ["answer2"].

I can solve this problem with indexOf(), when I have an array A consisting from 1 element. But when there are 2 or more elements I'm getting stuck

1
  • An alternative solution to @Ilia Komarov's perfect one can be: for (var i in A) C += B[A[i]-1] Just to know. Commented Mar 20, 2021 at 9:45

1 Answer 1

5

You should do something like:

let A = [1,3];
let B = ["answer1","answer2","answer3","answer4"];
let C = A.map(i => B[i-1]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.