javascript - Why would I get different results from a function due to the position of number 1 in an array? -
i have few problems stopping me proceeding on blackjack 21 game. first when open site in browser acecard function invoked supposed called if deal 1 (an ace card). below have written problems noticed acecard function.
when number 1 in userarray need call acecard(); in console. or @ least haven't got call automatically.
1.when number 1 in position 0 in array javascript don't recognize 1.
2.when number 1 in position 1 in array acecard function work , change value 1 11
3.when number 1 in position 2 in array see's value 1 in array, won't change value 1 11. if array position 0 = 1 won't see position 2 in array.
any great, have no idea why works differently depending number 1 positioned in userarray.
var userarray = []; var computerarray = []; var = userarray.indexof(1); function random_number() { var randnum = math.floor(math.random() * 10 + 1); return randnum; } document.getelementbyid('dealbutton').onclick = function() { userarray.push(random_number(), random_number()); computerdeal(); computerdeal(); calctotal(userarray); showme(calccomputertotal(userarray)); //cardmaker (); //cardmaker (); //inputmycardvalue (); //inputmycardvalue (); cardmaker(random_number()); cardmaker(random_number()); //inputmycardvaluecomputer (); //inputmycardvaluecomputer (); return userarray; } document.getelementbyid('hitbutton').onclick = function() { userarray.push(random_number()); //computerhit(); calctotal(userarray); showme(calccomputertotal(userarray)); cardmaker(random_number()); //inputmycardvalue (); //cardmakercomputer (); //inputmycardvaluecomputer (); return userarray; } if (userarray.indexof(1)) { acecard(); } function acecard() { if (userarray.indexof(1)) { var numberdesired = parseint(prompt('you got ace card! want equal 1 or 11?')); if (numberdesired === 11) { var = userarray.indexof(1); userarray[a] = 11; inputmycardvalue(); calctotal(userarray); }; }; } function stay() { //have computer try closest 21 posible } /*function cardmaker (number) { var id = 'card' + ($('#cardtable').children().length + 1); $("#cardtable").append('<div id="' + id + '" class="cardlook">' + number + '</div>'); }*/ function cardmakercomputer() { var id = 'cardcomputer' + ($('#computercardvalues').children().length + 1); $("#cardtable").append('<div id="' + id + '" class="cardlook"></div>'); } function inputmycardvalue() { card1.innerhtml = userarray[0]; card2.innerhtml = userarray[1]; card3.innerhtml = userarray[2]; card4.innerhtml = userarray[3]; card5.innerhtml = userarray[4]; card6.innerhtml = userarray[5]; } function cardmaker() { var id = 'card' + ($('#cardtable').children().length + 1); $("#cardtable").append('<div id="' + id + '" class="cardlook">' + userarray[$('#cardtable').children().length] + '</div>'); } inputmycardvalue() inputmycardvalue() function inputmycardvaluecomputer() { card10.innerhtml = computerarray[0]; card20.innerhtml = computerarray[1]; card30.innerhtml = computerarray[2]; card40.innerhtml = computerarray[3]; card50.innerhtml = computerarray[4]; card60.innerhtml = computerarray[5]; acecard(); } function calctotal(userarray) { var total = 0; (var = 0; < userarray.length; i++) { total += userarray[i]; } } function calccomputertotal(computerarray) { var total = 0; (var = 0; < computerarray.length; i++) { total += computerarray[i]; } return total; } function showme(val) { var total = calccomputertotal(userarray); total = document.getelementbyid('out2'); parent = total; parent.innerhtml = val; } //cumputer logic function computerdeal() { computerarray.push(random_number(), random_number()); } /*function computerhit () { if (calccomputertotal(computerarray) <= 17) { computerarray.push(random_number()); } }*/ //when 1 in position 0 in array javascript don't reconise 1. //when 1 in position 1 in array acecard function work , change value 1 11 //when 1 in position 2 in array see's value 1 in array, won't change value 1 11. if array position 0 = 1 won't see position 2 in array.
body { background-color: lightgrey; } #maincontainer { margin: 0 auto; padding: 15px; } #cardtable { height: 240px; width: 95%; background-color: green; border-radius: 5px; margin: 15px 0 15px 0; } #computercardvalues { height: 240px; width: 95%; background-color: green; border-radius: 5px; margin: 15px 0 15px 0; } #out2 { margin-bottom: 5px; } .cardlook { border: 1px solid black; width: 120px; height: 190px; border-radius: 5px; float: left; margin: 20px; padding: 5px; background-color: #fff; } #card1, #card2, #card3, #card4, #card5 { transform: rotate(180deg); transform: rotate(0deg); } #cardcomputer10, #cardcomputer20, #cardcomputer30, #cardcomputer40, #cardcomputer50 { transform: rotate(180deg); transform: rotate(0deg); }
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> <div id="maincontainer"> <div id="wrapper"> <div id="out2">my total</div> <button id="dealbutton">deal</button> <button id="hitbutton">hit</button> <button id="staybutton">stay</button> <div id="cardtable"></div> </div> <div id="computerwrapper"> <div id="computertotal">computer total</div> <div id="computercardvalues"></div> </div> </div>
heading
you're not using indexof
correctly test whether element in array. returns index if element in array, or -1
if it's not in array. can't use boolean, because -1
truthy -- falsey value returns when element in first array element, because returns index 0
.
so if (userarray.indexof(1))
should if (userarray.indexof(1) != -1)
.
there's no point in putting
if (userarray.indexof(1)) { acecard(); }
in top-level of script. nothing gets put userarray
until user starts dealing cards. should test in code runs when dealing card.
Comments
Post a Comment