ES6 lesson 1 var and let, what's wrong here?

Hello, this is the first time I’ve posted a question here, but I’m stumped. It seems simple enough. this is my code:

function catTalk() {
  "use strict";
  let catName = "Oliver";
  let quote = catName + " says  Meow!";
  return quote;
}
console.log(catTalk());

The log does give “Oliver says Meow!” but when I click ‘Run the test’ I get this:

// running tests
catName should be "Oliver"
quote should be "Oliver says Meow!"
// tests completed //
 console output Oliver says Meow!

I thought something to do with scope i.e they disappear outside of the function, though we’re not told that in the lesson. So I created them outside the function using let, that didn’t work either. Don’t really want to get stuck on the first lesson! Any help would be appreciated . Thanks.

Fixed now. Scope problem. Fix was rather stupid but it got the result. Thanks for your help.

is this a challenge? please give the link of the challenge


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.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums

You don’t need to make it output to the console. Restart the challenge and only change the var keywords to let keywords and run the tests. It should be able to work then.

Thanks for the reply. The trouble is to do with scope, what you suggest is what I did at first, the console stuff is so I could see what was going on. let in a function loses its value outside the function, I got it to work by declaring two other global variables and setting them inside the function to the values of the let variables, a rather pointless exercise! They only tell you about scope in the next lesson. Thanks again.

1 Like

Yes it’s lesson 1 from ES6 see link bellow:

this is the starting code:

var catName;
var quote;
function catTalk() {
  "use strict";

  catName = "Oliver";
  quote = catName + " says Meow!";

}
catTalk();

the only thing you need to do is change from var to let

I think this lesson is valuable though. My first experience also let me run into a similar problem. Which led me to Google let onto its own.
Which led me to this: https://www.javascripttutorial.net/es6/difference-between-var-and-let/
Freecodecamp doesn’t always explain everything because they want you to ask and they want you to get your information from several sources. And I think that is what makes some lessons very valuable :3
Just a kitty’s opinion though.

Well, I never thought of that, but it kind of makes sense. It’s trying to teach you how to do research. How to find answers correctly through Google, and not just randomly typing in questions.