Since this is an "instructional" answer, it will be longer than otherwise.
First off, you must define your tr variable
If you fail to define tr here you get an "Uncaught ReferenceError: tr is not defined" if used directly or undesired results if used in a string for example.
var tr;
alert(tr);//errors
Uncaught errors cause JavaScript to stop processing at the point of error
Example of bad declaration (no value) for our use here:
var tr; //done like this it is undefined (tr === undefined)
alert(typeof tr === "undefined") // alerts true
// now if we add a string to that we get:
tr = tr + "hi"; // returns string "undefinedhi" in tr by doing (undefined + "hi")
var tr = "";
alert(typeof tr === "string") // returns true
tr = tr + "hi" // returns string "hi" by doing ("" + "hi")
Thus we have to define our variable prior to use and assign an empty string to it (we DO want to create a string)
In JavaScript, the + is used for both addition of numbers as well as string concatenation.
You COULD use the concat function of the string prototype.
String.prototype.concat()
The concat() function combines the text from one or more strings and returns a new string. Changes to the text in one string do not affect the other string.
var oldstring = "Howdy";
var otherstring = "Freddy";
// combine 4 strings:
var newstring = oldstring.concat(otherstring, "End", " Is near");
This returns the text value of "HowdyFreddyEnd Is near" in newstring, oldstring still has "Freddy" as its value.
Note it is STRONGLY recommended to use the + and += instead of concat() for performance reasons, you simply have to understand the processing using those as described here.
Loops
Your requirement is to use n as a value and process from that value down to 1.
Negative while loop:
Negative while loops are faster than positive ones especially in older browsers, less so in more modern ones.
Here we use another feature of JavaScript where other types can be used as a Boolean. For numbers, any value that is positive will return true, 0 will return false. As long as we KNOW the value will start with a positive integer, we can simply use it (the number) and then process it in a while loop example: while (n). Note that -1 will return true as a Boolean value. For a single integer, we can simply use it, for other numbers we can coerce it into a Boolean value.
Some examples:
if (0) // returns false
if (-1) // returns true
if (1) // returns true
if (2 - 1) // returns true (1 value)
if (1-1) // returns false (value 0)
if (0 + "") // returns true (value "0" as a string) non-empty strings are true
if(n) // returns true for non zero values of n
if (n > 0) // returns true for positive values of n
if (n >= 1) //true for positive values of n (less clear than previous in my opinion)
if (n > -1 && n != 0) // same thing just more complicated
If we do NOT know if our value starts with a positive integer, we can check for that explicitly:
if(n > 0)
Why cover all that? your while (n <= 9) { will fail if n is 0 or negative to start with and has a hard coded value of 9 which is not typical.
Otherwise, we can simply use values starting with positive integers:
Example:
var n = 23;
var tr = ""; // done like this it is an empty string (tr === "")
while (n) {
tr = n + "," + tr;// put next lower value at the start
n--;
}
tr = tr.slice(0, -1); // remove last comma
Increment while loop
Process using a ++ requires use of a second variable
var n = 23;
var i = 1;
var tr = "";
while (i <= n) {
tr = tr + i + ",";
i++
}
tr = tr.slice(0, -1); // remove last comma
Use += instead of tr = tr + i + ",";
var n = 23;
var i = 1;
var tr = "";
while (i <= n) {
tr += i + ",";
i++
}
tr = tr.slice(0, -1); // remove last comma
Note on slice
tr = tr.slice(0, -1);
is the same as
tr = tr.slice(0, tr.length -1);