I have been recently doing the 1st course of Javascript certificate and I encountered a section where we have to count the value of cards assigned to them.
I managed to the point where we have to assign the count -
let count = 0;
function cc(card) {
// Only change code below this line
if (card >= 2 && card < 7) {
count += 1;
}
else if (card >= 7 && card < 10) {
count = count + 0;
}
else if (card === 10 || card == "J" || card == "Q"
|| card == "K" || card == "A") {
count = count - 1;
}
However, I couldnât solve the next part, but I found the following answer for it -
if (count > 0) {
return String(count) + " " + "Bet";
}
else {
return String(count) + " " + "Hold";
}
// Only change code above this line
}
I donât understand how we have nested âifâ in the âelse ifâ?. Semantically, I donât understand why itâs been done.
I know, the explanation is vague, but I really need help to understand why I am finding the function from that point off? It might not be actually. But it looks so unfamiliar.
Iâve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the âpreformatted textâ tool in the editor (</>) to add backticks around text.
Normally, it goes if, else if âŚ, else. But in this code the we have added if after else if. I get it why we have done that. To return the total count. But, the syntax logic is not something I have seen before.
Like @ilenia said, you have two separate problems you are solving. You can think of if, else if, else as a chain of questions that you are asking, and that chain can be as long as you need to solve your problem. In the one chain of questions you are looking at the card and asking what value is it. If its not in the first if statement it will go on to the if else statement, if its not that then the next if else. If one of those conditions is met you stop going down the chain and move on. You then have another chain of logic with the if then else concerning the count value. Is count > 0? else do that. It wouldnât really make sense to have these two things in the same chain, because it would break the chain when you check the value of card, so you make it another if/else chain. You could check it and change it in the first if/else chain, but its better to have a separate flow of logic to follow for a different problem to solve.
Okay. This one looks that itâs the end of one logic properly. So that means in one function we can include multiple logics and they can refer to each other. Is there anything I can read on just to get more comfortable ? When I was writing the function cc It didnât click me to write one more logic within a function instinctively. I felt so blanked at that point.
Thanks so much for explaining. I really appreciate it
True. Also, I think I am more familiar to seeing one logic per function so this one thatâs this one seemed a bit off in the moment. You both are right, the more exposure and practice I will get , the more familiarity and comfort will develop.