I am totally missing something

I am totally missing something
0

#1

And I dunno what it is! Everything was going well and then I realized that it’s playing all of the ‘replays’ twice each, after the initial one. I’ve went through every single piece of my code, tinkered with it, etc., and I cannot figure out which part is making it happen. So if you’d like to take a look and see if you can help me, that’d be awesome opposum.

https://codepen.io/k8codes/full/yPaRWJ/


#2
  function computerSelection() {
    if (curr == one) {
      action(one);
    } else if (curr == two) {
      action(two);
    } else if (curr == three) {
      action(three);
    } else if (curr == four) {
      action(four);
    }
  } 

Why not just?

  function computerSelection() {
    action(curr);
  } 

#3

That is definitely simpler and a good idea, but doesn’t really help with my current issue. :confused:


#4

It might help with many more issues that could have happened later on though :wink:


#5

I agree, and I’ve already updated it, and I also appreciate the tip. But, again, it doesn’t help with my current issue, which is the one I made the post about, and is slightly why I hesitated in posting at all. I’m not done with the challenge, just focusing on this current issue I am having.


#6

Good, but sometimes i get up to 7 second pauses between the program deciding which one to to flash. Its hard to decide my turn, thus, i get it wrong.


#7

Less code - fewer errors.


#8

What function plays demo-sequence?
How does this sequence forms?
Maybe function called twice. Maybe its called once, but plays wrong sequence.


#9

I added some console.log statements to your play function, so you can see what is happening. Run it a couple of times to see the interesting results. At one point when I ran it, it played over 30 times.

  function play() {
    console.log('i = ' + i);
    if (i < inPlay.length) {
      console.log('playing...');
      (flash === false)? on(): off();
      if (flash === false) {
        flash = true;
      } else {
        i++;
        flash = false;
      }
    } else {
      clearInterval(start);
    }
  } 

I believe this play function combined with the timed function (with the setinterval) are the culprits. Hopefully, this will get your started in the right direction.


#10

…yes. I understand that. Again, I was asking about a specific issue.


#11

Hmph. This simon challenge is going to be the end of me, I feel.

Those functions are the only way it’s worked at all. I couldn’t get it to work with setTimeout and a fellow coder suggested the play and timed function ideas. I think I’m just going to start over from scratch because sometimes I get it all jumbled up and keep making it worse. Thank you. :slight_smile:


#12

I wasn’t aware I had anything called demo-sequence.


#13

In Simon game computer generates random sequence of sounds and demonstrates it to player.
Then player has to repeat that sequence.

Demo-seq is just game abstraction.


#14

Okay, yes, you are TOTALLY right about the issue lying somewhere in here. I try to console a lot to keep things clear/find a problem but the way you suggested helped. Thanks!


#15

Even if you’re not using any special libraries or frameworks to manage state, it’s often useful to have a single object called state and keep all data regarding the app state in there. Then you can try console.logging the state object at points in the code where bugs are occurring. For example, in my Simon game (gh | pages), it’s declared like this:

const state = {
  init: function() {
    this.inProgress = false;
    this.userInputAllowed = true;
    this.sequence = [];
    this.idxToCheck = -1; //of sequence 
  }
};

Giving it an init() method also provides an easy way to revert it to its original state.


#16

That’s a nice praxis)

Another useful technique - is have as few mutable state as possible. And I believe these two are complement each other.


#17

My advice is put it aside for now … i have a whole list of things that are not quite right it would be just too awkward to sort it out … you used the code i showed you in previous thread but unfortunately you used it wrong eg you are using setInterval which calls function that uses setTimeout … this defeats the purpose of using setInterval

Also i see you still are trying to do the complete project all at once … rather than focus on a smaller part and get that working before moving on … this has you all tangled up as you have errors from the moment you hit start that hinder you throughout.

Focus only on computer playing his notes and flashing the div colours … nothing else … if you can do that … then its time to work on next bit
Do not use setTimeout() in your project … there is only one place i needed setTimeout … and that is when you are near completion … so for the timebeing setTimeout is a no no
So you can do a couple of things
A) ignore me and continue … im sure others will be glad to help but its going to be very tough going to untangle what you have created.
B) Start a fresh … create 4 divs … red , green yellow blue, … create an array eg panels=[’.red’,’.green’,’.yellow’,’.blue’]
and use setInterval to make it flash and playsound for each item in the array.
and again no using setTimeout for anything
C) i created a template with the divs and some of the code that i previously gave you + a bit extra … saves you doing it all from scratch … if you get it to work you will find the rest an awful lot easier … i can post it if you go with option C …


#18

I actually didn’t use anything wrong, just a bit differently. I also fixed the issue this post was about and it’s working fine now. The setTimeout that is called is not related to the setInterval other than it comes after that and it works just fine as I’ve tested it several times.

I’m also not trying to do too much at once. Everything that’s there has been done piece by piece. I’ve been working on this challenge for weeks.


#19

Nothing wrong with setTimeout. I use it all over the place in my Simon project and it’s working wonderfully. There are many ways of doing a project.