Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39059

Re: boolean while loops "how to stop" and "make room for textout to textarea"

Newsgroups comp.lang.javascript
Date 2020-10-31 10:05 -0700
References <6d2d33cc-9b19-469c-a25c-d8d2f862f0aco@googlegroups.com> <ba111515-c079-49bc-939b-bdc8eaf5279ao@googlegroups.com> <314f637c-5652-49f0-b79c-356f95244328o@googlegroups.com>
Message-ID <2397252d-ca36-4e93-8d73-88816bf4fabeo@googlegroups.com> (permalink)
Subject Re: boolean while loops "how to stop" and "make room for textout to textarea"
From Jonas Thörnvall <jonas.thornvall@gmail.com>

Show all headers | View raw


Den lördag 31 oktober 2020 kl. 14:08:00 UTC+1 skrev Julio Di Egidio:
> On Saturday, 31 October 2020 12:20:44 UTC+1, Jonas Thörnvall  wrote:
> > Den lördag 31 oktober 2020 kl. 11:44:43 UTC+1 skrev Jonas Thörnvall:
> > > When creating a while loop like this.
> > > while(run==true){
> > > 
> > > }
> > > 
> > > 1. How can i stop it, i tried a button that call function that set run=false but that do not work.
> > > 
> > > 2. How can i write out values to textarea while still in loop, it does not seem "to have time" want to do it until loop breaks?
> > 
> > <snipped disaster zone>
> 
> Your code is a complete mess!
> 
> Here is a working version, for the fun of it:
> <https://codepen.io/jp-diegidio/pen/mdExgZe>
> 
> Notice not just how to write that loop logic but also
> how *the logic is separate from the rendering*, and,
> more generally, *the use of functions* not only to
> partition the code, but also, in conjunction with a
> decent naming, to make it *dramatically* more readable
> and manageable. Code must speak (must be readable)!
> 
> Your sloppiness and lack of any I won't say discipline
> but even raw passion is appalling...
> 
> Julio

So again Julio, instead of point to code that obviously flawed in some functional way. 

1. Why does this loop not stop when stop button pushed?
The syntax tell it to do so but apparently Javascript or dome doesn't care.

2. Whey does it not write out value to textfield as the syntax tell, but apparently Javascript do not give a shit about syntactical order of the things to do.

<HTML>
<HEAD>
<script>
var run=false;

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

//Head = 1 Tail = 0
function streaks(){
	j=0;
	streak=0;consecutive=0;
	document.getElementById("textout").value="***WAIT***/n";
	choice=document.getElementById("mychoice").value;
	
	run=true;
	if (choice=="head"){
		//while (j<1000000000){
		while (run==true){
			myval=getRandomInt(2);
			if(myval==1){
                   	//alert(consecutive);
				consecutive=consecutive+1;
			} else {
				//alert("CONSECUTIVE  "+consecutive+" STREAK "+streak);
				if (consecutive>streak){
					mytext="TRY "+j+" STREAK "+choice+" "+consecutive+"\n";
					streak=consecutive;
		 			document.getElementById("textout").value+=mytext;
					//console.log(document.getElementById("textout").value);
					
				}
			consecutive=0;
			}
		
		
		j++;
		}
	} else if (choice=="tail"){
		//while (j<1000000000){
		while (run==true){
			myval=getRandomInt(2);
			if(myval==0){
                   		consecutive=consecutive+1;
			} else {
				
				if (consecutive>streak){
		  			mytext="Try "+j+" Consecutive "+choice+" "+consecutive+"\n";
					streak=consecutive;
		        		document.getElementById("textout").value+=mytext;
					//console.log(document.getElementById("textout").value);
					
				}
			consecutive=0;
			}
		
		j++;
		}
	} 
}

function stop(){
run=false;
}
</script>

</HEAD>
<h2>STATISTICAL OUTCOME **STREAKS** WHEN FLIPPING COINS</h2>

<BODY BGCOLOR="YELLOW">
<B>CONSECUTIVE COIN FLOPS  | SELECT</B>
<select name="mychoice" id="mychoice">
  <option value="head">HEADS</option>
  <option value="tail">TAILS</option>
</select>
<BR>
<textarea id="textout" name="textout" rows="20" cols="50"></textarea><BR>
<button onclick="streaks()">START COINFLIP</button> <button onclick="stop()">STOP FLIP</button>

</BODY>
</HTML>

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 03:44 -0700
  Re: boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 04:20 -0700
    Re: boolean while loops "how to stop" and  "make room for textout to textarea" Julio Di Egidio <julio@diegidio.name> - 2020-10-31 06:07 -0700
      Re: boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 09:41 -0700
      Re: boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 09:45 -0700
      Re: boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 09:48 -0700
        Re: boolean while loops "how to stop" and  "make room for textout to textarea" Julio Di Egidio <julio@diegidio.name> - 2020-10-31 10:10 -0700
          Re: boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 10:42 -0700
      Re: boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 10:05 -0700
        Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-10-31 18:49 +0100
          Re: boolean while loops "how to stop" and "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 11:07 -0700
            Re: boolean while loops "how to stop" and "make room for textout to textarea" John G Harris <niam@jghnorth.org.uk.invalid> - 2020-11-01 11:39 +0000
            Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-01 13:52 +0100
      Re: boolean while loops "how to stop" and  "make room for textout to textarea" Julio Di Egidio <julio@diegidio.name> - 2020-11-01 21:57 -0800
        Re: boolean while loops "how to stop" and  "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-01 23:44 -0800
  Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-10-31 18:24 +0100
    Re: boolean while loops "how to stop" and "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 10:44 -0700
      Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-10-31 18:52 +0100
        Re: boolean while loops "how to stop" and "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-10-31 11:14 -0700
          Re: boolean while loops "how to stop" and "make room for textout to textarea" John G Harris <niam@jghnorth.org.uk.invalid> - 2020-11-01 11:45 +0000
            Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-01 14:02 +0100
              Re: boolean while loops "how to stop" and "make room for textout to textarea" John G Harris <niam@jghnorth.org.uk.invalid> - 2020-11-01 17:38 +0000
                Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-01 21:07 +0100
                Re: boolean while loops "how to stop" and "make room for textout to textarea" John G Harris <niam@jghnorth.org.uk.invalid> - 2020-11-02 19:36 +0000
                Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-03 18:34 +0100
                Re: boolean while loops "how to stop" and "make room for textout to textarea" Julio Di Egidio <julio@diegidio.name> - 2020-11-03 11:48 -0800
                Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-04 15:17 +0100
                Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-04 15:18 +0100
                Re: boolean while loops "how to stop" and "make room for textout to textarea" Julio Di Egidio <julio@diegidio.name> - 2020-11-04 12:52 -0800
                Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-05 16:12 +0100
          Re: boolean while loops "how to stop" and "make room for textout to textarea" Arno Welzel <usenet@arnowelzel.de> - 2020-11-01 14:02 +0100
  Re: boolean while loops "how to stop" and "make room for textout to textarea" "sc...@sauyet.com" <scott@sauyet.com> - 2020-11-03 13:27 -0800
    Re: boolean while loops "how to stop" and "make room for textout to textarea" Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-03 14:08 -0800
      Re: boolean while loops "how to stop" and "make room for textout to textarea" Scott Sauyet <scott.sauyet@gmail.com> - 2020-11-03 17:28 -0800

csiph-web