0

I went through different answers on forums but could not find a solution. So I have an array like this:

var valuesArray = ["negative", "underweight", "neutral", "overweight", "positive"];

I have two variables: var previousRating and var currentRating which dynamically get assigned with one of the values from valuesArray.

I am trying to find index of previousRating and currentRating in valuesArray like this:

var prevRatingCounter = valuesArray.indexOf(previousRating.toLowerCase());
var curRatingCouter = valuesArray.indexOf(currentRating.toLowerCase());

previousRating and curRating are string values but still I am getting error:

Object doesn't support this property or method

Can someone please help me figure this out. Thank you.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
LearningAsIGo
  • 1,240
  • 2
  • 11
  • 23
  • if previousRating/curRating are strings, you shouldn't face this issue.. Nevertheless, you have live code? Put your indexOf code under if(typeof previousRating === "string") – Sunil B N Jun 05 '15 at 17:38
  • They are: I did Response.Write and they are indeed strings. – LearningAsIGo Jun 05 '15 at 17:39
  • 2
    I'd suggest posting the block of code that you are having issues with instead of describing bits and pieces of it because from what I can see & what you have explained it should work. If you don't want to do that I'd suggest using `console.log()` and the `typeof` operator right before your these lines to make sure no coercion is happening prior. – galitskyd Jun 05 '15 at 17:39
  • I did type of and it is returning as a string. – LearningAsIGo Jun 05 '15 at 17:44
  • Does definition of valuesArray have any problem? – LearningAsIGo Jun 05 '15 at 17:47
  • 1
    Are you using IE to check ? [Why doesn't indexOf work on an array IE8?](http://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8) – Flakes Jun 06 '15 at 13:11
  • @SearchAndResQ not relevant, it's classic ASP which is running server side. – Shadow The GPT Wizard Jun 08 '15 at 19:57

2 Answers2

3

As explained in this answer, the version of ECMAScript used by server side JScript for writing classic ASP, is only 3.0 - the method indexOf() of arrays was introduced only in version 5.0 of ECMAScript as far as I can tell, hence it simply does not exist.

Writing such method isn't that hard, here is a working code sample:

<%
function IndexOf(arr, item) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == item) {
            return i;
        }
    }
    return -1;
}
%>

Then change the code to use it, e.g.

var prevRatingCounter = IndexOf(valuesArray, previousRating.toLowerCase());

If you need to use it in many different files, you can put the function in a file named e.g. "Common.asp" then include it wherever you need it.

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

I think that you have a problem with the inizialization.

This works fine:

     var valuesArray = ["negative", "underweight", "neutral", "overweight", "positive"];

     var previousRating = "underweight";
     var currentRating = "neutral";

     var valuesArray = ["negative", "underweight", "neutral", "overweight", "positive"];

     var prevRatingCounter = valuesArray.indexOf(previousRating.toLowerCase());

     var curRatingCouter = valuesArray.indexOf(currentRating.toLowerCase());

     document.write(prevRatingCounter+" "+curRatingCouter);
sochas
  • 2,271
  • 1
  • 11
  • 12