My code for this challenge follows. Im not sure where I
m going wrong.
///
var count = 0;
var lowCards[2,3,4,5,6];
var medCards[7,8,9];
var highCards[10, 'J', 'Q', 'K', 'A'];
var allCards [lowCards, medCards, highCards];
function compareCard(card){
for(var i = 0; i < allCards.length; i++){ // goes through allCards
for(var j = 0; j<allCards[i].length; j++){// goes through each section of allCards
for(var lc = 0; lc < allCards[i].length; lc++){ //checks against lowCards
if (card == lowCards[lc]){
count += 1;
}
}
for(var mc = 0; mc < allCards[i].length; mc++){ //checks against midCards
if (card == midCards[mc]){
count = count;
}
}
for(var hc = 0; hc < allCards[i].length; hc++){ //checks against highCards
if (card == highCards[mc]){
count -=1;
}
}
}
}
if(count > 0){
return count + " Bet";
}
else{
return count + " Hold";
}
// Only change code above this line
}
function cc(card) {
// Only change code below this line
compareCard(card);
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
///
Iâve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The âpreformatted textâ tool in the editor (</>
) will also add backticks around text.
I think youâre going about this completely wrong. There is no need for looping.
I think either switch
makes the most sense here. This is something I wrote for someone else working on this:
function isAVowel(letter) {
let answer
switch (letter.toLowerCase()) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
answer = "yes"
break
case 'y':
answer = "sometimes"
break
default:
answer = "no"
}
return answer
}
You need to do something similar, listing out the different cards and what action should be taken. I would suggest grouping cards 2-6 in one where you add 1 to count, then 10-âAâ where you subtract one, and then just default to do nothing (7-9).
The action you need to take is incrementing or decrementing count.
Let us know if this clears this up.
Thanks. ya I get what youâre doing, I was trying to do it with arrays and loops so I could make one that I could use for different size sets and basically have it work for whatever the array was storing. Like a more general approach that would work for any size with minimal adjustment to the code. Like go through this multidimensional array and check each index of each array with the input.
Well, if thatâs the case, one approach might be this:
var count = 0;
var lowCards = [2, 3, 4, 5, 6];
var highCards = [10, "J", "Q", "K", "A"];
function cc(card) {
if (lowCards.indexOf(card)!==-1)
count++;
else if (highCards.indexOf(card)!==-1)
count--;
if (count > 0)
return count + " Bet";
else
return count + " Hold";
}
Abstracting it a little further, this would work:
var count = 0;
var lowCards = {
list: [2, 3, 4, 5, 6],
adjust: 1
};
var midCards = {
list: [7, 8, 9],
adjust: 0
};
var highCards = {
list: [10, "J", "Q", "K", "A"],
adjust: -1
};
var allCards = [lowCards, midCards, highCards];
function cc(card) {
for (let i = 0; i < allCards.length; i++) {
if (allCards[i].list.indexOf(card) !== -1) {
count += allCards[i].adjust;
break;
}
}
if (count > 0)
return count + " Bet";
else
return count + " Hold"
}
The key is to get rid of those inner loops. Why not use an array prototype method like indexOf?
Very clean solutions thank you. I like the usage of objects in the second more abstracted way. And youâre right. Index of is a much better approach to this. Thank you!
Yeah, I get your desire to understand more advanced design patterns, but in this case I think the switch
solution is the ârightâ one - it is simple and easy to understand and you just learned it a few lessons ago - I think that is what they were expecting you to use.
But I admire the drive to think about more complex situations.
You`re very right, save the complex solutions for complex problems. There will be the time and place for that thanks!
It reminds me of a friend of mine. He got hired as a junior dev. They wanted him to write software that would automatically call him and four other engineers if the server went down and then keep calling every five minutes until some one logged in to restart it.
Sounds simple enough. But he wanted to get clever. So he wrote it with a recursive loop. But he screwed up, so after 5 minutes, it called everyone twice. And then five minutes later it called them all three times. You see where this is going. This was at 3am. Unfortunately, the password had recently been changed and he was the only one with it and it was on his laptop that he left at work. Nicht gut.
In the end, the phone service had made thousands and thousands of calls to these 5 guys. He was not very popular the next morning.
Moral: Donât make things more complicated than you need to.
1 Like