3 out of 4 Boolean Check | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

3 out of 4 Boolean Check

Can someone give me an example of how to check if 3 out of 4 Boolean variables are valid? This is what I tried but didnt work: if ((a?1:0) + (b?1:0) + (c?1:0) + (d?1:0) >= 3)

1st Sep 2019, 9:44 PM
Jacques Cozart
Jacques Cozart - avatar
4 Answers
+ 3
var a = true; var b = true; var c = false; var d = true; if(a + b + c + d == 3){ console.log("is 3"); }else{ console.log("not 3"); }
1st Sep 2019, 10:01 PM
Anton Böhler
Anton Böhler - avatar
+ 2
Jacques Cozart I see some logical error here, one is that you should be using '==' operator instead of '>=' operator as you are looking for exactly 3 true values. second error is not understanding precedence and associativity of operators, whenever you are unsure of it, you can make use of brackets() //not sure whether 1+2*3 will be taken as (1+2)*3 or 1+(2*3) if (((a?1:0)+(b?1:0)+(c?1:0)+(d?1:0))==3) console.log("is 3"); else console.log("not 3"); //I am also not sure of these in javascript so I used () to remove if any such error
2nd Sep 2019, 2:52 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Thanks, I will test it out!
2nd Sep 2019, 3:06 AM
Jacques Cozart
Jacques Cozart - avatar
0
So an applcant needs to have at least 3 of the 4 skills. Here is what I have and still get the wrong output. It accepts the word skill, but then puts the next three skill questions in a line with a 0 at the end?...... public static void Boolean ApllicantSkills(Boolean word, Boolean spreadSht, Boolean dataBase, Boolean graph) { Boolean applicantSkills =true; if(((word?1:0) + (spreadSht?1:0) + (dataBase?1:0) + (graph?1:0)) ==3) System.out.println(“Qualified”); else System.out.println(“Not Qualified”); return applicantSkills; }}
2nd Sep 2019, 2:47 PM
Jacques Cozart
Jacques Cozart - avatar