Switch case doesn't work and give 0 values - javascript

I tried this switch and loop, but it only gives me 0 values when I expected it should be incrementing for all display "yes". May I know where goes wrong?
I am new to this javascript.
for (var i=3; i<=62; i++){
var quesNum = i - 2;
if (activeSheet.getRange(row, i).getDisplayValue() == "yes") {
switch (quesNum) {
case 1: case 3: case 5: case 12:
++realistic;
break;
case 2: case 6: case 9: case 14:
++investigative;
break;
case 4: case 8: case 11: case 15:
++artistic;
case 7: case 10: case 13: case 16:
++conventional;
break;
default:

Related

Function with two switch cases will only log one string

Im new to coding and javascript, atm im doing som tests in school.
I have this function with two different switch cases with strings which i want to add together and return. But it only returns one of the strings. If i use switch(card.value) on the first and switch(card.suit) on the second it only returns the first one. But if i take it away on the frist one:
switch(value) and switch(card.suit) it return string from the lower switch-case. Why is that? And how do i get it to return A♥? Here is the code. Sorry for my messy description.
const prettyCard = function (card) {
let suit, value
switch (card.value) {
case 1:
return 'A';
break;
case 10:
return 'T';
break;
case 11:
return 'J';
break;
case 12:
return 'Q';
break;
case 13:
return 'K';
break;
case 2:
return '2';
break;
case 3:
return '3';
break;
case 4:
return '4';
break;
case 5:
return '5';
break;
case 6:
return '6';
break;
case 7:
return '7';
break;
case 8:
return '8';
break;
case 9:
return '9';
break;
}
switch (card.suit) {
case 'HEARTS':
return '♥';
break;
case 'SPADES':
return '♠';
break;
case 'DIAMONDS':
return '♦';
break;
case 'CLUBS':
return '♣';
break;
}
return value + suit
}
console.log(prettyCard({ suit: 'HEARTS', value: 1 }))
In addition to the fix suggested in comments, the code can shrink considerably by looking-up suits and values in an object, rather than a long switch.
const prettyCard = card => {
const values = { 1: 'A', 11: 'J', 12: 'Q', 13: 'K' }
const suits = { 'HEARTS': '♥', 'SPADES': '♠', 'DIAMONDS': '♦', 'CLUBS': '♣' };
const suit = suits[card.suit];
const value = card.value > 1 && card.value < 11 ? `${card.value}` : values[card.value];
return { suit, value };
}
console.log(prettyCard({ value: 12, suit: 'HEARTS' }))

Why can I not view an updated count for a variable with console.log in Javascript?

I currently have this code below, it is supposed to emulate a card counting system, in which different characters increment/decrement the count when passed. The code below runs successfully, but when I attempt console.log(cc(2,3,4,5,6); it returns 1 Bet, when I know the code runs 5 Bet, as those characters should increment the count, yet console.log does not return the accurate count, I assume this is due to scope? But I would like if someone could explain why count isn't accurately returned.
var count = 0;
function cc(card) {
switch(card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1;
break;
case 7:
case 8:
case 9:
count += 0;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= 1;
break;
}
if (count > 0) {
return count + " Bet";
}
else {
return count + " Hold";
}
}
cc(2,3,4,5,6); \\ returns 5 Bet
console.log(cc(2,3,4,5,6)); \\ returns 2 Bet
var count = 0;
const bet = card => {
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1;
break;
case 7:
case 8:
case 9:
count += 0;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count -= 1;
break;
}
};
function cc(...moves) {
moves.forEach(bet);
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
}
console.log(cc(2, 3, 4, 5, 6)); // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)); // returns 10 Bet
Use arguments or splat operator to get all arguments passed and then loop it:
function cc(...cards) {
var count = 0;
var card;
for (var i = 0; i < cards.length; i++) {
card = cards[i];
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1;
break;
case 7:
case 8:
case 9:
count += 0;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= 1;
break;
}
}
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
}
cc(2, 3, 4, 5, 6); // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)); // returns 2 Bet
You have two major problems.
First, your count need to start at zero every time you call cc(). Because you have declared count outside the function, its values is preserved between calls to cc(). By declaring it inside the function, it's initialized to zero before it starts counting the card values.
(Unless you want to continue to the previous count, in which case you should keep it declared outside the function)
Second, your function only accepts one argument. You need to make it accept a list of arguments. This can be done simply with the spread operator .... Then you need to loop through each card value and do the count.
function cc(...cards) { // accept a list of card values as arguments.
let count = 0
for (const card of cards) { // loop through all card values.
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1
break
case 7:
case 8:
case 9:
count += 0
break
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= 1
break
}
}
if (count > 0) {
return `${count} Bet`
}
return `${count} Hold`
}
cc(2, 3, 4, 5, 6) // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)) // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)) // returns 5 Bet

Given an integer in the range 10-40 javascript [duplicate]

This question already has answers here:
JavaScript numbers to Words
(28 answers)
Closed 5 years ago.
Problem: Given an integer in the range 10-40, which determines the number of training assignments on a certain topic. Output a string-description of the specified number of tasks, ensuring the correct agreement of the number with the words "study assignment", for example: 18 - "eighteen study assignments", 23 - "twenty-three study assignments", 31 - "thirty one study assignments".
i wrote something like,
var ten,
set,
unit;
switch(ten){
case 11:
return "eleven";
break;
case 12:
return "twelve";
break;
case 13:
return "thirteen";
break;
case 14:
return "fourteen";
break;
case 15:
return "fifteen";
break;
case 16:
return "sixteen";
break;
case 17:
return "seventeen";
break;
case 18:
return "eighteen";
break;
case 19:
return "nineteen";
break;
}
if (ten<9) {
return "Error number is less than 9";
}
switch(set){
case 10:
return "ten";
break;
case 20:
return "twenty";
break;
case 30:
return "thirty";
break;
case 40:
return "fourty";
break;
}
if(set<9) {
return "Error number is less than 9";
}
switch (unit)
{
case 1:
return "on1";
break;
case 2:
return "two";
break;
case 3:
return "three";
break;
case 4:
return "four";
break;
case 5:
return "five";
break;
case 6:
return "six";
break;
case 7:
return "seven";
break;
case 8:
return "eight";
break;
case 9:
return "nine";
break;
}
Any help will be appreciated!
If you have a limited range of numbers and want an easy solution without any libraries, you could try simple mapping:
var numbers = {
'10': 'ten',
'11': 'eleven',
'12': 'twelve',
'13': 'thirteen'
// ...
};
console.log(numbers['11']); // 'eleven'
Otherwise the the already mentioned JavaScript numbers to Words seems a good solution.

Counting Cards Code Explanation

I am doing the FreeCodeCamp courses and am confused as to why my code doesn't work. It seems to be a messier version of the correct answer. Below is the correct answer and underneath that is mine. I just want to understand.
Here is the correct answer:
var count = 0;
function cc(card) {
{
case 2:
case 3:
case 4:
case 5:
case 6:
return count++;
case 7:
case 8:
case 9:
return count;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
return count--;
}
}
Here is my answer:
var count = 0;
function cc(card) {
// Only change code below this line
switch (card) {
case 2:
return count + 1;
case 3:
return count + 1;
case 4:
return count + 1;
case 5:
return count + 1;
case 6:
return count + 1;
case 7:
return count + 0;
case 8:
return count + 0;
case 9:
return count + 0;
case 10:
return count - 1;
case 'J':
return count - 1;
case 'Q':
return count - 1;
case 'K':
return count - 1;
case 'A':
return count - 1;
}
if (count <= 0){
return count + " hold";
} else {
return count + " bet";
}
Two differences; what you get back and what is modified
count++ and count-- give you back the value of count, then modify count
count + 1 and count - 1 give you back a calculated value based on count and do not modify count
Consider
let x = 1;
x++; // 1
x; // 2
vs
let x = 1;
x + 1; // 2
x; // 1
It is also worth noting that the unary increment and decrement operators can be used prefix, which performs the modification before you get back the value, i.e.
let x = 1;
++x; // 2
x; // 2
Furthermore, please notice that it is impossible to reach your condition if (count <= 0) ... assuming card in [2-9JQKA]|10 as an earlier return will always have been called.
Finally, as I can't see the input I can not be sure on this, however it looks like this code is mixing Number and String. It may be wise to consider choosing just one type so you do not get unexpected behaviour.
Depending on the behaviour you require for A, I would suggest either using Number and mapping {J: 11, Q: 12, K: 13, A: 14}, or doing everything in String.
var count = 0;
function cc(card) {
switch(card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += 1;
break;
case 7:
case 8:
case 9:
count += 0;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= 1;
break;
}
return count + (count > 0 ? " Bet" : " Hold");
}

Compare if a-z or A-Z and give output with switch JavaScript function

I'm learning JavaScript. Very new and know basic. I'm playing with various options of JavaScript.
I'm comparing a-z (lower case) and A-Z (upper case) from user input. and giving an answer base on the input.
Normally i can do this with this long code:
var x = prompt("Enter Your character");
switch (x) {
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
document.write("Lower case");
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
document.write("Upper case");
break;
default:
document.write("It is number");
break;
}
With switch I want to achieve same output but with less code! Something like this:
var x = prompt("Enter Your character");
switch(x) {
case x >= 'a'|| x <= 'z':
document.write("Lower case");
break;
case x >= 'A' || x <= 'Z':
document.write("Upper case");
break;
default:
document.write("It is number");
break;
}
Any Help?
Please I want to do this with only switch function. I know i can do this with if/else function but i want to do this with switch. If its not possible with switch let me know :-)
The switch statement compares the input expression with each case statement using strict comparison.
So use true inside the switch clause and specify expressions that evaluate to true in case clause:
var x = prompt("Enter Your character");
switch (true) {
case x >= "a" && x <= "z":
alert("Lower case");
break;
case x >= "A" && x <= "Z":
alert("Upper case");
break;
default:
alert("Something else");
break;
}
I personally do not recommend this. This is only for learning.
You can try it with something like this.
<script>
var x=prompt("Enter Your character");
if (x.test(/[a-z]/)) {
document.write("Lower case");
} else if (x.test(/[A-Z]/)) {
document.write("Upper case");
} else {
document.write("It is number");
}
</script>
You can see more info here: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
You could do a simple if/else
var x=prompt("Enter Your character");
if(!isNaN(x))
console.log("It is number");
else if(x.toLowerCase()==x)
console.log("Lower case");
else if(x.toUpperCase()==x)
console.log("Upper case"););
Check if the character is in in between other characters:
var x=prompt("Enter Your character");
if (x >= '0' && x <= '9')
alert("number!");
else if(x >= 'a' && x <= 'z')
alert("lowercase!");
else if(x >= 'A' && x <= 'Z')
alert("uppercase!");
else
alert("not a letter!");
The parameter to a case statement is something to perform an equality comparison with, you don't put a comparison there. If you want to do a comparison, use if, not switch. Also, you should be combining the comparisons with &&, not ||
if (x >= 'a' && x <= 'z') {
document.write('Lower case');
} else if (x >= 'A' && x <= 'Z') {
document.write('Upper case');
} else if (x >= '0' && x <= '9') {
documennt.write('Number');
} else {
document.write('Something else');
}
Try also:
var x = prompt("Enter Your character");
var find = x.match(/([a-z]*)([A-Z]*)([0-9]*)/);
var type = ["Lower case","Upper case", "It is number"];
document.write(find ? type[find.lastIndexOf(x)-1] : "Unknown char")

Categories