1

Can someone more intelligent than me please solve this simple math conversion that I need to accomplish within javascript.

If I have an ordered array of numbers say...

var values = [99, 101, 102, 103, 104];
var low = 99;
var high = 104;

...and I need the highest value in the array to be pushed to a new array as the value 1, and the second highest value to be pushed to the new array as 2, etc., so the new array is...

var newArray = [1, 2, 3, 4, 6];

I can make a hack version that works but I need a solution that will work on arrays with diverse number values. Appreciate the help!

1
  • 5
    what is the logic behind pushing 1 instead of 104. If there is no reason then simply Array.fill will work Commented Jun 5, 2018 at 17:13

5 Answers 5

2

You could map the delta of high value and value plus one and reverse the array.

var values = [99, 101, 102, 103, 104],
    low = 99,
    high = 104,
    result = values
        .map(v => high + 1 - v)
        .reverse();
 
console.log(result);

Sign up to request clarification or add additional context in comments.

5 Comments

Small question, with a callback function one can also get the index and the array, is that possible with the arrow function?
the callback has the same signature for arrow functions as for standard functions, like (item, index, array) => ....
Thank you very much for that ... I didn't understand to enclose the parameters with parenthesis.
maybe you have a look here, too: arrow functions
Thanks again ... actually been at that page before but obviously missed (read lazy :) the line // Parentheses are optional when there's only one parameter name. Never had the time to dig deeper into all these new stuff. They weren't available when I started coding 25 years ago :)
2

May be something like this

var values = [99, 101, 102, 103, 104];
var low = 99;
var high = 104;

var newOutputArr = [];
newOutputArr.push(1);


for (var j=values.length - 2; j>=0; j--) {
  var cal = Math.abs(values[j] - high) + 1;
  newOutputArr.push(cal);
}

console.log(newOutputArr);

Hope this helps!

Comments

2

With map and reverse, you could subtract each value from the first and then add 1

Stack snippiet

var values = [99, 101, 102, 103, 104];

var new_values = values.reverse().map(function (val, idx, arr) {
    return (arr[0] - val + 1);
})

console.log(new_values);


Or with an arrow function (this won't work with IE though)

var values = [99, 101, 102, 103, 104];

var new_values = values.reverse().map((val,idx,arr) => arr[0] - val + 1);

console.log(new_values);

1 Comment

Thanks so much guys! Really appreciate it!
0

I would use Array.prototype.reduce. And forgo the use of the low and high variables. Then you can use any sorted array. Something like this:

const values = [99, 101, 102, 103, 104, 108, 199, 200, 234, 235];

const newArray = values.reduce( (i, v, a, b) => {
  
  if(a === 0) i.push(1)
  else i.push(i[a - 1] + v - b[a-1])
  
  return i
    
}, []);

console.log(newArray)

Comments

0

Just a single pass of .reduceRight() is sufficient.

var values = [99, 101, 102, 103, 104],
    low    = 99,
    high   = 104,
    result = values.reduceRight((r,v) => r.concat(high-v+1), []);
console.log(result);

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.