Tic Tac Toe Winning Function - javascript

function checkWin(){
if (arro[0] === arro[1] === arro[2] === 1 || arro[3] === arro[4] === arro[5] === 1 || arro[6] === arro[7] === arro[8] === 1 || arro[0] === arro[4] === arro[8] === 1 || arro[2] === arro[4] === arro[6] === 1 || arro[0] === arro[3] === arro[6] === 1 || arro[1] === arro[4] === arro[7] === 1 || arro[2] === arro[5] === arro[8] === 1) {
console.log("O Won");
return "O";
}
else if (arrx[0] === arrx[1] === arrx[2] === 1 || arrx[3] === arrx[4] === arrx[5] === 1 || arrx[6] === arrx[7] === arrx[8] === 1 || arrx[0] === arrx[4] === arrx[8] === 1 || arrx[2] === arrx[4] === arrx[6] === 1 || arrx[0] === arrx[3] === arrx[6] === 1 || arrx[1] === arrx[4] === arrx[7] === 1 || arrx[2] === arrx[5] === arrx[8] === 1){
console.log("X Won");
return "X";
}
else
return "notwin"; }
Here, the arro is the matrix for O and the arrx is the array for X.
Running this in the console returns notwin everytime. Some help would be great. Thanks.

You can't combine condition checks like that. When you do a === b === c, what you're doing is comparing the result value of the a === b expression (which will be true [if they're the same] or false [if not]) with the value of c.
Instead, you need to combine them with &&, e.g. a === b && b === c.
E.g.:
function checkWin() {
if ((arro[0] === arro[1] && arro[1] === arro[2]) ||
(arro[3] === arro[4] && arro[4] === arro[5]) ||
/*...and so on...*/
) {
console.log("O Won");
return "O";
}
// ...
Side note: If you return from the block attached to the if, there's no need for the else prior to the next if. It's harmless, but pointless.

Related

How to return more than one thing in if statement?

I want to display all the product which fulfilled a condition.
if(correctSize === 3 || correctSize === 4) {return key == "3kw" }
if(correctSize === 4 || correctSize === 5) {return key == "4kw"}
if(correctSize === 5 || correctSize === 6) {return key == "5kw" }
if(correctSize === 6 || correctSize === 7) {return key == "6kw" }
but the problem is that it return just one object (the first with return).
there is easy way to say the statement is true if correctSize is from 4 to 6?
Try to create an array of results and push your answers inside.
For example:
let results= [];
if(correctSize === 3 || correctSize === 4) results.push("3kw");
if(correctSize === 4 || correctSize === 5) results.push("4kw");
if(correctSize === 5 || correctSize === 6) results.push("5kw");
if(correctSize === 6 || correctSize === 7) results.push("6kw");
return results;

How to make microbenchmark with console.time, to measure small differences in compiler optimization?

This code is an adaptation of this other one... It is an ugly code but the question is about "how to do a benchmark".
The new console.time function measure the "real execution time" or is it not so reliable? Is it really reliable for all browsers or can it change a bit?PS: is better to use Date.now() (or something like Unix terminal time), external time reference?
How to check "compiler optimizations" as ignoring a loop that do nothing?
The benchmark below is supposed to measure the cost of operations == and === that is near the same. The problem is that each browser (Firefox and Chorme) results in different average time... So,
Make sense to use an "average time for all browsers" when optimizations are oposite? There are another way to estimate execution times?
Test by yourself in different browsers, or also with NodeJs... And remember that it is not an usual performance ("which is faster?"), but a measure of the reality, a way to check what the compilers are doing (in average) to implement the ECMA 262 prescriptions.
var testString = "444442";
var testNumber = 444442;
var testString2 = "444443";
var testObject = {};
var testObject2 = {x:1};
const MAX = 1000000;
var name;
var result;
name='===';
result = 0;
console.time('Operation '+name);
for(var i = 0; i < MAX; i++){
result += Number(
(testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
&& (testString2 === testString || testNumber === testNumber) && (testObject2 === testObject || testString === testString) && (result === testNumber || testObject === testObject)
)
testNumber++
}
console.timeEnd('Operation '+name);
var result0 = result;
name='=='
result = 0;
console.time('Operation '+name);
var result = null;
for(var i = 0; i < MAX; i++){
result += Number(
(testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
&& (testString2 == testString || testNumber == testNumber) && (testObject2 == testObject || testString == testString) && (result == testNumber || testObject == testObject)
)
}
console.timeEnd('Operation '+name);
if (result0 != result)
console.log("OOPS BUG, result0 differing result: ", result0, result)
name='NoOp'
result = 0;
console.time('Operation '+name);
for(var i = 0; i < MAX; i++){
result = true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true &&
true && true && true
}
console.timeEnd('Operation '+name);
Sample results (cutting zeros of ms)
On Firefox:
Operation ===: 80 ms
Operation ==: 200 ms
Operation NoOp: 8 ms
On Chrome:
Operation ===: 21 ms
Operation ==: 27 ms
Operation NoOp: 22 ms
On NodeJS:
Operation ===: 108 ms
Operation ==: 152 ms
Operation NoOp: 9 ms
Conclusion: === is a little bit faster in all compilers.
The problem: running the other cited code, of this answer ("Rick test"),
On Firefox:
Operation ===: 1089 u
Operation ==: 280 u
Operation NoOp: 281 u
On Chrome:
Operation ===: 352 u
Operation ==: 349 u
Operation NoOp: 349 u
On NodeJS:
Operation ===: 201 u
Operation ==: 387 u
Operation NoOp: 195 u
Conclusion: == (not ===) is faster in browsers, but inverse for Node.
Final conclusions: no convergence of benchmark results, even when using more loops, etc. Seems that Rick's test is subjected to optimizations (that changes over compilers), and the above here not.
Final conclusions: there is no convergence of benchmark results, even when using more loops, etc. It seems that Rick's test is subject to (compiler dependent) optimizations, and the above test (in this page) is not.
PS: of course, ideally (by specification) the operators == and === have same time when comparing equal datatypes, so is difficult to measure difference... But this is the question, I want to check this little difference with console.time.

Need solution for if statement logic

I need help on a condition logic using if statement as following:
Variables A and B contain 3 properties which is level1, level2 and level3.
level1 and level2 can be 0 or more and level3 is null or numeric.
Variables A and B can be null.
Currently I have this condition:
if (A.level1 == 0 and B.level1 == 0) {
code here
} else if (A.level2 == 0 and B.level2 == 0) {
code here
} else if (A.level3 != null and B.level3 != null) {
code here
}
The problem is that this code doesn't handle the Variables A and B can be null part. The code should handle that part like this:
When A is null, B will still go through the same condition but without A and vice versa.
However, if A and B is null then the condition will be false at once.
I have problem in how to implement the Variables A and B can be null part in my condition, any advice?
Add the A and B isNull check in your condition:
if (A == null && B == null) {
return;
} else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) {
// code here
} else if ((A == null || A.level2 == 0) && (B == null || B.level2 == 0)) {
// code here
} else if ((A == null || A.level3 != null) && (B == null || B.level3 != null)) {
// code here
}
Explanation,
Take A for example in this else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) statement:
By putting the A == null || in (A == null || A.level1 == 0) && (B == null || B.level1 == 0), if A is null, then this A.level1 == 0 check will be ignored, thus the check will be equivalent with else if (B == null || B.level1 == 0).
Since the first if already check A == null && B == null, the else if below won't have A and B both null. Therefore now else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) will be equivalent to else if (B.level1 == 0).
p.s. Here we are taking advantage of the || characteristic, that is if the first condition is fulfilled, the second condition will be ignored.
Seems like this is what you're looking for:
// Both A and B are null
if(A == null && B == null){
// do something
}
// Only A is null
else if(A == null){
if(B.level1 == 0){
// do something
}
else if(B.level2 == 0){
// do something
}
else if(B.level3 != null){
// do something
}
}
// Only B is null
else if(B == null){
if(A.level1 == 0){
// do something
}
else if(A.level2 == 0){
// do something
}
else if(A.level3 != null){
// do something
}
}
// Neither A or B are null
else{
if(A.level1 == 0 && B.level1 == 0){
// do something
}
else if(A.level2 == 0 && B.level1 == 0){
// do something
}
else if(A.level3 != null && B.level3 != null){
// do something
}
}
I suggest to check for falsy values of A or B first and then check the properties.
var A = null,
B = null;
if (!A && !B) {
console.log('A or B is null');
} else if (A.level1 === 0 && B.level1 === 0) {
console.log(1);
} else if (A.level2 === 0 && B.level2 === 0) {
console.log(2);
} else if (A.level3 != null && B.level3 != null) {
console.log(3);
}
Handle A and B separately,
if(!A){
if(A.level1 === 0 && A.level2 ===0){
}
else if(A.level1 === 0){
}
else if(A.level1 > 0){
}
if(A.level2 === 0){
}
else if(A.level2 > 0){
}
if(A.level3 === null){
}
else if (typeof (A.level3) === "number"){
}
}
same for B here...

Why is the 'o' is true? Javascript(Angular)

Help me please, I'm trying to make a rule for the little game and there is the problem.
I'm creating winning combination and say if the cell && cell+1 && cell+2 == to 'X' then you win, but when between two "X"s presents "o" it also says that "X" wins. Why? Please see my code and the game example on link a the bottom.
this.rezult = function(){
this.arr2.forEach(function(arr, i, innerArr){
arr.forEach(function(val, j){
var wincomb = innerArr[i][j] && innerArr[i][j+1] && innerArr[i][j+2];
var wincomb2 = innerArr[i][j] && innerArr[i+1][j] && innerArr[i+2][j];
var wincomb3 = innerArr[i][j] && innerArr[i+1][j+1] && innerArr[i+2][j+2];
console.log(wincomb == "X" && innerArr[i][j] !== "o");
// console.log(innerArr);
// THE RULE
if(wincomb == "X"){
alert(' X wins!');
}
});
});
};
Link to JSFiddle
In JavaScript, the && operator has interesting behavior with non-boolean values.
If the left-side of && is "truthy", the result is the right-side.
If the left-side of && is "falsey", the result is the left-side.
All non-empty strings are "truthy".
So, consider these examples:
("A" && "B" && "C") === "C"
("" && "B" && "C") === ""
(0 && "B" && "C") === 0
("X" && "X" && "O") === "O"
("O" && "O" && "X") === "X"
By the looks of it, you're trying to check if all 3 values are equal. You shouldn't use && for that, you should use === for that.
At the risk of doing your homework for you ;) here's a good way to do this:
function areTheSame(a,b,c) {
return a === b && b === c;
}
var down = areTheSame(innerArr[i][j], innerArr[i][j+1], innerArr[i][j+2]);
var across = areTheSame(innerArr[i][j], innerArr[i+1][j], innerArr[i+2][j]);
var diagonal = areTheSame(innerArr[i][j], innerArr[i+1][j+1], innerArr[i+2][j+2]);
if (down || across || diagonal) {
var winner = innerArr[i][j];
alert( winner + " wins!");
}

Javascript syntax

How do you say if (A == 0) OR (B == 0)?
Just to be snarky:
if (A === 0 || B === 0)
if (A == 0 || B == 0)
or
if ((A == 0) || (B == 0))
Check out Control Structures and Operators on Wikibooks
if ( A == 0 || B == 0 ) {
}
depends if you mean exclusive or inclusive OR :)
Inclusive OR:
if(A == 0 || B == 0)
{
}
Exclusive OR:
if(A == 0 && B != 0 || A != 0 && B == 0)
{
}

Categories

Resources