1

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>
6
  • I can't reproduce that error from the code you posted, but I do get one about iteration being undefined. Commented Feb 25, 2016 at 20:16
  • my bad there also needs to be var iteration = 20; at the top of the code Commented Feb 25, 2016 at 20:18
  • what is old.reversed supposed to do? Commented Feb 25, 2016 at 20:22
  • @BarryDoyle I changed my post. And yea there is cycle++ at the end (and inside) of the while loop. Commented Feb 25, 2016 at 20:22
  • @BarryDoyle old.reverse should reverse the old string. So the first letter of old will be the last etc. Commented Feb 25, 2016 at 20:25

2 Answers 2

1

"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.

Sign up to request clarification or add additional context in comments.

2 Comments

he's not even calling reverse as a function, he's using it as a property.
Final thing: you may want to clarify what you mean by "default function"
0

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

2 Comments

reverse() not reversed(), and you really should edit your already accepted answer instead of writing another - especially in this case because this is a better answer (provided you fix the function name)
It happens. Mobile is really bad for SO, it doesn't get the context at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.