30

I am new to JavaScript and I'm getting an error as below.

Uncaught TypeError: time.indexOf is not a function

Gee, I really thought indexOf() really was a function. Here is a snippet of my code:

    var timeofday = new Date().getHours() + (new Date().getMinutes()) / 60;
    document.getElementById("oset").innerHTML = timeD2C(timeofday);
</script>


<script>
 function timeD2C(time) { // Converts 11.5 (decimal) to 11:30 (colon)
    var pos = time.indexOf('.');
    var hrs = time.substr(1, pos - 1);
    var min = (time.substr(pos, 2)) * 60;

    if (hrs > 11) {
        hrs = (hrs - 12) + ":" + min + " PM";
    } else {
        hrs += ":" + min + " AM";
    }
    return hrs;
}
</script>
Jino Shaji
  • 1,097
  • 14
  • 27
TerryO
  • 301
  • 1
  • 3
  • 3
  • 2
    It is indeed a function - of a valid object - meaning `time` isn't a valid object. Make sure time exists, log it inside the `timeD2C` function call. – Stuart Apr 07 '16 at 17:23
  • 1
    Why do you think a number has an `indexOf` function? – SLaks Apr 07 '16 at 17:25
  • 1
    Your code is mixing strings and numbers. You need to be careful that you are using the correct type when performing each operation. – Hoyen Apr 07 '16 at 17:39

4 Answers4

33

Basically indexOf() is a method belongs to string(array object also), But while calling the function you are passing a number, try to cast it to a string and pass it.

document.getElementById("oset").innerHTML = timeD2C(timeofday + "");

 var timeofday = new Date().getHours() + (new Date().getMinutes()) / 60;




 function timeD2C(time) { // Converts 11.5 (decimal) to 11:30 (colon)
    var pos = time.indexOf('.');
    var hrs = time.substr(1, pos - 1);
    var min = (time.substr(pos, 2)) * 60;

    if (hrs > 11) {
        hrs = (hrs - 12) + ":" + min + " PM";
    } else {
        hrs += ":" + min + " AM";
    }
    return hrs;
}
alert(timeD2C(timeofday+""));

And it is good to do the string conversion inside your function definition,

function timeD2C(time) { 
  time = time + "";
  var pos = time.indexOf('.');

So that the code flow won't break at times when devs forget to pass a string into this function.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • 1
    Casting to string would better be placed _inside_ the function – that way, it won’t throw errors should it be called with a number from somewhere else for whatever reason. – CBroe Apr 07 '16 at 17:30
  • 1
    Thank you all for your suggestions. I added the time=time+"" and the problem went away. JavaScript is so liberal when dealing with stings and numbers that it's easy to forget that a number can't always be treated as a string. – TerryO Apr 07 '16 at 17:49
  • 1
    I think a clean approach will be converting to String like `time = String(time)` – Umair Malhi Aug 24 '17 at 10:39
  • @TerryO, please accept this answer if it worked. This question is marked without an accepted answer until then. – ReinstateMonica3167040 Dec 07 '19 at 15:18
8

Convert timeofday to string to use indexOf

var timeofday = new Date().getHours() + (new Date().getMinutes()) / 60;
console.log(typeof(timeofday)) // for testing will log number
function timeD2C(time) { // Converts 11.5 (decimal) to 11:30 (colon)
    var pos = time.indexOf('.');
    var hrs = time.substr(1, pos - 1);
    var min = (time.substr(pos, 2)) * 60;

    if (hrs > 11) {
        hrs = (hrs - 12) + ":" + min + " PM";
    } else {
        hrs += ":" + min + " AM";
    }
    return hrs;
}
 // "" for typecasting to string
 document.getElementById("oset").innerHTML = timeD2C(""+timeofday);

Test Here

Solution 2

use toString() to convert to string

document.getElementById("oset").innerHTML = timeD2C(timeofday.toString());

jsfiddle with toString()

brk
  • 48,835
  • 10
  • 56
  • 78
6

I was getting e.data.indexOf is not a function error, after debugging it, I found that it was actually a TypeError, which meant, indexOf() being a function is applicable to strings, so I typecasted the data like the following and then used the indexOf() method to make it work

e.data.toString().indexOf('<stringToBeMatchedToPosition>')

Not sure if my answer was accurate to the question, but yes shared my opinion as i faced a similar kind of situation.

Ali
  • 2,702
  • 3
  • 32
  • 54
Rohit
  • 61
  • 1
  • 1
0

I ran across this error recently using a javascript library which changes the parameters of a function based on conditions.

You can test an object to see if it has the function. I would only do this in scenarios where you don't control what is getting passed to you.

if( param.indexOf != undefined ) {
   // we have a string or other object that 
   // happens to have a function named indexOf
}

You can test this in your browser console:

> (3).indexOf == undefined;
true

> "".indexOf == undefined;
false
Andrew Grothe
  • 2,562
  • 1
  • 32
  • 48