sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

assigning randomized color choices to variable, squareColor #218

Closed bmaiden closed 9 months ago

bmaiden commented 9 months ago

What's the problem you're trying to solve?

Assigning randomized color choices to variable, squareColor

Post any code you think might be relevant (one fenced block per file)

/*----- state variables -----*/
let squareColor;

// function -randomColors- randomize colors for squares
function randomColors() {
  for (let i = 0; i < squareEls.length; i++) {
    const randomIndex = Math.floor(Math.random() * colors.length);
    squareColor = colors[randomIndex];
    colors.splice(randomIndex, 1);
    // console.log(squareColor);                 //here gives all colors
    // return (squareColor[randomIndex]);        //here gives one color with console log outside function
    // return (squareColor[randomIndex], i);     //here gives one color with console log outside function
  }
}

randomColors();
console.log(squareColor);         //here gives one color for the return statement within the function

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

returning only one color versus an array of colors

What is your best guess as to the source of the problem?

?

What things have you already tried to solve the problem?

moving "squareColor = colors[randomIndex];" under splice statement.

Paste a link to your repository here

https://github.com/bmaiden/memory-browser-game/commits/main

Clund1 commented 9 months ago

What does your colors index look like?

bmaiden commented 9 months ago

const colors = [ "red", "blue", "green", "yellow", "orange", "purple", "pink", "darkgray", "red", "blue", "green", "yellow", "orange", "purple", "pink", "darkgray", ];

timmshinbone commented 9 months ago

Where are you using the squareColor variable to apply the color?

bmaiden commented 9 months ago

Want to use it in the the squarePicked function.

bmaiden commented 9 months ago

Sorry when the square is picked.

timmshinbone commented 9 months ago

I should have stated more clearly, share the function where that's happening. Sorry about the miscommunication!

bmaiden commented 9 months ago

I want to use the squareColor within this function.

//function -squarePicked- determines squares selected with a event listener
function squarePicked(event) {
  //get index of square when clicked on
  const squareIdx = parseInt(event.target.id.replace(`sq-`, ""));
  // console.log('squareIdx:', squareIdx)     //console displays squareIdx
  //changed board array null to numbers and colors display on board.
  //change color of square when clicked on

  //render updated state
  render();
}
bmaiden commented 9 months ago

I am struggling here, been working on this aspect for 1 1/2 days now.

bmaiden commented 9 months ago

I am not sure how to proceed with the coding I have. Help.

bmaiden commented 9 months ago

Thank you, Nabila!! You went above and beyond, it's very much appreciated. Your approach on walking me through my coding challenge and explain in detail what changes were needed and why, makes me feel better prepared to finish this project. Thank you again!