<?xml version="1.0" encoding="UTF-8"?>
	<rss version="2.0"
		xmlns:content="http://purl.org/rss/1.0/modules/content/"
		xmlns:wfw="http://wellformedweb.org/CommentAPI/"
		xmlns:dc="http://purl.org/dc/elements/1.1/"
		xmlns:atom="http://www.w3.org/2005/Atom"

			>

	<channel>
		<title>Cycling 74  &#187;  Topic: Beginning Javascript &#8211; While + If/else problem.</title>
		<atom:link href="http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/feed" rel="self" type="application/rss+xml" />
		<link>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/feed</link>
		<description></description>
		<pubDate>Wed, 19 Jun 2013 17:17:01 +0000</pubDate>
		<generator>http://bbpress.org/?v=2.2.4</generator>
		<language></language>

		
														
					
				<item>
					<guid>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-60856</guid>
					<title><![CDATA[Beginning Javascript &#8211; While + If/else problem.]]></title>
					<link>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-60856</link>
					<pubDate>Wed, 23 May 2012 16:24:17 +0000</pubDate>
					<dc:creator>Mike S</dc:creator>

					<description>
						<![CDATA[
						<p>Hello Javascript forum!</p>
<p>I&#8217;ve just started to learn Javascript, and have a specific problem to work on. This is the jist of it is here &#8211;  <a href="http://cycling74.com/forums/topic.php?id=39655" rel="nofollow">http://cycling74.com/forums/topic.php?id=39655</a></p>
<p>Below is my code, which all works apart from the query function at the bottom which crashes Max. </p>
<p>Any ideas?</p>
<p><code><br />
// automatically updates js object in max environment<br />
autowatch = 1;</code></p>
<p>//define number of inlets and outlets<br />
inlets = 1;<br />
outlets = 1;</p>
<p>//define variables<br />
var start_time = new Array();<br />
var end_time = new Array();<br />
var mouse_pos = 0;</p>
<p>// get length of coll to set array length<br />
function set_length(coll_length)<br />
{<br />
    start_time.length = coll_length;<br />
    end_time.length = coll_length;<br />
    post(start_time.length, "is the length of the start_time array n");<br />
}</p>
<p>// populate the array, x is index, y is value<br />
function populate(index,start,end)</p>
<p>{<br />
    start_time[index] = start;<br />
    end_time[index] = end;<br />
    post(start_time[index], end_time[index], "n");</p>
<p>}</p>
<p>// check things are working by sending an integer which outputs the data at the index specified.<br />
function msg_int(x)<br />
{<br />
    post("The start time at index "+ x + " is equal to " + start_time[x], "n");</p>
<p>}</p>
<p>// query </p>
<p>function query(mouse_x)<br />
{</p>
<p>    var current_segment = 1;</p>
<p>        while (current_segment < = start_time.length)</p>
</p><p>        {</p>
<p>            if (mouse_x >= start_time[current_segment] &#038;&#038; mouse_x < = end_time[current_segment])</p>
</p><p>                {<br />
                    outlet(0, current_segment);<br />
                }</p>
<p>            else</p>
<p>                {<br />
                    outlet(0, -1);<br />
                }</p>
<p>        }<br />
}</p>
<p></p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219109</guid>
					<title><![CDATA[Re: Beginning Javascript &#8211; While + If/else problem.]]></title>
					<link>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219109</link>
					<pubDate>Wed, 23 May 2012 17:11:08 +0000</pubDate>
					<dc:creator>jamesson</dc:creator>

					<description>
						<![CDATA[
						<p>does the post work?</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219110</guid>
					<title><![CDATA[Re: Beginning Javascript &#8211; While + If/else problem.]]></title>
					<link>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219110</link>
					<pubDate>Wed, 23 May 2012 17:22:32 +0000</pubDate>
					<dc:creator>Mike S</dc:creator>

					<description>
						<![CDATA[
						<p>Yes, everything is fine up until the while loop.</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219111</guid>
					<title><![CDATA[Re: Beginning Javascript &#8211; While + If/else problem.]]></title>
					<link>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219111</link>
					<pubDate>Wed, 23 May 2012 19:58:50 +0000</pubDate>
					<dc:creator>Mike S</dc:creator>

					<description>
						<![CDATA[
						<p>I changed the mouse_pos declared at the top to mouse_x, so that&#8217;s not it! Still crashes.</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219112</guid>
					<title><![CDATA[Re: Beginning Javascript &#8211; While + If/else problem.]]></title>
					<link>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219112</link>
					<pubDate>Wed, 23 May 2012 20:10:34 +0000</pubDate>
					<dc:creator>Christopher Dobrian</dc:creator>

					<description>
						<![CDATA[
						<p>Looks like your query function is very likely to result in an endless loop (the while loop). What will make it stop looping endlessly, if neither current_segment nor start_time.length is being changed?</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219113</guid>
					<title><![CDATA[Re: Beginning Javascript &#8211; While + If/else problem.]]></title>
					<link>http://cycling74.com/forums/topic/beginning-javascript-while-ifelse-problem/#post-219113</link>
					<pubDate>Wed, 23 May 2012 22:24:42 +0000</pubDate>
					<dc:creator>Mike S</dc:creator>

					<description>
						<![CDATA[
						<p>Hi Christoper, </p>
<p>That makes complete sense, thanks. I needed to increment the loop AND also use &#8216;return 0&#8242; when the if statement was true. </p>
<p>The following appears to work. </p>
<p><code><br />
function query(mouse_x)<br />
{</code></p>
<p>    var current_segment = 1;</p>
<p>        while (current_segment < = start_time.length)</p>
</p><p>        {</p>
<p>            if (mouse_x >= start_time[current_segment] &#038;&#038; mouse_x < = end_time[current_segment])</p>
</p><p>                {<br />
                    outlet(0, current_segment);<br />
                    return 0;<br />
                }</p>
<p>            else</p>
<p>                {</p>
<p>                    outlet(0, -1);<br />
                    current_segment ++;<br />
                }</p>
<p>        }<br />
}<br /></p>
						]]>
					</description>

					
					
				</item>

					
		
	</channel>
	</rss>

