Status
Not open for further replies.

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
3,261
Reaction score
2,537
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi people!

Well, after I see a lot of things on the internet, I can't figure that out.
I see some examples with .some() and with .every() but I can't manage to put it in my code and it works.

In the code below, the variables a,b,c will get a random value between 0 and 3.
So, if any of them are equal( a = b || a = c || b= c), then the code will make a random value again for each one of the variables. If not, the loop will break and the code will proceed.

my code:
Code:
var a, b, c, habitat;

function SetHabitat() {
a = Math.floor(Math.random()*4);
b = Math.floor(Math.random()*4);
c = Math.floor(Math.random()*4);
return;
};


for (var habitat = [a, b, c];; SetHabitat()) {
if((a !== b) && (a !== c) && (c !== b)) {
habitat = [a, b, c];
break;
}
};

console.log(habitat);

How can I check if are any equal values inside an array?

I manage to do that. But only when I specify the arrays.
But, in case that will be a lot of arrays. How can I make it simple? Like putting in the "if(any arrays are equal == false)" { then i execute my code}

Thanks!
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,641
Reaction score
1,475
First Language
French
Primarily Uses
RMMV
PHP:
    var habitat = [];
    while(habitat.length < 3){ // while will break when you will get 3 unique random data in array
        const r = ~~(Math.random()*4);
        (habitat.indexOf(r) === -1) && habitat .push(r); // if not unique, add value
    }
 
Last edited:

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
3,261
Reaction score
2,537
First Language
Portuguese - Br
Primarily Uses
RMMZ
PHP:
    var habitat = [];
    while(habitat.length < 3){ // while will break when you will get 3 unique random data in array
        const r = ~~(Math.random()*4)
        (habitat.indexOf(r) === -1) && habitat .push(r); // if not unique, add value
    }
Thanks!
But I cannot seem to understand, I think.
With your code, I can substitute all of mine, right? Or I have to find out where I can put it?

If I change the number in here, "habitat.length < 3", to 6 for example, this will generate more length to the array, right?

Also, it gets "r is not defined"
I try to define it with var or const but didn't work.
PcEvBbe.png

what is the goal for "~~"? :aswt:
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,228
Reaction score
11,239
First Language
Czech
Primarily Uses
RMMV
~~ is substitute od Math.floor.

As for the r, he forgot a semicolon at the end of the line. In some JS interpreters they are important.
Also, if const doesn't work, try var.
Either way it looks like it should work.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,641
Reaction score
1,475
First Language
French
Primarily Uses
RMMV

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,641
Reaction score
1,475
First Language
French
Primarily Uses
RMMV
i also recommend you create a function or method
PHP:
    /** Generate n random unic habitat */
    generateHabitat = function(max,range) {
        var buffer = [];
        while(buffer.length < max){ // while will break when you will get max unique random data in array
            var r = ~~(Math.random()*range); // range between 0=>range
            (buffer.indexOf(r) === -1) && buffer .push(r); 
        }
        return buffer;
    }



    var habitat = generateHabitat(6,30);
    console.log('habitat: ', habitat); // return a arrays with 6 random unic number range between 0=>30
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
3,261
Reaction score
2,537
First Language
Portuguese - Br
Primarily Uses
RMMZ
i also recommend you create a function or method
PHP:
    /** Generate n random unic habitat */
    generateHabitat = function(max,range) {
        var buffer = [];
        while(buffer.length < max){ // while will break when you will get max unique random data in array
            var r = ~~(Math.random()*range); // range between 0=>range
            (buffer.indexOf(r) === -1) && buffer .push(r);
        }
        return buffer;
    }



    var habitat = generateHabitat(6,30);
    console.log('habitat: ', habitat); // return a arrays with 6 random unic number range between 0=>30

Hi!!
I tested and it works!
But took me a while to understand it ^^''
The only things that I have doubt are this line:
"(buffer.indexOf(r) === -1) && buffer .push(r);"

why, indexOf(r) is equal to -1? What does that mean?
buffer .push(r); > This is for add another object inside the array, right?
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,641
Reaction score
1,475
First Language
French
Primarily Uses
RMMV
yes , don't worry this forum it for learn.
indexOf return a index of the element founds in a arrays , but if found nothing, it return -1;
you could also write `buffer.indexOf(r) < 0` it will work.
so to translate this code line `(buffer.indexOf(r) === -1) && buffer .push(r);`
it like `check if r existe in buffer, if not? (index-1)...continue loop ,
if yes (index>-1) , add inside bufferArray r and continue loop`

more info here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
3,261
Reaction score
2,537
First Language
Portuguese - Br
Primarily Uses
RMMZ
yes , don't worry this forum it for learn.
indexOf return a index of the element founds in a arrays , but if found nothing, it return -1;
you could also write `buffer.indexOf(r) < 0` it will work.
so to translate this code line `(buffer.indexOf(r) === -1) && buffer .push(r);`
it like `check if r existe in buffer, if not? (index-1)...continue loop ,
if yes (index>-1) , add inside bufferArray r and continue loop`

more info here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
OHHHH!!
I get it now "indexOf return a index of the element founds in a arrays , but if found nothing, it return -1;"
Thanks! I learn a lot from all this! ^^
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
16,768
Reaction score
9,304
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Profile Posts

SpyroFan67 wrote on HexMozart88's profile.
Hi Hex. It's good to see you back. You know, you didn't do anything wrong. That guy WAS being rude-you were RIGHT-and he continues to be rude and condescending in his posts. I know you went through a lot of trauma with being bullied. I honestly think you were just trying to protect other people so they wouldn't go through what you had to go through. I actually think that was quite noble of you to be honest.
Design Aesthetic Question Time. For the Main Menu:

Primary and Subclasses


Side By Side:

SbSClassesStyle.png

Or Stacked:

StackedClassesStyle.png
World building was one of the main reasons i got in to game dev, but geeez, it eats up the time... This is a WIP from today before i call it a night.. maybe 80% finished.
maps.png
Dial_000_f002_show.jpg
Wohoo, its finished. It took some time but slowly I get used to this retro 90s anime style.
Now I just have to finish the other 37 frames. for the first dialogue... oh lord, what have I done :LZSlol:
(and if you are confused: yes, I place various eastereggs about other great cyberpunk games/videos/books that I like in my game ;) )
Has anyone watched the movie "Tiny Toons: How I Spent My Summer Vacation"? It is a cartoon and it is freaking hilarious. We watched that movie so many times when our kids were growing up. Thankfully it has a lot of adult humor to keep the adults entertained that the kids don't really get.

Forum statistics

Threads
131,778
Messages
1,223,311
Members
173,563
Latest member
yiuro
Top