I want to write a ternary operator in js that do the following:
If index equals 1 or 3, set var x to 10, else set var x to 0.
How do i continue here?
Currently i have this:
let x = 0
const arr = ['string', 'string', 'string', 'string', 'string', 'string etc']
arr.forEach((el, index) => {
index ? == 1 || index ? == 3
})
2
Answers
You can set the value of x within the forEach loop based on the condition .I’ve added a code snippet.
The above answer works, If you want it to try some variations you can do this as well :