This rule of three needs to identify were is the X or Y and then calculate it from it's position and the type of the proporcion (direct or inverse). But for some reason the page in HTML it's returning the value "ERROR!" (set on the else condition).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="radio" id="inv">Inversamente</input>
<input type="radio" id="dir">Diretamente</input><br />
<input type="text" id="I1"></input>
<input type="text" id="I2"></input><br />
<input type="text" id="I3"></input>
<input type="text" id="I4"></input><br /><br />
<button id="button" onclick="button()">Calcular</button>
<script type="text/javascript">
function button() {
var i1 = parseInt(document.getElementById("I1"));
var i2 = parseInt(document.getElementById("I2"));
var i3 = parseInt(document.getElementById("I3"));
var i4 = parseInt(document.getElementById("I4"));
var i5 = document.getElementById("inv").value;
var i6 = document.getElementById("dir").value;
if (i1.value == "x" && i5.value === true || i1.value =="y" && i5.value === true){
var r1 = i3.value*i4.value/i2.value;
document.write("Resultado é " + r1);
}
else if (i1.value == "x" && i6.value === true || i1.value =="y" && i6.value === true) {
var r2 = i3.value*i2.value/i4.value;
document.write("Resultado é " + r2);
}
else if (i2.value == "x" && i5.value === true || i2.value =="y" && i5.value === true) {
var r3 = i3.value*i4.value/i1.value;
document.write("Resultado é " + r3);
}
else if (i2.value == "x" && i6.value === true || i2.value =="y" && i6.value ===true) {
var r4 = i1.value*i4.value/i3.value;
document.write("Resultado é " + r4);
}
else if (i3.value == "x" && i5.value === true || i3.value =="y" && i5.value === true) {
var r5 = i1.value*i2.value/i4.value;
document.write("Resultado é " + r5);
}
else if (i3.value == "x" && i6.value === true || i3.value =="y" && i6.value === true) {
var r6 = i1.value*i4.value/i2.value;
document.write("Resultado é " + r6);
}
else if (i4.value == "x" && i5.value === true || i4.value =="y" && i5.value === true) {
var r7 = i1.value*i2.value/i3.value;
document.write("Resultado é " + r7);
}
else if (i4.value == "x" && i6.value === true || i4.value =="y" && i6.value === true) {
var r8 = i2.value*i3.value/i1.value;
document.write("Resultado é " + r8);
}
else{
document.write("ERROR!");//whatever the case of the rule of the above, it aways return "ERROR!"
}
}
</script>
I'm sorry for my english. I'm brazilian.
You were running parseInt() on a DOM element instead of the element's value, but you're also looking for character input into anyone of those inputs. I've corrected the issues regarding value comparisons, but if all the inputs are not filled, you'll receive a NaN response.
Corrected JS:
function button() {
var i1 = document.getElementById("I1");
var i2 = document.getElementById("I2");
var i3 = document.getElementById("I3");
var i4 = document.getElementById("I4");
var i5 = document.getElementById("inv");
var i6 = document.getElementById("dir");
console.log(i1.value);
console.log(i2.value);
console.log(i3.value);
console.log(i4.value);
console.log(i5.checked);
console.log(i6.checked);
if (i1.value == "x" && i5.checked === true || i1.value == "y" && i5.checked === true) {
var r1 = i3.value * i4.value / i2.value;
console.log("Resultado é " + r1);
} else if (i1.value == "x" && i6.value === true || i1.value == "y" && i6.value === true) {
var r2 = i3.value * i2.value / i4.value;
console.log("Resultado é " + r2);
} else if (i2.value == "x" && i5.value === true || i2.value == "y" && i5.value === true) {
var r3 = i3.value * i4.value / i1.value;
console.log("Resultado é " + r3);
} else if (i2.value == "x" && i6.value === true || i2.value == "y" && i6.value === true) {
var r4 = i1.value * i4.value / i3.value;
console.log("Resultado é " + r4);
} else if (i3.value == "x" && i5.value === true || i3.value == "y" && i5.value === true) {
var r5 = i1.value * i2.value / i4.value;
console.log("Resultado é " + r5);
} else if (i3.value == "x" && i6.value === true || i3.value == "y" && i6.value === true) {
var r6 = i1.value * i4.value / i2.value;
console.log("Resultado é " + r6);
} else if (i4.value == "x" && i5.value === true || i4.value == "y" && i5.value === true) {
var r7 = i1.value * i2.value / i3.value;
console.log("Resultado é " + r7);
} else if (i4.value == "x" && i6.value === true || i4.value == "y" && i6.value === true) {
var r8 = i2.value * i3.value / i1.value;
console.log("Resultado é " + r8);
} else {
console.log("ERROR!"); //whatever the case of the rule of the above, it aways return "ERROR!"
}
}
Related
Creating a SharePoint Portal using JavaScript and HTML where the problem is when i'm inputting a number around 100,000 to 800,000; ex(523,546) it would enter in the first if condition and do the statement below in the. even though the value is less than the given MDV "Total Estimated Freight".
The value of the pagetdv is inputted.
var pagetdvz = document.getElementById('pagetdv');
var pagesrz = document.getElementById('pagesr');
var pagemdvz = document.getElementById('pagemdv');
/------------ Maximum Declared Value ----------/
if (document.getElementById('dropct').selectedIndex == 0)
document.getElementById('pagemdv').value = 1500000;
else if (document.getElementById('dropct').selectedIndex == 1)
document.getElementById('pagemdv').value = 3000000;
/------------ Dest + 20 Ftr ----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 50000;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 47583.67;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59981.33;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59000;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59,981.33;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 58500;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 49000;
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 51000;
/------------ Dest + 40 Ftr----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 95500;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 94944.66;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 117487.73;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 113000;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
/------------ Total Estimated Freight ----------/
if (document.getElementById('pagetdv').value > document.getElementById('pagemdv').value){
var tdva = parseFloat(pagetdvz.value) - parseFloat(pagemdvz.value);
var tdvb = tdva / 1000;
var tdvc = tdvb * 3.36;
var tef = tdvc + parseFloat(pagesrz.value);
document.getElementById('pagetef').value = tef;
}
else if (document.getElementById('pagetdv').value <= document.getElementById('pagemdv').value)
document.getElementById('pagetef').value = document.getElementById('pagesr').value;
I am trying to do filter based on mens/women in such a way that whenever I click on men then the url should be updated to women by finding men in url and replacing it.
Any help highly appreciated.
$('.men').on('click',function(){
var men = location.href;
if (men.includes("constraint") === false && men.includes("types") === false && men.includes("vendors") === false){
men += "?constraint=men";
}
else if(men.includes("constraint=men") === false && men.includes("+men") === false && men.includes("vendors") === false && men.includes("women") === false){
men += "+men";
// women.replace("women","men");
}
else if(men.includes("types") === true && men.includes("=men") === false){
men += "&constraint=men";
}
else if(men.includes("vendors") === true && men.includes("=men") === false){
men += "&constraint=men";
}
else if(men.includes("men") === true && men.includes("women") === false) {
men.replace("women","men");
}
else if(men.includes("=women") === true || men.includes("+women") === true){
alert("url contains women");
}
else{
men += "";
}
var men_result = location.href = men;
document.getElementById("wow").href = men_result;
});
var href = window.location.href;
if(href.indexOf('men') >= 0 && href.indexOf('women') === -1)
window.location.href = href.replace('men', 'women');
}
var boxId = ['boxMid','boxLeft','boxRight','boxTopMid','boxTopRight','boxTopLeft','boxLow Mid','boxLowLeft','boxLowRight'];
var turn = 0;
var pOne = "X";
var pTwo = "O";
var blankBox = "";
var boxMid = document.getElementById('boxMid').innerhtml;
var boxLeft = document.getElementById('boxLeft').innerhtml;
var boxRight = document.getElementById('boxRight').innerhtml;
var boxTopMid = document.getElementById('boxTopMid').innerhtml;
var boxTopLeft = document.getElementById('boxTopLeft').innerhtml;
var boxTopRight = document.getElementById('boxTopRight').innerhtml;
var boxLowMid = document.getElementById('boxLowMid').innerhtml;
var boxLowLeft = document.getElementById('boxLowLeft').innerhtml;
var boxLowRight = document.getElementById('boxLowRight').innerhtml;
var i = 0;
My player win function is not working and I've had several others look at it, so I figured I would get an outside perspective.
function xWins() {
if(boxId[0,1,2] == 'X'||
boxTopLeft == "X" && boxTopMid == "X" && boxTopRight == "X"||
boxLowMid == "X" && boxLowLeft == "X" && boxLowRight == "X"||
boxMid == "X" && boxTopMid == "X" && boxLowMid == "X"||
boxLeft == "X" && boxTopLeft == "X" && boxLowLeft == "X"||
boxRight == "X" && boxTopRight == "X" && boxLowRight == "X"||
boxMid == "X" && boxTopLeft == "X" && boxLowRight == "X"||
boxMid == "X" && boxLowLeft == "X" && boxTopRight == "X" ) {
alert ('Player One Wins!');
turn = 1;
}
}
function oWins(){
if(boxId[0,1,2] == "O"||
boxTopLeft == "O" && boxTopMid == "O" && boxTopRight == "O"||
boxLowMid == "O" && boxLowLeft == "O" && boxLowRight == "O"||
boxMid == "O" && boxTopMid == "O" && boxLowMid == "O"||
boxLeft == "O" && boxTopLeft == "O" && boxLowLeft == "O"||
boxRight == "O" && boxTopRight == "O" && boxLowRight == "O"||
boxMid == "O" && boxTopLeft == "O" && boxLowRight == "O"||
boxMid == "O" && boxLowLeft == "O" && boxTopRight == "O" ) {
alert ('Player Two Wins!');
turn = 0;
}
}
function test(bx){
if(document.getElementById(boxId[bx]).innerHTML == ""){
if (turn<1) {
document.getElementById(boxId[bx]).innerHTML = pOne;
document.onclick (turn++);
oWins();
xWins();
}
if (turn == 1) {
document.getElementById(boxId[bx]).innerHTML = pTwo;
document.onclick (turn--);
oWins();
xWins();
}
}
}
function reset() {
for(i=0;i<boxId.length;i++)
document.getElementsByClassName('boxy')[i].innerHTML = blankBox;
}
As far as I know, everything else works except for my scoreboard, but I'll figure that out when I get there. Please keep in mind that I am a high school student that has less than 2 months of real experience coding, so I probably did some things that could've been a lot easier, and more DRY.
Cause of Bug: You are telling it that oWins() by calling the oWins function, where it runs through your cascasde of OR conditionals to which one of them is matching. So you have 2 problems. 1, that oWins() is always testing true via your OR statements, and 2, that xWins() is being called after the oWins() function that is evaluating to true each time. Stringing together OR conditionals like that is problematic and often difficult to debug.
if (turn == 1) {
document.getElementById(boxId[bx]).innerHTML = pTwo;
document.onclick (turn--);
oWins();
xWins();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
In a project I am working on I have 21 buttons that all have active and inactive states. The state of certain buttons is affected by other buttons being pressed as well as that button being pressed. In my html I use ng-click to call a function updateActiveButtons(num) to activate or deactivate certain buttons.
The best way I could think of was to use an array of 21 elements, all of which were set to false by default and then changed when they were pressed.
The problem is that my code is UGLY and I know that there has to be a much better way to logic it out.
Here is my updateActiveButtons function:
/* Array for active buttons
0: Company Name 1: Country 2: Industry 3: Search 4: Company Name - Seller Name 5: Company Name - Buyer Name 6: Country - USA 7: Country - China 8: Country - Israel
9: Country - Russia 10: Country - India 11: Country - Japan 12: Industry - Tech 13: Industry - Consumer 14: Industry - Pharma 15: Industry - Financial 16: Industry - Biotech 17: Industry - Industrial
18: Date 19: Valuation 20: Industry - Business
*/
$scope.activeButtonArray = new Array(21);
for (var i = 0; i < $scope.activeButtonArray.length; i++) { $scope.activeButtonArray[i] = false; }
//pos = position in array
$scope.updateActiveButtons = function(pos) {
console.log($scope.activeButtonArray[20]);
if(pos != 0 || pos != 1 || pos != 2 || pos != 3 || pos != 4 || pos != 5) {
$scope.activeButtonArray[pos] = !$scope.activeButtonArray[pos];
} else if(pos == 3 && !$scope.activeButtonArray[pos]) {
$scope.activeButtonArray[pos] = true;
} else if(pos == 3 && $scope.activeButtonArray[pos]) {
$scope.activeButtonArray[pos] = false;
}
if(pos == 18 || pos == 19) {
$scope.activeButtonArray[0] = false;
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
}
if(pos == 0) {
$scope.activeButtonArray[0] = true;
if($scope.activeButtonArray[4] || $scope.activeButtonArray[5]) {
$scope.activeButtonArray[0] = true;
}
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
if($scope.search.text == undefined || $scope.search.text == '') {
$scope.activeButtonArray[3] = false;
}
}
if(pos == 1) {
if($scope.activeButtonArray[4] == false && $scope.activeButtonArray[5] == false) {
$scope.activeButtonArray[0] = false;
}
if($scope.activeButtonArray[6] == true || $scope.activeButtonArray[7] == true || $scope.activeButtonArray[8] == true || $scope.activeButtonArray[9] == true || $scope.activeButtonArray[10] == true || $scope.activeButtonArray[11] == true) {
$scope.activeButtonArray[1] = true;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
if($scope.search.text == undefined || $scope.search.text == '') {
$scope.activeButtonArray[3] = false;
}
}
if(pos == 2) {
if($scope.activeButtonArray[4] == false && $scope.activeButtonArray[5] == false) {
$scope.activeButtonArray[0] = false;
}
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == true || $scope.activeButtonArray[13] == true || $scope.activeButtonArray[14] == true || $scope.activeButtonArray[15] == true || $scope.activeButtonArray[16] == true || $scope.activeButtonArray[17] == true || $scope.activeButtonArray[20] == true) {
$scope.activeButtonArray[2] = true;
}
if($scope.search.text == undefined || $scope.search.text == '') {
$scope.activeButtonArray[3] = false;
}
}
if(pos == 3) {
if($scope.activeButtonArray[4] == false && $scope.activeButtonArray[5] == false) {
$scope.activeButtonArray[0] = false;
}
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
}
if(pos == 4) {
$scope.activeButtonArray[4] = true;
$scope.activeButtonArray[5] = false;
}
if(pos == 5) {
$scope.activeButtonArray[4] = false;
$scope.activeButtonArray[5] = true;
}
}
I have a lot of repeated code that comes out in a way that just doesn't feel very well done or professional. I wouldn't be proud to send this to a client. Does anyone have any suggestions as to how I could make this better?
On way would be to replace entire conditions (or blocks) by methods/functions
so
if($scope.activeButtonArray[4] || $scope.activeButtonArray[5]) {
$scope.activeButtonArray[0] = true;
}
becomes
if (somethingIsSomething($scope))
This has the added benefit of be much more self-documenting so you can "read" what you're doing.
I liked pixelearth's recommendation to just create another function so I did.
I decided to make a function that took an array, a start, and a end point as parameters and return true if any of the array values in that range are true.
Here is the function:
var arrayContainsTrue = function(arr, start, end) {
for(var i = start; i <= end; i++) {
if(arr[i] == true) {
return true;
}
}
return false;
}
and then to shorten my code I just did this (with different start and end points based on what was needed):
if(!arrayContainsTrue($scope.activeButtonArray, 6, 11))
I am trying to build a simple game of Tic Tac Toe with an AI that can only win or draw. I have taken the brute force approach to building this AI as the minmax approach is fairly complicated seen as I am new to javascript. The AI seems to work but the problem I have is that it makes too many moves when it is the AI's turn, so sometimes the AI will take two squares when I want it to take only one and sometimes it won't make any moves at all.
Code:
$(document).ready(function() {
var userChoice;
var computerChoice;
var dialogBox = $("#dialogBox");
var s1 = $("#top-left");
var s2 = $("#top");
var s3 = $("#top-right");
var s4 = $("#left");
var s5 = $("#middle");
var s6 = $("#right");
var s7 = $("#bottom-left");
var s8 = $("#bottom");
var s9 = $("#bottom-right");
var turn = 1; // turn 1=computer turn 2=user
var countMoves = 9;
renderDialog();
dialogChoice(); // user chooses X or O
move();
// make dialog box
function renderDialog() {
var winW = window.innerWidth;
var centerDialog = (winW / 2) - (600 * 0.5); // to center
dialogBox.css("display", "block")
dialogBox.css("left", centerDialog + "px");
dialogBox.css("top", "100px")
}
// make dialog interactive
function dialogChoice() {
var cross = $('#cross');
var circle = $('#circle');
cross.on("click", function() {
dialogBox.css("display", "none");
userChoice = "X";
computerChoice = "O";
computerRandom();
});
circle.on("click", function() {
dialogBox.css("display", "none");
userChoice = "O";
computerChoice = "X";
computerRandom();
});
}
// make function to allow user to click on square to move
function move() {
$("#top-left").on("click", function() {
if ($("#top-left").html() === '') {
$("#top-left").text(userChoice);
turn = 1;
userTurn();
};
});
$("#top").on("click", function() {
if ($("#top").html() === '') {
$("#top").text(userChoice);
turn = 1;
userTurn();
};
});
$("#top-right").on("click", function() {
if ($("#top-right").html() === '') {
$("#top-right").text(userChoice);
turn = 1;
userTurn();
}
});
$("#left").on("click", function() {
if ($("#left").html() === '') {
$("#left").text(userChoice);
turn = 1;
userTurn();
};
});
$("#middle").on("click", function() {
if ($("#middle").html() === '') {
$("#middle").text(userChoice);
turn = 1;
userTurn();
};
});
$("#right").on("click", function() {
if ($("#right").html() === '') {
$("#right").text(userChoice);
turn = 1;
userTurn();
};
});
$("#bottom-left").on("click", function() {
if ($("#bottom-left").html() === '') {
$("#bottom-left").text(userChoice);
turn = 1;
userTurn();
};
});
$("#bottom").on("click", function() {
if ($("#bottom").html() === '') {
$("#bottom").text(userChoice);
turn = 1;
userTurn();
};
});
$("#bottom-right").on("click", function() {
if ($("#bottom-right").html() === '') {
$("#bottom-right").text(userChoice);
turn = 1;
userTurn();
};
});
};
// switch turns on every round aswell as check for gameover tests.
function userTurn() {
computerAI();
computerRandom();
countMoves--;
console.log(turn);
if (isWinner() || draw()) {
countMoves = 9;
restartGame();
}
};
//check for winner
function isWinner() {
var result = false;
// first row
if (s1.text() == "X" && s2.text() == "X" && s3.text() == "X" || s1.text() == "O" && s2.text() == "O" && s3.text() == "O") {
s1.css("background", "green");
s2.css("background", "green");
s3.css("background", "green");
result = true;
return result;
}
//seconds row
if (s4.text() == "X" && s5.text() == "X" && s6.text() == "X" || s4.text() == "O" && s5.text() == "O" && s6.text() == "O") {
s4.css("background", "green");
s5.css("background", "green");
s6.css("background", "green");
result = true;
return result;
}
//third row
if (s7.text() == "X" && s8.text() == "X" && s9.text() == "X" || s7.text() == "O" && s8.text() == "O" && s9.text() == "O") {
s7.css("background", "green");
s8.css("background", "green");
s9.css("background", "green");
result = true;
return result;
}
// first column
if (s1.text() == "X" && s4.text() == "X" && s7.text() == "X" || s1.text() == "O" && s4.text() == "O" && s7.text() == "O") {
s1.css("background", "green");
s4.css("background", "green");
s7.css("background", "green");
result = true
return result;
}
//second column
if (s2.text() == "X" && s5.text() == "X" && s8.text() == "X" || s2.text() == "O" && s5.text() == "O" && s8.text() == "O") {
s2.css("background", "green");
s5.css("background", "green");
s8.css("background", "green");
result = true
return result;
}
//third column
if (s3.text() == "X" && s6.text() == "X" && s9.text() == "X" || s3.text() == "O" && s6.text() == "O" && s9.text() == "O") {
s3.css("background", "green");
s6.css("background", "green");
s9.css("background", "green");
result = true
return result;
}
// first diagonal
if (s1.text() == "X" && s5.text() == "X" && s9.text() == "X" || s1.text() == "O" && s5.text() == "O" && s9.text() == "O") {
s1.css("background", "green");
s5.css("background", "green");
s9.css("background", "green");
result = true
return result;
}
//second diagonal
if (s3.text() == "X" && s5.text() == "X" && s7.text() == "X" || s3.text() == "O" && s5.text() == "O" && s7.text() == "O") {
s3.css("background", "green");
s5.css("background", "green");
s7.css("background", "green");
result = true
return result;
}
};
function draw() {
var result = false;
if (countMoves == 0) {
result = true;
}
return result;
}
function restartGame() {
setTimeout(function() {
$(".col-xs-4").text('');
$(".col-xs-4").css("background", "red");
if (s5.text() == '') {
s5.text(computerChoice)
};
}, 2000)
}
function computerAI() {
countMoves--;
if (turn == 1) {
computerWin()
};
if (turn == 1) {
computerBlock()
};
if (turn == 1) {
computerFork()
};
if (turn == 1) {
blockFork()
}
if (turn == 1) {
computerRandom();
}
};
// computer will block opponent move
function computerBlock() {
// first Row1
if (s1.text() == userChoice && s2.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//first Row2
if (s3.text() == userChoice && s2.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//first Row3
if (s1.text() == userChoice && s3.text() == userChoice && s2.text() == '') {
s2.text(computerChoice);
turn = 2;
};
//second Row
if (s4.text() == userChoice && s5.text() == userChoice && s6.text() == '') {
s6.text(computerChoice);
turn = 2;
};
//second Row2
if (s6.text() == userChoice && s5.text() == userChoice && s4.text() == '') {
s4.text(computerChoice);
turn = 2;
};
//second Row3
if (s6.text() == userChoice && s4.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
//third row
if (s7.text() == userChoice && s8.text() == userChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third row2
if (s9.text() == userChoice && s8.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//third Row3
if (s9.text() == userChoice && s7.text() == userChoice && s8.text() == '') {
s8.text(computerChoice);
turn = 2;
};
//first column
if (s1.text() == userChoice && s3.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//first column2
if (s7.text() == userChoice && s4.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//first column3
if (s1.text() == userChoice && s7.text() == userChoice && s4.text() == '') {
s4.text(computerChoice);
turn = 2;
};
//second column
if (s2.text() == userChoice && s5.text() == userChoice && s8.text() == '') {
s8.text(computerChoice);
turn = 2;
};
//second column2
if (s8.text() == userChoice && s5.text() == userChoice && s2.text() == '') {
s2.text(computerChoice);
turn = 2;
};
//second column3
if (s2.text() == userChoice && s8.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
//third column
if (s3.text() == userChoice && s6.text() == userChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third column2
if (s9.text() == userChoice && s6.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//third column3
if (s3.text() == userChoice && s9.text() == userChoice && s6.text() == '') {
s6.text(computerChoice);
turn = 2;
};
// first diagonal
if (s1.text() == userChoice && s5.text() == userChoice) {
s9.text(computerChoice);
turn = 2;
};
//first diagonal 2
if (s9.text() == userChoice && s5.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
// first diagonal 3
if (s1.text() == userChoice && s9.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
//second diagonal
if (s3.text() == userChoice && s5.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//second diagonal2
if (s7.text() == userChoice && s5.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//second diagonal 3
if (s7.text() == userChoice && s3.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
};
// computer will make move to win
function computerWin() {
// take third for win
// first Row1
if (s1.text() == computerChoice && s2.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//first Row2
if (s3.text() == computerChoice && s2.text() == computerChoice) {
s1.text(computerChoice);
turn = 2;
};
//second Row
if (s4.text() == computerChoice && s5.text() == computerChoice && s6.text() == '') {
s6.text(computerChoice);
turn = 2;
};
//second Row2
if (s6.text() == computerChoice && s5.text() == computerChoice && s4.text() == '') {
s4.text(computerChoice);
turn = 2;
};
//third row
if (s7.text() == computerChoice && s8.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third row2
if (s9.text() == computerChoice && s8.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//first column
if (s1.text() == computerChoice && s3.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//first column2
if (s7.text() == computerChoice && s4.text() == computerChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//second column
if (s2.text() == computerChoice && s5.text() == computerChoice && s8.text() == '') {
s8.text(computerChoice);
turn = 2;
};
//second column2
if (s8.text() == computerChoice && s5.text() == computerChoice && s2.text() == '') {
s2.text(computerChoice);
turn = 2;
};
//third column
if (s3.text() == computerChoice && s6.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third column2
if (s9.text() == computerChoice && s6.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
// first diagonal
if (s1.text() == computerChoice && s5.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//first diagonal2
if (s9.text() == computerChoice && s5.text() == computerChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//second diagonal
if (s3.text() == computerChoice && s5.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//second diagonal2
if (s7.text() == computerChoice && s5.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
};
// computer make move to create a fork
function computerFork() {
// fork1
if (s1.text() == computerChoice && s5.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//fork2
if (s3.text() == computerChoice && s5.text() == computerChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//fork3
if (s7.text() == computerChoice && s5.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//fork4
if (s9.text() == computerChoice && s5.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
};
function blockFork() {
// fork1
if (s1.text() == userChoice && s5.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//fork2
if (s3.text() == userChoice && s5.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//fork3
if (s7.text() == userChoice && s5.text() == userChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//fork4
if (s9.text() == userChoice && s5.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
};
function computerRandom() {
var squareArr = [s1, s2, s3, s4, s5, s6, s7, s8, s9];
var randomNum = Math.floor(Math.random() * 8);
if (squareArr[randomNum].text() == '') {
squareArr[randomNum].text(computerChoice);
squareArr.splice(randomNum, 1);
turn = 2;
}
};
});
example:http://codepen.io/aliz16/full/yJgQQd/
Check you functions that play for the computer.
Let's take computerWin for an example. What is stopping the function from making multiple marks? Several Conditions could be true. Delegating the turn doesn't stop the checks.
P.S. not all lines are correct either.