My problem is not with the code here. This code works properly without any errors. But the problem I have is with the function call here.
As far as I know, when calling such a function, first we need to assign it to a variable and then call that variable through console.log(). For example console.log(cc(2)). But without that, the function has been called several times here. How does the output change when the values of the function called without being assigned to that variable are changed? So all the function callings are executed here?
let count = 0;
function cc(card) {
// Only change code below this line
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;
}
var holdbet = "Hold";
if (count > 0) {
holdbet = "Bet";
}
return count + " " + "holdbet";
// Only change code above this line
}
cc(2);
cc("K");
cc(10);
cc("K");
cc("A");
console.log(cc(2));
Your function cc
modifies the value of a global variable count
depending on the value of the argument card
passed to it, and returns a string containing the updated value of count
concatenated with " holdbet" (As an aside, I think return count + " " + "holdbet";
was meant to be return count + " " + holdbet;
to replace holdbet
with either "hold" or "bet").
You then make sequential calls to the cc
function, passing varying card
arguments. Each call will modify the value of count
and so a later call with the same argument (2 as the argument in your case) would not necessarily result in the same value of count
.
In order to record a return value from cc
you must assign it to a variable, which can be done simultaneously with the call:
let result = cc(2); // cc is called, count changes, result stores the value;
cc("K"); // cc is called, count changes, result is unchanged
cc(10); // cc is called, count changes, result is unchanged
cc("K"); // cc is called, count changes, result is unchanged
cc("A"); // cc is called, count changes, result is unchanged
console.log(cc(2)); // cc is called, count changes, result is unchanged
console.log(result); // value of count at the first call to cc is logged, cc is not called again, count does not change.
in the above example result
does not become a function, it becomes a string, the return value of the call to cc
.
There may be some confusion caused by the practice of assigning a function to a variable name:
let result = cc; // no call to cc as no (), instead result becomes a function;
console.log(result(2)); // makes a call to cc;