I have a function that returns a random number from 1-10. When I use it in my other function it works once, but then it starts having an error that it’s not a function.
This isn’t the exact code, just an example code that’s similar. It produces an error saying “TypeError: Property ‘ran’ of object [object DOMWindow] is not a function” Why is it doing this?
Thanks alot
var buffer = [];
function ran() {
return Math.round(Math.random()*10);
};
function test(){
var size = 6;
for (i=0; i<=size;i++) {
var num = ran();
if (num === 2 || num === 3){
buffer.push(num);
};
};
};
Your code is fine, but from the error it looks like you’re assigning some value to an implied global variable called
ransomewhere else in your code.If this is the case, make sure to limit the scope of your variables to the function in which they are declared by using the
varkeyword: