What Am I doing wrong here? (question 17, Jabascript


#1

if ("thisword".length =10) {
console.log=("You got a true");
}
else {
console.log="You got a false";
}


#2

Hey!

First of all, next time you post your code in here I highly suggest that you use the three backticks (`) to wrap your code as that makes it much easier for everyone else to understand and read it properly.

Now onto your question,
There are some syntactic mistakes on your code as well as mistakes regarding what you are asked to do, let's tackle them one by one.

This is your code:

if("thisword".length = 10) {
    console.log = ("You got a true");
} else {
    console.log = "You got a false";
}

This was the initial code:

if (10 = 10); {
    console.log("You got a true!");
} else {
    console.log("You got a false!");
}

As I've seen your successfuly corrected the semicolon from the initial code I wont concetrate on that.

There's a syntax error on the first line of your code, it is the equal sign (=) whenever you use that sign in JavaScript keep in mind that it is used to "set" or "assign" values rather than evaluate or compare and there shouldn't be any assignment operators inside the if conditional, therefore it is wrong. For instance lets say x = 5. The = operator doesn't "check" if the x variable is equal to 5, rather it assigns the value 5 to the variable x). Whenever you want to check if two values are equal I suggest that you use the === operator. For instance 10 === 10 evaluates to true. To conclude "=" is the assignment operator while "===" is the Identity / strict equality operator.

Given what we learnt above, we can tweak the first line of the code accorndingly:
Your code:
if("thisword".length === 10) {

Initial code (corrected, without the semicolon):
if (10 === 10) {

Now while the syntax of your first line is technically correct, in our case it's wrong because it evaluates to false and to pass the exercise your if conditional must evaluate to true. So why it evaluates to false you may ask, lets attempt to evaluate the conditional ourselves:
"thisword".length === 10
First of all the .length property it is used to return the length of a string or in other words, the number of letters of a text (string). In our case "thisword".length returns 8 because the string "thisword" has 8 letters in it. Therefore we can rephrase the conditional as 8 === 10 and because 8 does not equals 10 the conditional evaluates to false. The string was unnececery in the first place so lets just replace it with 10 so the the conditional evaluates to true.

Your first line of code corrected:
if(10 === 10) {

Moving on to the code block of your if statement:
console.log = ("You got a true");

The only thing wrong with that line of code is the equal sign again as it goes against the JavaScript function syntax (function(argument)) note that console.log is a function therefore the correct syntax will be console.log("You gota a true!").

Moving on to the rest of your code, there's again some messy syntax on your fourth line of code (console.log = "You got a false";). I'll let you figure that out as giving you the asnwer every time wont help you learn JavaScript!

One last tip I can give you to reduce clutter is that instead of having 10 === 10 you can use the Boolean value true this way you increase the readability of your code while also removing unnececery calculations that the computer will have to make. After that tweak your first line of code will looke something like this:
if(true) {
see how much better it looks?

There doesn't seem to be anything else wrong with your code, I hope I helped you understand your mistakes on your code, if you need help again or you didn't understand something from my answer please reply here and I'll do my best to help you!


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.