25/28 Javascript: more variable practise


#1

// Use console.log to print out the length of the variable myCountry.
console.log("myCountry");
var myCountry = "Canada";
myCounrty.substring(0,6)

// Use console.log to print out the first three letters of myCountry.
console.log("myCountry");
var myCountry = "Canada";
myCountry.substring(0,3)

What am i doing wrong? i understood the html and css with no major issues, but this is just confusing, there's not really any clear hints on how this should look no clear reference points to work from, starting from the console log() im finding this course extremely hard to understand. i winged 24/28, but i want to understand the code im wrtting so i know how to apply it later on


Please help with 25.More Variable Practice!
#2

// Declare a variable on line 3 called
// myCountry and give it a string value.
var myCountry="indonesia";

// Use console.log to print out the length of the variable myCountry.
console.log("indonesia".length);

// Use console.log to print out the first three letters of myCountry.
console.log("indonesia".substring(0,3));

IS what it should look like


#3

What var name; does is reserving some space in the memory to store information. Meaning you can think that somewhere is a place idk 0x34fe78 where you can store information. And because nobody want to remember that original name 0x34fe78 you can set up an alias here name. Now you can store information in this space by using name = value and you can retrieve the information by using name just like you'd have used value.

So in terms of the exercise:

var myCountry = "Canada";

this creates you a space in the memory where a value of "Canada" is stored. When you'd use

myCountry = myCountry.substring(0,3);

what happens is that you fetch the value stored in myCountry which is "Canada" and use the substring functionality on it which is the same as "Canada".substring(0,3). So you end up with "Can" which is then stored at the place referrenced by myCountry. So from now on myCountry will mean "Can".

Hope this makes it a little clearer.


#4

i got to the same spot and put in console.log(myCountry.substring(0, 2)); because its asking for the first and third letter. so should'nt it go like 0, 1, 2?

Any explanation would help, thanks !


#5

It goes something like this...

0 1 2 3
C A N

If you want the third letter, you have to consider the 3 to be the end of the one covering the N. i.e. The number to the right of the letter is what covers it.. 2 covers A. 1 covers C. This actually confused me to at first.


#6

3 posts were split to a new topic: 25/28 length not printed although it is