1

I have an array that I created using Array(...) and Array.prototype.map, like this:

var array_name = Array(255).map(function(undef, i) {
  return i + 1;
});

The values of this array are:

[1, 2, 3, ..., 253, 254, 255]

This is an array that won't get modified, so the first value of this array will always be 1 and the last value of this array will always be 255.

I already know the index of each value is {value} - 1, so 200 would be 199, 199 would be 198, so on and so forth.

Let's say I want 255's opposite value, which would be 0, I could get that using array_name[0], but what if I wanted 200's opposite value, how would I know what the opposite index of 199 is so I could get it's value?

Malekai
  • 4,765
  • 5
  • 25
  • 60

6 Answers6

4

Do:

opposite_index = arr.length - index - 1

For example:

a = [1,2,3,4,5,6,7,8,9,10]

index = 3
a[index]
4

It's opposite is 7 so:

opposite_index = a.length - index - 1
a[opposite_index]
7

With reverse as per @Maheer Ali suggestion:

a.reverse()[index]
7
R. Schifini
  • 9,085
  • 2
  • 26
  • 32
1

First, you gotta understand that there is weird behavior concerning Array(n).map(f) (it won't create the array you're expecting), see this answer for explanation, second, do this to get the opposite values:

/* fill it first with .fill(), see the question I linked for more explanation */
var array = Array(255).fill(undefined).map(function(undef, i) {
  return i + 1;
});

function opposite(array, n) {
  return array[array.length - n];
}

console.log(opposite(array, 255));
console.log(opposite(array, 200));
console.log(opposite(array, 199));
console.log(opposite(array, 1));

Notice that length - n is used instead of length - n - 1, because we're dealing with values from 1 to n, not from 0 to n - 1.

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1

Your Array(255).map() create undefined array value.So do with Array#from length object.And pass your value.get index of the value and match with reverse array you get opposite value

let check = (val) => {
var array_name = Array.from({length:255},(a,b)=>b+1);
var nor_ind = array_name.indexOf(val);
var re_in = array_name.reverse(array_name).indexOf(val)
return ({nor_val:val,nor_ind:nor_ind,opp_val:re_in})
}

console.log(check(254))
prasanth
  • 22,145
  • 4
  • 29
  • 53
1

First of all the code you provided doesn't create array [1,2,3...255]. It will create it will 255 empty items first you need to fill().

var arr = Array(255).fill().map((a,i) => i+1);


//Create an array which will have revese items.
let revarr= arr.reverse()
console.log(revarr[0]) //255
console.log(revarr[254]) // 1

If you don't want to create a reverse arr. You can create a function

var arr = Array(255).fill().map((a,i) => i+1);
const opp = (arr,num) => arr[arr.length - num - 1];
console.log(opp(arr,0));
console.log(opp(arr,254));
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • Thank you for taking the time and effort to answer this question and thank you for the suggestion, +1. – Malekai Mar 16 '19 at 15:13
1

Subtract the index from (length-1) -> max index of the array

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function findOpp(index, length) {
  maxIndex = length - 1;
  if (index <= maxIndex && index >= 0) {
    return maxIndex - index;
  } else {
    
    return 'You have enter a wrong index';
  }
}

console.log(findOpp(-1, 10));
console.log(findOpp(0, 10));
console.log(findOpp(1, 10));
console.log(findOpp(2, 10));
console.log(findOpp(4, 10));
console.log(findOpp(5, 10));
console.log(findOpp(6, 10));
console.log(findOpp(7, 10));
console.log(findOpp(8, 10));
console.log(findOpp(9, 10));
console.log(findOpp(10, 10));
Syed Mehtab Hassan
  • 1,297
  • 1
  • 9
  • 23
0

Using Maheer Ali's suggestion I managed to get the desired result by reversing the array and using indexOf to get the index of that number:

var numbers = Array(255).map(function(v, i) {
  return i + 1;
});

var opposite_brightness = numbers.reverse()[numbers.indexOf(brightness)];
Malekai
  • 4,765
  • 5
  • 25
  • 60