Write a JavaScript function to compute and return a person’s score in the game o
ID: 3703687 • Letter: W
Question
Write a JavaScript function to compute and return a person’s score in the game of 21. Your function must have this header
The parameter hand is an array that contains integers between 1 and 10, inclusive. The length of the hand array will vary. Your function must add all the integers in the array. If the sum is greater than 21, your function must return the string "bust", otherwise your function must return the sum. For example, if your function were called like this:
your function would return the number 17. If your function were called like this:
your function would return the string "bust".
Explanation / Answer
<!DOCTYPE html>
<html>
<body>
<p>Sum of numbers in array: <span id="demo"></span></p>
<script>
var cards = [8, 3, 6];
function twentyOne(cards) {
var total = 0;
for(var i=0;i<cards.length;i++)
{
total += cards[i];
}
if(total>21)
return "bust";
else
return total;
}
var scores=twentyOne(cards);
document.getElementById("demo").innerHTML = twentyOne(cards);
</script>
</body>
</html>
sum of numbers in array:17
sum of numbers in array:bust
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.