I'm looking to identify a category based on this table:
I have an if statement that seems to work for some conditions, but not others. R, P, and Q are working, but conditions that go between numbers aren't returning the right category.
If statement:
function getCategory(featureFunctionalScore, featureDysfunctionalScore) {
if (featureFunctionalScore == 4 && featureDysfunctionalScore == -2) {
return "Performance";
} else if (featureFunctionalScore == 4 && featureDysfunctionalScore <= -1 && featureDysfunctionalScore > 4) {
return "Attractive"
} else if (featureFunctionalScore <= -1 && featureFunctionalScore > 4 && featureDysfunctionalScore == 4) {
return "Expected"
} else if ((featureFunctionalScore >= -2 && featureFunctionalScore <= 2 && featureDysfunctionalScore == -2) || (featureFunctionalScore == -2 && featureDysfunctionalScore >= -2 && featureDysfunctionalScore <= 2)) {
return "Reverse"
} else if ((featureFunctionalScore == 4 && featureDysfunctionalScore == -2) || (featureFunctionalScore == 2 && featureDysfunctionalScore == -1) || (featureFunctionalScore == -1 && featureDysfunctionalScore == 2) || (featureFunctionalScore == -2 && featureDysfunctionalScore == 4)) {
return "Questionable"
} else {
return "Indifferent"
};
};
Am I missing something important?
Update
This statement works in Excel, but I'm struggling to get it to work in JS:
=IF(OR(AND(C3 <= 2, B3 <= -1), AND(C3 <= -1, B3 <= 2)), "R", IF(AND(C3 <= 2, C3 >= -1, B3 <= 2, B3 >= -1), "I", IF(AND(C3 >= 2,B3 >= -1, B3 <= 2),"A", IF(AND(C3 <= 2, B3 <= 4, B3 >= 2), "M", IF(AND(C3 >= 2, B3 >= 2), "P", "Q")))))
This should be what you're looking for. I'm sure it could be optimized, but it works. JSFiddle: https://jsfiddle.net/yxb7tr9n/
function getCategory(x,y){
var answer = -999;
if (x == 4 && y == 4){
answer = "p";
}else if([-1,0,2].indexOf(x) >= 0 && y == 4){
answer = "A";
}else if((x == -2 && y == 4) || (x == -1 && y == 2) || (x == 4, y == -2)){
answer = "Q";
}else if(x == 4 && [-1,0,2].indexOf(y) >= 0) {
answer = "M";
}else if((x == -1 && [-1,0].indexOf(y) >= 0) || (x == 0 && [-1,0,2].indexOf(y) >= 0) || (x == 2 && [0,2].indexOf(y) >= 0)){
answer = "I";
}else if ((x == -2 && [-2,-1,0,2].indexOf(y) >= 0) || (y == -2 && [-2,-1,0,2].indexOf(x) >= 0)) {
answer = "R";
}else{
answer = "??";
}
return answer;
}
UPDATE: Alternate version using a coordinate mapping system. JSFiddle: https://jsfiddle.net/g2d6p4rL/4/
function indexOfCustom (parentArray, searchElement) {
for ( var i = 0; i < parentArray.length; i++ ) {
if ( parentArray[i][0] == searchElement[0] && parentArray[i][1] == searchElement[1] ) {
return i;
}
}
return -1;
}
function getCategory2(x,y){
var p = [[4,4]];
var q = [[-2,4],[-1,2],[2,-1],[4,-2]];
var a = [[-1,4],[0,4],[2,4]];
var m = [[4,2],[4,0],[4,-1]];
var i = [[0,2],[2,2],[-1,0],[0,0],[2,0],[-1,-1],[0,-1]];
var r = [[-2,2],[-2,0],[-2,-1],[-2,-2],[-1,-2],[0,-2],[2,-2]];
coord = [x,y];
if (indexOfCustom(p,coord) >= 0){
return "p";
} else if (indexOfCustom(q,coord) >= 0){
return "Q";
} else if (indexOfCustom(a,coord) >= 0){
return "A";
} else if (indexOfCustom(m,coord) >= 0){
return "M";
} else if (indexOfCustom(i,coord) >= 0){
return "I";
} else if (indexOfCustom(r,coord) >= 0){
return "R";
}else{
return "??";
}
}
Output of all answers:
[-2,-2] = R
[-2,-1] = R
[-2,0] = R
[-2,2] = R
[-2,4] = Q
[-1,-2] = R
[-1,-1] = I
[-1,0] = I
[-1,2] = Q
[-1,4] = A
[0,-2] = R
[0,-1] = I
[0,0] = I
[0,2] = I
[0,4] = A
[2,-2] = R
[2,-1] = Q
[2,0] = I
[2,2] = I
[2,4] = A
[4,-2] = Q
[4,-1] = M
[4,0] = M
[4,2] = M
[4,4] = p
Related
I'm having a problem in my code where the initial x and y values (gx and gy) start out as positive, then jump to negative at around the third loop (specifically to coordinates (-8, 12)), then continue on as normal. I've checked multiple times, and there is only one thing that changes the variables. However, it doesn't set them to a negative number. Why does it keep going to that particular point?
Edit: To clarify, the fact that gx and gy become negative isn't the issue so much as the fact that they jump to (-8, 12) after being at (355, 350) for two loops. I'm unclear on why this happens, and have not been able to troubleshoot it yet. Any ideas why it goes to that point?
Also, it may be worth noting that I'm using a web editor called p5.js.
Here is my code:
var ris = 0;
var hyp = 0;
var gx = 355;
var gy = 350;
var count = 1;
var rem = 0
var dir = 0
var xdir = 1
var ydir = 1
var cx = 0
var cy = 0
var xpos = 200;
var ypos = 200;
var moveHoriz = 0;
var moveVert = 0;
function gmove(){
if (gx == xpos){
cx = 1
} else if(gy == ypos){
cy = 1
}
if (cx == 1 && cy == 1){
console.log("this functions");
if ((400 - gx)-(400-xpos) < 0 && (400-gy)-(400-ypos) == 0){
xdir = -1
ydir = 1
} else if ((400 - gx)-(400-xpos) < 0 && (400-gy)-(400-ypos) > 0){
xdir = -1
ydir = -1
} else if ((400 - gx)-(400-xpos) == 0 && (400-gy)-(400-ypos) > 0){
xdir = 1
ydir = -1
} else if ((400 - gx)-(400-xpos) > 0 && (400-gy)-(400-ypos) > 0){
xdir = -1
ydir = -1
} else if ((400 - gx)-(400-xpos) > 0 && (400-gy)-(400-ypos) > 0){
xdir = -1
ydir = 1
print("yeeeee")
} else if ((400 - gx)-(400-xpos) > 0 && (400-gy)-(400-ypos) < 0){
xdir = -1
ydir = -1
print("yeeeeee")
} else if ((400 - gx)-(400-xpos) == 0 && (400-gy)-(400-ypos) < 0){
xdir = 1
ydir = -1
print("yeeeeeee")
} else if ((400 - gx)-(400-xpos) < 0 && (400-gy)-(400-ypos) < 0){
xdir = -1
ydir = -1
print("yeeeeeeee")
}
} else {
ellipse(gx, gy, 20, 20);
ris = (ypos-gy);
run = (xpos-gx);
hyp = pow(ris, 2) + pow(run, 2);
hyp = sqrt(hyp);
stroke(0);
line(gx, gy, xpos, ypos);
console.log(xpos+10, ypos+10, gx-10, gy+10);
hyp = hyp/(100*hyp);
if ((400 - gx)-(400-xpos) > -30 && (400-gx)-(400-xpos) < 30 && (400-ypos)-(400-gy) > -30 && (400-ypos)-(400-gy) < 30){
if(count == 1){
rem = round(gx/400);
count = count + 1;
}
} else {
if(count == 1){
rem = round(gx/400);
count = count + 1;
} else {
gx = xdir*(rem*count);
console.log("math:", ydir*(rem*count))
gy = ydir*(rem*count);
count = count + 1;
ellipse(gx, gy, 20, 20);
console.log(count);
}
}
cx = 0;
cy = 0;
}
}
function setup() {
//this part isn't relevant either
createCanvas(400, 400);
print(gx, gy);
}
//this is a forever loop
function draw() {
background(255);
gmove();
ellipse(xpos, ypos, 20, 20);
}
}
//everything below this point is also irrelevant; it just makes an ellipse move with wasd
function keyPressed(){
if (key == "a") {
moveHoriz = -1;
ellipse(xpos, ypos, 20, 20);
}
if (key == "d") {
moveHoriz = 1;
ellipse(xpos, ypos, 20, 20);
}
if (key == "w"){
moveVert = -1;
ellipse(xpos, ypos, 20, 20);
}
if (key == "s"){
moveVert = 1;
ellipse(xpos, ypos, 20, 20);
}
}
function keyReleased(){
if (key == "d") {
moveHoriz = 0;
}
if (key == "a") {
moveHoriz = 0;
}
if (key == "s"){
moveVert = 0;
}
if (key == "w"){
moveVert = 0;
}
}
Apologies for the length, and thank you for taking the time to look at this!
After some extensive reading and help from the community I have started to create formulae in the form of calculators to practice my knowledge using my real world influences. I have created this calculator and added an additional condition to the equation but now the working calculator can't define a function which had worked previously.
The value is defined and given at the beginning of the function so I don't understand why it is not longer defined when adding the condition.
var lsaForm = document.forms["lsacalc"],
days = document.getElementById('days'),
rlsa = document.getElementById("rlsa"),
dayRateLsa = document.getElementById("rlsa"),
rddlsa = document.getElementById("rddlsa"),
tlsa = document.getElementById('tlsa');
function lsaupdateTotals() {
var x = rlsa.value;
var y = dayRateLsa.value;
if (x == 1 || (y > 1 && y < 281)) {
rdlsa = 7.45;
} else if (x == 2 || (y > 280 && y < 461)) {
rdlsa = 11.65;
} else if (x == 3) {
rdlsa = 15.85;
} else if (x == 4) {
rdlsa = 17.40;
} else if (x == 5) {
rdlsa = 18.73;
}
let total = days.value * rdlsa;
rddlsa.innerHTML = `Daily rate of LSA £${rdlsa}`;
tlsa.innerHTML = `Total LSA Enitlement £${total}`;
}
I have resolved this by applying the rest of the conditions throughout the function, the console returned an error but unspecified. Theoretically I think that the value of 'RDLSA' returned different with the additional conditions.
function lsaupdateTotals() {
var x = rlsa.value;
var y = dayRateLsa.value;
if (x = 1 || (y > 1 && y < 281)) {
rdlsa = 7.45;
} else if (x = 2 || (y > 280 && y < 461)) {
rdlsa = 11.65;
} else if (x = 3 || (y > 461 && y < 640)) {
rdlsa = 15.85;
} else if (x = 4 || (y > 641 && y < 821)) {
rdlsa = 17.40;
} else if (x = 5 || (y > 821 && y < 1001)) {
rdlsa = 18.73;
} else if (x = 6 || (y > 1001 && y < 1181)) {
rdlsa = 20.07;
} else if (x = 7 || (y > 1181 && y < 1360)) {
rdlsa = 21.39;
} else if (x = 8 || (y > 1361 && y < 1540)) {
rdlsa = 23.40;
} else if (x = 9 || (y > 1541 && y < 1720)) {
rdlsa = 24.75;
} else if (x = 10 || (y > 1721 && y < 1901)) {
rdlsa = 26.09;
} else if (x = 11 || (y > 1901 && y < 2081)) {
rdlsa = 27.42;
} else if (x = 12 || (y > 2081 && y < 2261)) {
rdlsa = 28.77;
} else if (x = 13 || (y > 2261 && y < 2241)) {
rdlsa = 30.09;
} else if (x = 14 || (y > 2241 && y < 2801)) {
rdlsa = 31.43;
} else if (x = 15 || (y > 2801 && y < 3160)) {
rdlsa = 32.75;
} else if (x = 16 || y < 3161) {
rdlsa = 34.07;
};
let total = days.value * rdlsa;
rddlsa.innerHTML = `Daily rate of LSA £${rdlsa}`;
tlsa.innerHTML = `Total LSA Enitlement £${total}`;
}
How can I loop through an array of numbers and compare them to multiple sets of numbers. I am trying to program the game logic for a Tic Tac Toe project. When the players click a square the number of the square that was clicked is stored in p1Taken or p2Taken. I am trying to loop through those arrays and compare the numbers to the numbers for the squares needed to win. Only the first if statement works and I don’t think it’s bc of the number of squares but because there is 3 numbers in the array. Here is the code that I have so far:
const turnTaken = {
X: [],
O: []
}
var whosTurn = 'X';
let p1Taken = turnTaken.X;
let p2Taken = turnTaken.O;
const gameGrid = document.getElementsByClassName("grid");
const message = document.getElementById("message");
for(i=0, l=gameGrid.length; i < l; i++){
const boardNum = gameGrid[i].getAttribute("value");
gameGrid[i].addEventListener('click', function(e) {
if(whosTurn == 'X') {
message.innerHTML = "O's Turn";
player1Turn(e);
} else {
message.innerHTML = "X's Turn";
player2Turn(e);
}
});
}
function player1Turn(e){
const clickedValue = e.target.getAttribute("value");
const clicked = e.target;
const xIcon = document.createElement("IMG");
const srcX = "img/x-icon.png";
xIcon.setAttribute("src", srcX);
clicked.appendChild(xIcon);
p1Taken.push(clickedValue);
whosTurn = 'O';
player1CheckWin(p1Taken);
}
function player2Turn(e){
const clickedValue = e.target.getAttribute("value");
const clicked = e.target;
const oIcon = document.createElement("IMG");
const srcO = "img/o-icon.png";
oIcon.setAttribute("src", srcO);
clicked.appendChild(oIcon);
p2Taken.push(clickedValue);
whosTurn = 'X';
player2CheckWin(p2Taken);
}
function player1CheckWin(p1Taken){
for(var i=0; i < p1Taken.length; i++) {
if(p1Taken.length > 2) {
if(p1Taken[i] == 1 || p1Taken[i] == 2 || p1Taken[i] == 3) {
console.log("first");
message.innerHTML = "X Won!";
} else if(p1Taken[i] == 4 || p1Taken[i] == 5 || p1Taken[i] == 6) {
console.log("second");
message.innerHTML = "X Won!";
} else if(p1Taken[i] == 7 || p1Taken[i] == 8 || p1Taken[i] == 9) {
console.log("third");
message.innerHTML = "X Won!";
} else if(p1Taken[i] == 1 || p1Taken[i] == 4 || p1Taken[i] == 7) {
message.innerHTML = "X Won!";
} else if(p1Taken[i] == 2 || p1Taken[i] == 5 || p1Taken[i] == 8) {
message.innerHTML = "X Won!";
} else if(p1Taken[i] == 3 || p1Taken[i] == 6 || p1Taken[i] == 9) {
message.innerHTML = "X Won!";
} else if(p1Taken[i] == 1 || p1Taken[i] == 5 || p1Taken[i] == 9){
message.innerHTML = "X Won!";
} else if(p1Taken[i] == 3 || p1Taken[i] == 5 || p1Taken[i] == 7) {
message.innerHTML = "X Won!";
} else {
break;
}
} else {
break;
}
}
}
function player2CheckWin(p2Taken){
for(var j=0; j < p2Taken.length; j++) {
if(p2Taken.length > 2) {
if(p2Taken[j] == 1 || p2Taken[j] == 2 || p2Taken[j] == 3) {
message.innerHTML = "O Won!";
} else if(p2Taken[j] == 4 || p2Taken[j] == 5 || p2Taken[j] == 6) {
message.innerHTML = "O Won!";
} else if(p2Taken[j] == 7 || p2Taken[j] == 8 || p2Taken[j] == 9) {
message.innerHTML = "O Won!";
} else if(p2Taken[j] == 1 || p2Taken[j] == 4 || p2Taken[j] == 7) {
message.innerHTML = "O Won!";
} else if(p2Taken[j] == 2 || p2Taken[j] == 5 || p2Taken[j] == 8) {
message.innerHTML = "O Won!";
} else if(p2Taken[j] == 3 || p2Taken[j] == 6 || p2Taken[j] == 9) {
message.innerHTML = "O Won!";
} else if(p2Taken[j] == 1 || p2Taken[j] == 5 || p2Taken[j] == 9){
message.innerHTML = "O Won!";
} else if(p2Taken[j] == 3 || p2Taken[j] == 5 || p2Taken[j] == 7) {
message.innerHTML = "O Won!";
} else {
break;
}
} else {
break;
}
}
}
I'll just pick one of your predicates:
p1Taken[i] == 1 && 2 && 3
This is true iff all of the following are true:
p1Taken[i] is similar to 1 (after necessary casting)
2 is true (after necessary casting)
3 is true (after necessary casting)
So I know that the numbers 2 and 3 are truthy and thus casting to boolean will both be true. If you meant to compare p1Taken[i] to the 3 numbers you need to do 3 comparisons:
p1Taken[i] == 1 || p1Taken[i] == 2 || p1Taken[i] == 3
If the number always are integers you can use ranges:
p1Taken[i] >= 1 && p1Taken[i] <= 3
EDIT
The key to making something easy rather than hard is to have a though out data structure that supports the operations you want to do. I would imagine that a board with the indexes representing the positions would be just as easy to update and it will be easy to check if one won. Here is how I would have modelled it:
function getWinner(b) {
if (b[4] && (
b[3] == b[4] && b[4] == b[5] ||
b[0] == b[4] && b[4] == b[8] ||
b[2] == b[4] && b[4] == b[6] ||
b[1] == b[4] && b[4] == b[7])) {
return b[4];
}
if (b[0] && (
b[1] == b[0] && b[0] == b[2] ||
b[3] == b[0] && b[0] == b[6])) {
return b[0];
}
if (b[8] && (
b[6] == b[8] && b[8] == b[7] ||
b[2] == b[8] && b[8] == b[5])) {
return b[8];
}
return false;
}
// tests
const o = 'o';
const x = 'x';
const n = undefined;
const test1 = [
o, o, x,
x, o, x,
o, n, x
];
const test2 = [
o, x, x,
x, o, o,
o, n, x
];
const test3 = [
o, x, x,
o, o, o,
x, n, x
];
console.log(getWinner(test1)); // prints x
console.log(getWinner(test2)); // prints false
console.log(getWinner(test3)); // prints o
you need to correct your logic of comparison
change this
p1Taken[i] == 1 && 2 && 3
to
p1Taken[i] == 1 && p1Taken[i] == 2 && p1Taken[i] == 3
similarly for others
&& operator expects expressions to be compared, in your case you gave first expression as p1Taken[i] == 1 which is correct but second & third expression given were '2' & '3' which are truthy values
Now about your logic part, the above code still wont give correct result as you expecting same thing p1Taken[i] to be equal to multiple values at same time.
so just a hint, p1Taken keep it as array of taken tile nos & use condition as
p1Taken.includes(1) && p1Taken.includes(2) && p1Taken.includes(3)
TicTacToe game
I keep having
Uncaught SyntaxError: Unexpected token ( at pen.js:121
on my code on codepen, which causes it to abort execution, although it is working in Brackets & other editors and other people browsers.
Here is the link : http://codepen.io/N0bl3/full/EKPYZB/
/*jshint esnext:true, browser:true*/
var TICTACTOE = {
grid: {
size: 3,
color: "#000000",
plays: []
},
players: [{
name: "Cross",
symbol: "X"
}, {
name: "Circle",
symbol: "O"
}],
currentPlayer: 1,
played: 0,
drawGrid(ctx) {
for (var i = 0; i <= this.grid.size; i++) {
ctx.beginPath();
ctx.strokeStyle = "red";
ctx.lineWidth = "1";
ctx.moveTo(0, 200 / 3 * i);
ctx.lineTo(200, 200 / 3 * i);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(200 / 3 * i, 0);
ctx.lineTo(200 / 3 * i, 200);
ctx.stroke();
}
},
drawSign(ctx, symbol, x, y) {
//Canvas draw
if (symbol === "X") {
ctx.strokeStyle = "red";
ctx.lineWidth = "1";
ctx.beginPath();
ctx.moveTo(200 / 3 * x, 200 / 3 * y);
ctx.lineTo(200 / 3 * (x + 1), 200 / 3 * (y + 1));
ctx.stroke();
ctx.beginPath();
ctx.moveTo(200 / 3 * (x + 1), 200 / 3 * y);
ctx.lineTo(200 / 3 * x, 200 / 3 * (y + 1));
ctx.stroke();
} else {
ctx.strokeStyle = "green";
ctx.lineWidth = "1";
ctx.beginPath();
ctx.arc(200 / 3 * x + 200 / 3 / 2, 200 / 3 * y + 200 / 3 / 2, 200 / 3 / 2, 0, 2 * Math.PI);
ctx.stroke();
}
alert("Drawing: " + x + " " + y);
},
init() {
var c = document.getElementById("TICTACTOE");
var ctx = c.getContext("2d");
this.drawGrid(ctx);
c.addEventListener("click", function(event) {
var x = event.clientX;
var y = event.clientY;
var sizeSquare = 200 / 3;
var legal = false;
if (x < sizeSquare && y < sizeSquare && typeof TICTACTOE.grid.plays[0] !== "number") {
TICTACTOE.grid.plays[0] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[0]].symbol, 0, 0);
legal = true;
} else if (x >= sizeSquare && x < sizeSquare * 2 && y < sizeSquare && typeof TICTACTOE.grid.plays[1] !== "number") {
TICTACTOE.grid.plays[1] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[1]].symbol, 1, 0);
legal = true;
} else if (x >= sizeSquare * 2 && y < sizeSquare && typeof TICTACTOE.grid.plays[2] !== "number") {
TICTACTOE.grid.plays[2] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[2]].symbol, 2, 0);
legal = true;
} else if (x < sizeSquare && y >= sizeSquare && y < sizeSquare * 2 && typeof TICTACTOE.grid.plays[3] !== "number") {
TICTACTOE.grid.plays[3] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[3]].symbol, 0, 1);
legal = true;
} else if (x >= sizeSquare && x < sizeSquare * 2 && y >= sizeSquare && y < sizeSquare * 2 && typeof TICTACTOE.grid.plays[4] !== "number") {
TICTACTOE.grid.plays[4] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[4]].symbol, 1, 1);
legal = true;
} else if (x >= sizeSquare * 2 && y >= sizeSquare && y < sizeSquare * 2 && typeof TICTACTOE.grid.plays[5] !== "number") {
TICTACTOE.grid.plays[5] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[5]].symbol, 2, 1);
legal = true;
} else if (x < sizeSquare && y >= sizeSquare * 2 && typeof TICTACTOE.grid.plays[6] !== "number") {
TICTACTOE.grid.plays[6] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[6]].symbol, 0, 2);
legal = true;
} else if (x >= sizeSquare && y >= sizeSquare * 2 && x < sizeSquare * 2 && typeof TICTACTOE.grid.plays[7] !== "number") {
TICTACTOE.grid.plays[7] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[7]].symbol, 1, 2);
legal = true;
} else if (x >= sizeSquare * 2 && y >= sizeSquare * 2 && typeof TICTACTOE.grid.plays[8] !== "number") {
TICTACTOE.grid.plays[8] = TICTACTOE.currentPlayer;
TICTACTOE.drawSign(ctx, TICTACTOE.players[TICTACTOE.grid.plays[8]].symbol, 2, 2);
legal = true;
} else {
legal = false;
}
if (legal) {
TICTACTOE.played++;
TICTACTOE.checkEnd();
} else {
alert("This move is not allowed");
}
TICTACTOE.currentPlayer = TICTACTOE.currentPlayer === 1 ? 0 : 1;
document.getElementById("player").textContent = TICTACTOE.currentPlayer;
});
},
checkEnd() {
if (this.played >= 5) {
//Verifier les possibilités gagnantes 0 2 4 fonctionne pourquoi? et 023
var plays = this.grid.plays;
if (
(plays[0] === plays[1] && plays[1] === plays[2] && typeof plays[0] === "number") ||
(plays[0] === plays[4] && plays[4] === plays[8] && typeof plays[0] === "number") ||
(plays[0] === plays[3] && plays[3] === plays[6] && typeof plays[0] === "number") ||
(plays[1] === plays[4] && plays[4] === plays[7] && typeof plays[1] === "number") ||
(plays[2] === plays[4] && plays[4] === plays[6] && typeof plays[2] === "number") ||
(plays[2] === plays[5] && plays[5] === plays[8] && typeof plays[2] === "number") ||
(plays[3] === plays[4] && plays[4] === plays[5] && typeof plays[3] === "number") ||
(plays[6] === plays[7] && plays[7] === plays[8] && typeof plays[6] === "number")
) {
alert("The winner is: " + this.players[this.currentPlayer].name);
window.location.reload();
} else if (this.played >= 9) {
alert("Match end! No one wins this time!");
window.location.reload();
}
}
}
};
document.getElementById("player").textContent = TICTACTOE.currentPlayer;
TICTACTOE.init();
& the HTML :
<canvas id="TICTACTOE" width="200" height="200">Your browser doesn't support HTML 5 canvas. Consider upgrading or changing your browser</canvas>
<p>Hello!</p>
<p id="player"></p>
What is wrong?
Thanks.
Just formalizing my comment with an "official" answer. The error was being thrown on the following line:
// ...
alert("The winner is: " + this.players[this.currentPlayer].name);
window.(); // Uncaught SyntaxError: Unexpected token (
// ...
Whereas, the source code contains the following:
// ...
alert("The winner is: " + this.players[this.currentPlayer].name);
window.location.reload();
// ...
Clearly, codepen does not like the window.location.reload() calls (?) and is stripping that code out.
Note that any errors/exceptions that appear in your browser's dev tools usually include a link you can click to be taken to the offending line of source code.
for example I have this number:
1,234,567.89
And I want to convert this number to a number with a specific length like these:
1M
01M
1.2M
1.23M
1.234M
1.2346M
1.23457M
1,234,568
01,234,568
1,234,567.9
1,234,567.89
Is there any javascript function, plugin or something for doing this in such a way (with K, M, B...)?
I don't understand your downvotes because this a not a duplicated question und I found NOTHING similar. So i made it by myself and it works ;)
function number(num, size) {
var s = num;
var l1 = s.toString().length;
if (l1 > size && num > 1) s = Math.floor(num);
var l2 = s.toString().length;
if (l2 > size) {
if (num >= 1e3 && num < 1e6) {
s = num / 1e3;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'k';
} else if (num >= 1e6 && num < 1e9) {
s = num / 1e6;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'M';
} else if (num >= 1e9 && num < 1e12) {
s = num / 1e9;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'G';
} else if (num >= 1e12 && num < 1e15) {
s = num / 1e12;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'T';
} else if (num >= 1e15 && num < 1e18) {
s = num / 1e18;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'P';
} else if (num >= 1e18 && num < 1e21) {
s = num / 1e18;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'E';
} else if (num >= 1e21 && num < 1e24) {
s = num / 1e21;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'Z';
} else if (num >= 1e24 && num < 1e27) {
s = num / 1e24;
var m = Math.max(0, size - 2 - (s.toString().split('.')[0] || num).length);
s = ((m == 0) ? Math.floor(s) : s.toFixed(m)) + 'Y';
}
}
var l3 = s.toString().length;
if (l3 > size) s = '-';
var s = s.toString();
while (s.length < size) s = s.charAt(0) == '-' ? '-' + s : '0' + s;
var startZeros = /^(0)\1+/.exec(s),
startZerosCount = (startZeros != null) ? startZeros[0].length : 0,
decimalCount = (num.toString().split('.')[1] || []).length;
if (startZerosCount >= 2 && decimalCount > 0 && s.indexOf('.') < 0) {
var decimals = num.toString().split('.')[1],
movedDigits = Math.min(startZerosCount, decimalCount),
lastChar = s.substring(s.length - 1);
if (isNaN(lastChar)) {
s = s.substring(0, s.length - 1);
s = s.substring(movedDigits) + '.' + decimals.substring(0, movedDigits - 1);
s += lastChar;
} else {
s = s.substring(movedDigits) + '.' + decimals.substring(0, movedDigits - 1);
}
}
return s;
}
The output for 784432432.9999 is:
-
--
---
784M
0784M
784.4M
784.43M
784.432M
784432432
0784432432