Hi, I’ve once again got stuck doing the Basic JavaScript exercises. I would really appreciate any help
function caseInSwitch(val) {
let answer = "";
// Only change code below this line
switch(val) {
case 1 :
let answer = "alpha";
break;
case 2 :
let answer = "beta";
break;
case 3 :
let answer = "gamma";
break;
case 4 :
let answer = "delta";
break;
}
// Only change code above this line
return answer;
}
caseInSwitch(1);
Also, I was wondering if you had any advice on how to overcome getting stuck in tutorials by myself. Even though I’m very grateful for resources like this, I’m going to need to learn how to get myself out of situations like this. Any help for these two things would be greatly appreciated!
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.
Do you have a link to the challenge? We don’t have them memorized.
One thing I notice immediately, is that this:
let answer = "";
already declares answer. You don’t need to (shouldn’t) redeclare it (with let) every time you use it.
One of the most important skills a developer can learn is using google. No one can remember all this stuff. At work today, I googled almost a dozen things.
And cut yourself some slack - this is hard stuff. Struggle with it for a while, reread the material, google it… And then ask for help. Seriously, I do that at work sometimes to, “Hey, guys, I’m not seeing how to do this. can I get another set of eyes here? Bounce some ideas off you?” For you, right now, if you don’t have a coding partner, that’t the forum. There is not shame in asking for help.
If that’s the case, I don’t think I fully understand what declaring a variable means. If I don’t redeclare it as something else, how will the variable change for any of the cases?
const - this means the variable will never change its value
let - this means the variable can change its value
var - legacy feature, don’t use this
Javascript normally tries to stop you from re-declaring variables, but the switch has its own scope (given by the {}s), so you actually made a separate version of answer that only existed inside of your switch cases!