Hi, I had no trouble in executing a simple hello world file in VSC but I couldn’t find in the web a way to execute more complicated files there. To be sure that the problem wasn’t the code I used the solution of the card counting challenge, I added console.log(cards);
at the end and then chose the “Run without debugging” in the Run menu.
The results are these below. Should I use some value or extra code in the console.log line?
Thank you
On line 18 you’re trying to console.log variable cards
. Where it’s defined?
You’re right. Should I console.log count instead?
if you want to debug your code, you really need to log existing variables
when you post your code for help, please post your actual code, not screenshots
code issues:
line 5: to make a character class, you do not need to separate characters with commas, doing that you are also adding the comma in the character class
line 8: you may want to review the match
method syntax and what it returns (or maybe use the test
method)
I’m sorry about the screenshot.
But it was the solution taken from the Hint section of that challenge!
Here’s the code as taken from VSC
var count = 0;
function cc(card) {
// Only change code below this line
var regex = /[J,Q,K,A]/;
if (card > 1 && card < 7){
count++;
} else if (card === 10 || String(card).match(regex)) {
count--;
}
if (count > 0) return count + " Bet";
return count + " Hold";
}
// Only change code above this line
cc(2); cc(3); cc(7); cc('K'); cc('A');
console.log(cards);
ok, this makes sense, to make also a possible number into a string and avoid the card.match is not a function
error
the character class thing stands
[from regex101]
OK so the code checks, what line should I use to execute the file then?
it seems it’s executing fine
to see the output you will need to put a console.log around the function call
or inside the function, you store your output in a variable, print that variable, and then return it
Ah, OK, thank you very much.
Didn’t work.
Tried to print the output but it underlined it in red.
var count = 0;
function cc(card) {
// Only change code below this line
var regex = /[J,Q,K,A]/;
if (card > 1 && card < 7){
count++;
} else if (card === 10 || String(card).match(regex)) {
count--;
}
if (count > 0) return count + " Bet";
return count + " Hold";
var cardsResult = cc(2); cc(3); cc(7); cc('K'); cc('A');
return cardsResult;
}
// Only change code above this line
cc(2); cc(3); cc(7); cc('K'); cc('A');
Or this:
var count = 0;
console.log(function cc(card) {;
// Only change code below this line
var regex = /[J,Q,K,A]/;
if (card > 1 && card < 7){
count++;
} else if (card === 10 || String(card).match(regex)) {
count--;
}
if (count > 0) return count + " Bet";
return count + " Hold";
var cardsResult = cc(2); cc(3); cc(7); cc('K'); cc('A');
return cardsResult;
})
// Only change code above this line
cc(2); cc(3); cc(7); cc('K'); cc('A');
Pay attention to the closing parentheses of the console.log enclosing the function.
do not put a function definition imside console.log…
to print the function call would mean to do this: console.log(cc(4))
also, do not call the function inside itself, you are just going to get a stack overflow error, as that is recursion and the function just get calling itself over and over again without something to stop it
to print the function output inside the function would mean to do something like
var result = ...;
console.log(resul);
return result;
instead of returning directly what should be the function output, you store it in a variable, then print it, then return it
@ILM It worked. I called the function as you did on line 2 with, for instance, a card of 7 and returned “0 Hold”, at last results.
So now to understand it.
I’ve been busy with other things.