Marzia Abasszadah Marzia Abasszadah - 3 months ago 17
Ajax Question

Ternary operator condition is never true, trying multiline ajax request

I am creating a Slot Machine. The ternary operator is always false, so the ajax request

$.ajax("delpoint.php")
works, but
$.ajax("addpoint")
doesn't. What I am sure of, is that the error is in this part:

Javascript snippet:

function check(){
$msg.html(
r[0] === r[1] && r[1] === r[2] ?
'You won! Enjoy your ' + reels[1][ (r[0] / 70 + 1) % 3 | 0 ].split(' ')[0]
var jqxhr = $.ajax( "addpoint.php")
:
'Try again'
var jqxhr = $.ajax( "delpoint.php")
);
}


And here is the full code :

/*
requestAnimationFrame polyfill
*/
(function(w){
var lastTime = 0,
vendors = ['webkit', /*'moz',*/ 'o', 'ms'];
for (var i = 0; i < vendors.length && !w.requestAnimationFrame; ++i){
w.requestAnimationFrame = w[vendors[i] + 'RequestAnimationFrame'];
w.cancelAnimationFrame = w[vendors[i] + 'CancelAnimationFrame']
|| w[vendors[i] + 'CancelRequestAnimationFrame'];
}

if (!w.requestAnimationFrame)
w.requestAnimationFrame = function(callback, element){
var currTime = +new Date(),
timeToCall = Math.max(0, 16 - (currTime - lastTime)),
id = w.setTimeout(function(){ callback(currTime + timeToCall) }, timeToCall);
lastTime = currTime + timeToCall;
return id;
};

if (!w.cancelAnimationFrame)
w.cancelAnimationFrame = function(id){
clearTimeout(id);
};
})(this);

/*
Slot Machine
*/
var sm = (function(undefined){

var tMax = 3000, // animation time, ms
height = 210,
speeds = [],
r = [],
reels = [
['coffee maker', 'teapot', 'espresso machine'],
['coffee filter', 'tea strainer', 'espresso tamper'],
['coffee grounds', 'loose tea', 'ground espresso beans']
],
$reels, $msg,
start;

function init(){
$reels = $('.reel').each(function(i, el){
el.innerHTML = '<div><p>' + reels[i].join('</p><p>') + '</p></div><div><p>' + reels[i].join('</p><p>') + '</p></div>'
});

$msg = $('.msg');

$('button').click(action);
}

function action(){
if (start !== undefined) return;

for (var i = 0; i < 3; ++i) {
speeds[i] = Math.random() + .5;
r[i] = (Math.random() * 3 | 0) * height / 3;
}

$msg.html('Spinning...');
animate();
}

function animate(now){
if (!start) start = now;
var t = now - start || 0;

for (var i = 0; i < 3; ++i)
$reels[i].scrollTop = (speeds[i] / tMax / 2 * (tMax - t) * (tMax - t) + r[i]) % height | 0;

if (t < tMax)
requestAnimationFrame(animate);
else {
start = undefined;
check();
}
}

function check(){
$msg.html(
r[0] === r[1] && r[1] === r[2] ?
'You won! Enjoy your ' + reels[1][ (r[0] / 70 + 1) % 3 | 0 ].split(' ')[0]
var jqxhr = $.ajax( "addpoint.php")
:
'Try again'
var jqxhr = $.ajax( "delpoint.php")
);
}

return {init: init}

})();

$(sm.init);

Answer

Try this:

function check() {
  $msg.html(r[0] === r[1] && r[1] === r[2] ?
    ('You won! Enjoy your ' + reels[1][(r[0] / 70 + 1) % 3 | 0].split(' ')[0],
    jqxhr = $.ajax("addpoint.php"), console.log("Enters"))
    : 
    ('Try again',
    jqxhr = $.ajax("delpoint.php"))
  );
}

It works for me, the only problem I have found is the exception thrown SyntaxError: missing : in conditional expression var jqxhr = $.ajax( "addpoint.php")

That meant you were not using comma opeartor to split two sentences within the ternary operator.

--> Here you have the test <--