Tristan
Tristan

Reputation: 2088

string typeError: variable is undefined

I am writing a little program with javascript and html canvas. It is my first javascript program. It will make the dragonCurve thing. But I am getting this error:TypeError: old is undefined. I am not the first one with this question but after reading the other questions I still couldn't find a solution.

The following code is part of the program where it raises an error. So why does it give an error?

<script>
var r = 'r';
var l = 'l';

var old = r;
var newer = old;

var iteration = 10;
var cycle = 1;

while (cycle < iteration){
  newer = (old) + (r);
  old = old.reversed;

  var oldstring = old.split("");  <!-- here is the error -->

  cycle++;
}
</script>

Upvotes: 1

Views: 163

Answers (2)

Barry Michael Doyle
Barry Michael Doyle

Reputation: 10658

Yeah so if he really wanted to he could also just say old.reversed ();

Upvotes: 0

Barry Michael Doyle
Barry Michael Doyle

Reputation: 10658

"reversed" isn't a default function in JavaScript.

You'll need to create and add a function like this one:

function reverse(s){
  return s.split("").reverse().join("");
}

Then replace old = old.reversed; with old = reverse(old);

This should solve the problem.

Upvotes: 1

Related Questions