1

I want to get the number of the reverse order of a given series in javascript. If I have the following series:

[1,2,2,2,5,5,7,8,8,10]

then if the input is number 8 the output should be 2, since:

 1  = 10
 2  = 7
 2  = 7
 2  = 7
 5  = 5
 5  = 5
 7  = 4
[8  = 2]
[8  = 2]
 10 = 1
//--> [1 , 2,3,4, 5,6, 7, 8,9, 10]
  --> [1 , 2,2,2, 5,5, 7, 8,8, 10]
      [10, 7,7,7, 5,5, 4, 2,2, 1 ] <-- // ==> [1,2,2,4,5,5,7,7,7,10]

Here is what I have done so far:

function getReverseNumber(arr, num)
{
    var newArr = new Array(arr.length);
    var temp;
    var counter = 1;
    for(var i = arr.length; i > 0; i--)
    {
        if(temp === arr[i])
        {
            newArr[arr.length - i] = counter;
            continue;
        }
        newArr[arr.length - i] = counter;
        temp = arr[i];
        counter++;
    }
    return newArr[num - 1];
}

but it doesn't work as expected:

getReverseNumber(new Array(1,2,2,2,5,5,7,8,8,10), 8) // returns 5 not 2

what is wrong in my function?

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • What is the expected output if the input is 2, which matches both 7 and 8? – JYelton Aug 06 '11 at 00:18
  • if the input is `2` then the output is `7` – Eng.Fouad Aug 06 '11 at 00:21
  • 2
    your explanation confuses me, why if the number input is 2 is the answer always 7? If you are getting the reverse order the 2's would correspond to [8,8,7] respectively. Is what you have a typo? – Hunter McMillen Aug 06 '11 at 00:23
  • Hunter makes a great point. biased code is so hard to debug – Maverick Aug 06 '11 at 00:31
  • the reversed series would change the counter only if the number changed in the orginial series. i.e. if I have `1,2,3,3,5` then the reversed series would be `1,2,2,4,5` – Eng.Fouad Aug 06 '11 at 00:35

4 Answers4

1

I think that you are overcomplicating it. You are only increasing counter by one when you increase it, and you place the numbers in revese order, so newArr end up as [1,2,2,3,4,4,5,5,5,6] instead of [10,7,7,7,5,5,4,2,2,1].

There is no need to calculate all those numbers and keep in an array. Just loop from 1 and up, and calculate which position that is in the array. Return the index when you find the value:

function getReverseNumber(arr, num) {
  for (var i = 1; i <= arr.length; i++) {
    if (arr[arr.length - i] == num) return i;
  }
  return -1; // not found
}

Demo: http://jsfiddle.net/Xedz6/

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • why does the function return `not found`? – Eng.Fouad Aug 06 '11 at 00:41
  • @End.Fouad: It only returns that if the number is not found in the array, for example if you would call `getReverseNumber([1,2,3], 42)`. – Guffa Aug 06 '11 at 00:50
  • @stereofrog: I don't see how that page is relevant at all, or why it would explain anything at all about this code. – Guffa Aug 06 '11 at 10:22
  • @stereofrog: The implementation that you linked to is not a "standard function" as it's clearly not well tested, and changes daily... – Guffa Aug 07 '11 at 13:03
1

fiddle tested in IE 8 and 7 enjoy

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
    "use strict";
    if (this === void 0 || this === null) {
        throw new TypeError();
    }
    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0) {
        return -1;
    }
    var n = 0;
    if (arguments.length > 0) {
        n = Number(arguments[1]);
        if (n !== n) { // shortcut for verifying if it's NaN
            n = 0;
        } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
            n = (n > 0 || -1) * Math.floor(Math.abs(n));
        }
    }
    if (n >= len) {
        return -1;
    }
    var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
    for (; k < len; k++) {
        if (k in t && t[k] === searchElement) {
            return k;
       }

    }

    return -1;

  }

}


...

arr[ Math.Abs(arr.indexOf(num)-arr.length-1)]

sorry about the formatting. I'm on my phone.

Joe
  • 80,724
  • 18
  • 127
  • 145
  • @Guffa, ouch, it works in IE8 and earlier with the prototype in the following article quite easily: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf – Joe Aug 06 '11 at 00:32
  • `getReverseNumber([1,2,2,2,5,5,7,8,8,10], 2)` returns 10 not 7 – Eng.Fouad Aug 06 '11 at 00:55
  • @Joey: That prototype code looks buggy... For example, why check if the first parameter exist and then read the second: `if (arguments.length > 0) { n = Number(arguments[1]);`? – Guffa Aug 06 '11 at 01:05
1

Works in IE6 even ;)

function getReverseNumber(arr,num){
    alert(arr[ arr.length + arr.indexOf(num) * -1  ]);
}

getReverseNumber(new Array(1,2,2,2,5,5,7,8,8,10), 8); // alerts 2

Working demo: http://jsfiddle.net/AlienWebguy/7qvzE/

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • I don't know why `arr.indexOf(num)` gives me same error `Object doesn't support this property or method` – Eng.Fouad Aug 06 '11 at 00:39
  • Hmm works fine for me in IETester for ie6,7, and 8. I know old IE's don't like JSFiddle. Try a local example. – AlienWebguy Aug 06 '11 at 00:53
  • According to this, `indexOf` doesn't exist in IE 6: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf – Guffa Aug 06 '11 at 00:55
0

check out lastIndexOf

a = [1,2,2,2,5,5,7,8,8,10]
n = a.lastIndexOf(8)
alert(a.length - n)

Regarding these "IE8" remarks - even if dealing with obsolete browsers, this is not an excuse for reinventing the wheel. Use standard documented javascript library functions and include compatibility/degradation layers when appropriate.

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
user187291
  • 53,363
  • 19
  • 95
  • 127
  • It gives me an error: `Object doesn't support this property or method` – Eng.Fouad Aug 06 '11 at 00:24
  • -1 for offering a solution without a propery degradation solution. The code in the page that you linked to is clearly not properly tested. Also, for trying to impose this bad practice as "good practice". – Guffa Aug 07 '11 at 12:49