0
   

Why this variable has to be equal to quotation marks?

 
 
maniuni
 
Reply Thu 10 Nov, 2011 04:18 am
Here is an easy question I hope Smile
Could you explain to me why there has to be this variable “theRoots” and it has to be equal to quotation marks so the result can be achived. Why should there be this line at all: result=result+"Square root of "+x+" is "+number+"\n"
I tried to write in the alert like this: alert ("Square root of "+x+" is "+number+"\n"); but it doesn’t work.

<html>
<head>
<title></title>

<script type="text/javascript">

function mySquareRoot()
{
the Roots="";
for (var x=0; x<20;x++)
{
number=Math.sqrt(x);
number=number*10;
number=Math.round(number);
number=number/10;
theRoots=theRoots+"Square root of "+x+" is "+number+"\n"
}
alert (theRoots);
}
</script>

</head>
<body>

<a href="javascript:mySquareRoot()">Click here</a>

</body>
</html>
 
contrex
 
  3  
Reply Thu 10 Nov, 2011 07:58 am
@maniuni,
It makes the code in the loop simpler.

First you set the variable theRoots (note no space in the variable name!) equal to an empty string. This is what "" means. A zero length string. Then in the loop you append further strings, each terminated by a \n (carriage return) to what started out as an empty string.

Thus we avoid having to decide between creating the string if we are on the first pass or appending to it on subsequent passes.

0 Replies
 
 

Related Topics

 
  1. Forums
  2. » Why this variable has to be equal to quotation marks?
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/26/2024 at 11:57:37