Note that there are some explanatory texts on larger screens.

plurals
  1. POAlert with cookies, calling a variable from inside another function?
    primarykey
    data
    text
    <p>I'm trying to create an alert in JavaScript using cookies that both greets the user by name and tells them how many times they've won a specific game on the website. I have the user greeting working fine, but I can't seem to get the counter to work correctly. It involves calling variables that are altered by a previous function, and I think that's where my problem is, but I'm not sure how to fix that. (The variables are called 'cardOnePoints' and 'cardTwoPoints'.) Can anyone help me with that or tell me if there's anything else I'm doing wrong?</p> <pre><code>function getCookieOne(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i&lt;ARRcookies.length;i++) { x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if(x==c_name) { return unescape(y); } } } function setCookieOne(c_name,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; } var winnerCount; var getCount = getCookieOne('getCount'); function winnerCount() { if (cardOnePoints + cardTwoPoints === 21) { winnerCount = getCount + 1 } else { winnerCount = getCount }; } function countWinner() { return winnerCount } function checkCookie() { var username=getCookieOne("username"); var gamesWonCount=getCookieOne("gamesWonCount"); function winnerCounter() { var gamesWin if (cardOnePoints + cardTwoPoints === 21) { gamesWin=++winnerCount; } else { gamesWin=winnerCount }; return gamesWin } if (username!=null &amp;&amp; username!="") { alert("Sup " + username+ "!!! Woah man, you've won " + countWinner() + " game(s)!!!"); } else { username=prompt("Please enter your name:",""); if (username!=null &amp;&amp; username!="") { setCookieOne("username",username,365); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload