caramba
caramba

Reputation: 22480

Determine array by value and order

Given an array which will always contain two or three values. I need to determine the index of the biggest value and add attribute to it lets say main. Then depending on the order set left to the first small value and right to the second small value. (Careful for array6)

There must always be one main.

It's easy to determine the biggest value like:

var array1   = [0.25, 0.75];        // desired result: ['left', 'main']
var array2   = [0.75, 0.25];        // desired result: ['main', 'right']
var array3   = [0.25, 0.50, 0.25];  // desired result: ['left', 'main', 'right']
var array4   = [0.50, 0.25, 0.25];  // desired result: ['main', 'left', 'right']
var array5   = [0.25, 0.25, 0.50];  // desired result: ['left', 'right', 'main']
var array6   = [0.40, 0.10, 0.50];  // desired result: ['left', 'right', 'main']
var array7   = [0.50, 0.50];        // desired result: ['left', 'main']


index1 = array1.indexOf(Math.max.apply( Math, array1 ));
index2 = array2.indexOf(Math.max.apply( Math, array2 ));
index3 = array3.indexOf(Math.max.apply( Math, array3 ));
index4 = array4.indexOf(Math.max.apply( Math, array4 ));
index5 = array5.indexOf(Math.max.apply( Math, array5 ));
index6 = array6.indexOf(Math.max.apply( Math, array6 ));

console.log(
    'index1: ', index1,
    'index2: ', index2,
    'index3: ', index3,
    'index4: ', index4,
    'index5: ', index5,
    'index6: ', index6
);

But how can I achieve the rest of desired result without using now 100 if else to determine?

Update The total sum of the array values will always be 1.

Upvotes: 3

Views: 101

Answers (3)

Koushik Chatterjee
Koushik Chatterjee

Reputation: 4175

Here I wrote a function that will solve your purpose with minimal if...else

var array1   = [0.25, 0.75];        // desired result: ['left', 'main']
var array2   = [0.75, 0.25];        // desired result: ['main', 'right']
var array3   = [0.25, 0.50, 0.25];  // desired result: ['left', 'main', 'right']
var array4   = [0.50, 0.25, 0.25];  // desired result: ['main', 'left', 'right']
var array5   = [0.25, 0.25, 0.50];  // desired result: ['left', 'right', 'main']
var array6   = [0.40, 0.10, 0.50];  // desired result: ['left', 'right', 'main']
var array7   = [0.50, 0.50];        // desired result: ['left', 'main']

function transformArr(numArr) {
    var res = ['left', 'right'];
    maxIdx = numArr.lastIndexOf(Math.max.apply(Math, numArr));
    res.splice(maxIdx, numArr.length === 2, 'main')
    return res;
}

console.log('Result 1', transformArr(array1));
console.log('Result 2', transformArr(array2));
console.log('Result 3', transformArr(array3));
console.log('Result 4', transformArr(array4));
console.log('Result 5', transformArr(array5));
console.log('Result 6', transformArr(array6));
console.log('Result 7', transformArr(array7));

This code is written on the considering that arrays will contain either 2 or 3 numbers.

Comment if anything is not clear.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386620

You could splice at the index of the first max value from the right, the value main and delete a value, if the length of the array is two.

function x(array) {

    function getMax(r, a, i, aa) {
        return a > aa[r] ? i : r;
    }

    var r = ['left', 'right'];
    r.splice(array.reduceRight(getMax, array.length - 1), array.length === 2, 'main');
    return r;
}

console.log(x([0.25, 0.75]));        // ['left', 'main']
console.log(x([0.75, 0.25]));        // ['main', 'right']
console.log(x([0.25, 0.50, 0.25]));  // ['left', 'main', 'right']
console.log(x([0.50, 0.25, 0.25]));  // ['main', 'left', 'right']
console.log(x([0.25, 0.25, 0.50]));  // ['left', 'right', 'main']
console.log(x([0.40, 0.10, 0.50]));  // ['left', 'right', 'main']
console.log(x([0.50, 0.50]));        // ['left', 'main']
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

user3797387
user3797387

Reputation:

My 5 cents:

function transformArr(arr){
  let indexes = arr.map((_, i) => i);
  indexes.sort((i1, i2) => arr[i2] - arr[i1]);

  let mainI = indexes[0];
  indexes = indexes.map(x => {
    return {index: x, atLeft: x < mainI, value: arr[x]};
  });

  let res = {"main": indexes[0].value};
  // let names = ['left', 'inner left', 'inner right', 'right'];
  let names = ['left', 'right'];
  for(let i = 1; i < indexes.length; i++){
    let val = indexes[i];
    let name = val.atLeft ? names.shift() : names.pop();
    res[name] = val.value;
  }
  return res;
}

Upvotes: 0

Related Questions