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


Groups > comp.lang.javascript > #38715

Re: Allowed moves database chessboard, knight to many entries....

Newsgroups comp.lang.javascript
Subject Re: Allowed moves database chessboard, knight to many entries....
From "Evertjan." <exxjxw.hannivoort@inter.nl.net>
References (7 earlier) <b5f52c14-ed6e-4b0a-852d-dca3393d5889o@googlegroups.com> <XnsAC1582524E3CAeejj99@194.109.6.166> <39f38e8f-f303-4f76-9034-dc51bf49f7d6o@googlegroups.com> <XnsAC16EFA465EBFeejj99@194.109.6.166> <fc58b34d-2cb6-4c28-85e4-31f569a5903do@googlegroups.com>
Date 2020-08-12 11:58 +0200
Message-ID <XnsAC1779BE25C4eejj99@194.109.6.166> (permalink)

Show all headers | View raw


=?UTF-8?Q?Jonas_Th=C3=B6rnvall?= <jonas.thornvall@gmail.com> wrote on 12
Aug 2020 in comp.lang.javascript: 

> Den tisdag 11 augusti 2020 kl. 23:33:36 UTC+2 skrev Evertjan.:
>> <jonas.thornvall@gmail.com> wrote on 11 Aug 2020 in
>> comp.lang.javascript: 
>> 
>> > Honestly Evertjan i find 
>> > Math.random() < 0.5
>> > giving a boolean weird enough
>> 
>> The result of any comparison must be a boolean.
>> 
>> Programming needs some honest brain exercise.
>> 
>> -- 
>> Evertjan.
>> The Netherlands.
>> (Please change the x'es to dots in my emailaddress)
> 
> But you were correct Evertjan my code was double checking, probably
> because i did not care to learn what the expresssion actually did mean. 
> 
> So now i changed it to something more sensible.
> But it still hard to read because you have to know that Math.random
> gives a value between 0 and 1. Personally i have hard to read code where
> one operator is a function, maybe it is just to use it often to get
> familiarised with how the code look. 
> 
> if (Math.random()<0.5){uplayer="black";} else {uplayer="white";}
> 

When you have to initialize the variable,
skip the undefined fase i your mind, so
perhaps this makes more sense to you:

let uplayer = "white";
if ( Math.random() < 0.5 )
         uplayer = "black";

========

But still, 
using a boolean variable for your task makes more sense,
as probably it works better in the overall code:

==================
let uplayerIsWhite = Math.random() < 0.5 ;
==================

so you do not have to compare it to a string over and over again:

==================
if ( uplayerIsWhite ) doThis() else doThat();
==================

or transferring it to the inside of a single function,
witch is nice for recursive reentrant functions,
also handy in prognostic testing:

==================
let uplayerIsWhite = Math.random() < 0.5 ;
....

doSteps( uplayerIsWhite, 7 ); // 7 steps to do

function doSteps( playerColour, stepsToGo ) {

    	// do your step depending on playerColour

    	console.log('playerColour:', playerColour, 'stepsToGo:', stepsToGo );

    	if ( stepsToGo > 0 ) doSteps( ! playerColour, stepsToGo - 1 );
};
==================

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


Thread

Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-07-28 14:31 -0700
  Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-07-28 14:53 -0700
  Re: Allowed moves database chessboard, knight to many entries.... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-07-29 00:55 +0200
    Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-06 09:31 -0700
      Re: Allowed moves database chessboard, knight to many entries.... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-06 19:01 +0200
        Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-08 04:43 -0700
          Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-08 04:53 -0700
            Re: Allowed moves database chessboard, knight to many entries.... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-08 14:45 +0200
              Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-10 02:11 -0700
                Re: Allowed moves database chessboard, knight to many entries.... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-10 12:48 +0200
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-11 11:24 -0700
                Re: Allowed moves database chessboard, knight to many entries.... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-11 23:33 +0200
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-11 14:40 -0700
                Re: Allowed moves database chessboard, knight to many entries.... luserdroog <luser.droog@gmail.com> - 2020-08-11 15:57 -0700
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-12 00:40 -0700
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-12 00:42 -0700
                Re: Allowed moves database chessboard, knight to many entries.... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-08-12 12:12 +0100
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-12 05:13 -0700
                Re: Allowed moves database chessboard, knight to many entries.... luserdroog <luser.droog@gmail.com> - 2020-08-12 10:23 -0700
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-12 00:50 -0700
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-12 01:00 -0700
                Re: Allowed moves database chessboard, knight to many entries.... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-12 11:58 +0200
                Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-12 05:14 -0700
          Re: Allowed moves database chessboard, knight to many entries.... "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-08 14:44 +0200
          Re: Allowed moves database chessboard, knight to many entries.... Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-08-22 08:10 -0700
          Re: Allowed moves database chessboard, knight to many entries.... Scott Sauyet <scott@sauyet.com> - 2020-08-25 16:03 -0700
  Re: Allowed moves database chessboard, knight to many entries.... Scott Sauyet <scott@sauyet.com> - 2020-08-25 15:06 -0700

csiph-web