-2

i am trying to make a colourwheel which works by selecting 2 colours first and finally outputting the right color when combining those two original colours. I have got mostly everything working but cannot get the operators to work accordingly, either im doing something wrong and maybe someone could guide me in the right direction?.

I have tried reading up about operators but to no help, can anyone help or at least point me in the right direction ?

this is the function i am having trouble with...

function resultColorFunction()  {
    if (colorSquareOne === blue || red && colorSquareTwo === red || blue)    
        {resultColor = purple;}
          
    else if (colorSquareOne == blue || yellow & colorSquareTwo == yellow || blue)       
        {resultColor = green;}

   else if (colorSquareOne == yellow || red & colorSquareTwo == red || yellow)    
        {resultColor = orange;}

    show();
}
Martijn
  • 15,791
  • 4
  • 36
  • 68
Freshtone
  • 27
  • 5
  • whats the input and whats the incorrect output? provide us more details or else it'll be closed soon – Shashank Vivek May 06 '20 at 09:51
  • It's obvious what is wanted. This doesnt need to be closed, all you have to do is read his conditions. The fact that the comments below answer his question is proof enough. – Martijn May 06 '20 at 09:53
  • 1. You likely want to compare strings. so `if (colorSquareOne === "blue"` - 2. You cannot do `if (something === x || y)` you need to specify the variable again: `if (something === x || something === y)` – mplungjan May 06 '20 at 09:53
  • 1
    `colorSquareOne === blue || red` should be `colorSquareOne === blue || colorSquareOne == red` (Assuming there are variables named `red` and `blue` in the scope. If you are comparing strings, you need to add quotes around them `"red"`). And Single `&` should be `&&` – adiga May 06 '20 at 09:53
  • @Martijn I have reopened. There are other logical issues in the code. OP only wants purple when one of them is blue and the other is red. Not when both of them are blue. – adiga May 06 '20 at 10:02
  • Dupes for the most glaring issue: https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values https://stackoverflow.com/questions/13737091/concise-way-to-compare-against-multiple-values – mplungjan May 06 '20 at 10:04
  • Please update the question with HTML and the remaining scripts for show etc in a [mcve] – mplungjan May 06 '20 at 10:06
  • Thank you for the help, i apologise for not propperly searching before asking this question, i am new here so will be more observative next time. Also my apologise for being a little unspecific about what the problem was ! i will try harder next time ! but thank you for the help i think i actually got the answer and it works ! – Freshtone May 06 '20 at 11:06

1 Answers1

1

Your first error is because you think like a human. You think 'when example is blue or red' so you write:

if ( example === 'blue' || 'red'){}

You have to check two times:

if ( example === 'blue' || example === 'red'){}

Your second mistake is the use of OR (||) and AND (and). Even if your current code would pass mistake one, you have this now:

if ((one === 'blue' || one === 'red') && (two === 'red' || two === 'blue'))    

That would be ambigious. If both are red or both are blue, this would also match. You have the AND and OR switched, it should be:

if (one === 'blue' && two === 'red') || (one === 'red' && two === 'blue')

You also repeat yourself a lot, this code isnt maintainable if you have way more colours. I suggest you create a small function to do the checking and then use that:

function isCombination(one, two, color1, color2){
    return  (one === color1 && two === color2) || (one === color2 && two === color1);
}


function resultColorFunction()  {
    if (isCombination(colorSquareOne, colorSquareTwo, 'blue', 'red') {
        resultColor = purple;
    }
    else if (isCombination(colorSquareOne, colorSquareTwo, 'blue', 'yellow') {
        resultColor = green;
    }   
    else if (isCombination(colorSquareOne, colorSquareTwo, 'red', 'yellow') {
        resultColor = orange;
    }

    show();
}
Martijn
  • 15,791
  • 4
  • 36
  • 68