Javascript 19


#1

hello i am struggling on number 19 for Getting Started With Programming which is lesson 1 with java script. here is my code. please let me know how to correct it.

("Jon".length*13 / (2+1) ===12 )
{if
console.log("The answer makes sense!");
}
{else
console.log("The awnser is retarted!");
}


#2

It won't ever print the first result because 39 / 3 is 13 not 12 that's your first problem.

} else {

Make sure the instructions aren't asking for specific strings for answers.


#3

Why did you quote "Jon"?
You can't call a method for an string, "Jon" is a string because is quoted. You can call a method like "length()" only for objects, actually send a message for method "length". First of all you have to create an object that store a variable string type, and then you can call method "length" for object.

myVar = "Jon"
(myVar.length*13 / (2+1) ===12 ){...}


#4

@echosomething

Actually he could if he moved the if to before the start if that

which is where it should be and I missed it earlier.

if("Jon".length * 13 / (2+1) === 12) {

is fine and will work, if he adds in the changes I stated in my previous post.

I just tested this in jsbin and changing it from === 12 to ===13 without declaring "Jon" as a variable before still returns the correct answer of the first console.log statement. Making it 12 will return the 2nd console.log statement because 39 / 3 is not equal to 12

edit:
just read it's not really best practice to use the method I stated. Also it would be better for you to write your own loop rather than having almost the same as the last 25 people to post about this exercise. even something like if(24 > 6) {


#5

As far as I know this is some default condition where you just need to enter the correct result after the ===.

@echosomething
http://adripofjavascript.com/blog/drips/javascripts-primitive-wrapper-objects.html


#6

@haxor789

Yes i understand now, actually I wrote the right way, but I have no idea how can i create an object in JS.

var myVar = new String('Jon')
if("myVar.length * 13 / (2+1) === 12) { ...};


myVar = "Jon"

is available for python


#7

As the link implies javaScript will automatically create a temporary object when using a method on a primitive data type so there is no need for you to create an object yourself. Objects are covered later in this track as well so but for now using "Jon".length is perfectly fine. What do you mean by

myVar = "Jon"

this would work in JS as well, the problem is that in python everything is local by default unless stated differently whereas in JS everything is global so better use var before the first use of a variable.


#8

guys I ried your method but i still dont full understand
Here is my method
"myVar".length=2
if ("myVar".length*13 / (2+1) ===13)
if{
(console.log("The answer makes sense!");
};
{else
(console.log("The awnser is ■■■■■■■■!");
}


#9

Lets analyze and Evaluate the Given codes.

"Jon".length * 2 / (2+1) === ????

  1. Jon.length: meaning it will count the letters on the word Jon. It is 3 letters. ( 3 * 2 / (2+1) === )
  2. So number of letters which is 3 * 2 = 6. ( 6 / (2+1) === )
  3. 2 + 1 =3 . ( 6 / 3 === )
  4. 6 / 3 = 2 ( 2 === 2 )

the result is

"Jon".length * 2 / (2+1) === 2

And you should write your code like this:

if("Jon".length * 2 / (2+1) === 2 ){
    console.log("The answer makes sense!");
    } 
else {
    console.log("Error Error Error");
    }

not like this

We dont put console.log("statement here") inside the parenthesis in javascript.
And the position of your curly braces are not on the right place. Here's the right syntax for if else statement, for your reference:

  if(condition here){
      console.log("statement here");
    }
else{
      console.log("another statement here");
    }

Remember that curly braces provides scope-limit
Hope this helps.


#10

A post was split to a new topic: Why is that wrong?


#11