function IsIntersect () { const enemyLeft = parseInt($('#enemies').css("left")); const playerLeft = parseInt($('#player').css("left")); const playerWidth = $("#player").width(); const enemyWidth = $('#enemies').width(); if ((playerLeft + playerWidth >= (enemyLeft * -1) && playerLeft < (enemyLeft * -1) + enemyWidth)) { console.log(true); return true; } else { console.log(false); return false; } } 

The fact is that the function does not return anything, it’s not about if’s work or something else, for some reason it doesn't give anything to the exit, what could be the matter?

  • How do you check what the function returns? - Nick

1 answer 1

This is a syntactically incorrect code. Click the "Run Code" button.

 function IsIntersect() { const enemyLeft = parseInt($('#enemies').css("left")); const playerLeft = parseInt($('#player').css("left")); const playerWidth = $("#player").width(); const enemyWidth = $('#enemies').width(); // Что значит эта строчка? playerLeft, "<", (enemyLeft * -1), "+", enemyWidth, true); if ((playerLeft + playerWidth >= (enemyLeft * -1) && playerLeft < (enemyLeft * -1) + enemyWidth)) { console.log(true); return true; } else { console.log(false); return false; } } 


Is the function called? Add before if :

 console.log(enemyLeft, playerLeft, playerWidth, enemyWidth); 

What is displayed in the console?

  • This line was accidentally copied, I corrected. - a.hatson
  • if works like that console.log (), which is displayed inside if, but does not want to work. - a.hatson 2:41 pm
  • It is very strange, but in my opinion the matter was in the function name, strange as it may sound, but after changing the name to check_intersect, the code started to work quite normally ... - a.hatson
  • @ a.hatson return cannot "want" or "not want". Show the code that calls this function. - Igor