26 Changing Variable values


#1

So....Im having a little trouble on the variable changes. This is what I have;

// On line 2, declare a variable myName and give it your name.
var myName = "John";
// On line 4, use console.log to print out the myName variable.
console.log(myName);
// On line 7, change the value of myName to be just the first 2
// letters of your name.
myName = myName.substring(0,1);
// On line 9, use console.log to print out the myName variable.
console.log(myName);


#2

It is not expecting the substring method.


#3

What is it looking for?


#4

Change the value of myName to be just the first 2 letters of your name.


#5

Or try .substring(0,2). For string.substring(x,y), x is is included but y is excluded from the range so it is like saying from 0 to < 2 .This is convenient when it comes to slicing strings. Counting through the string like 0, 1, 2 means that the first two letters in the name are actually indexed 0 and 1.


#6

Tried all the above. None of it seems to be working


#7

Please can you put your code up?


#8

// On line 2, declare a variable myName and give it your name.
var myName = "John";
// On line 4, use console.log to print out the myName variable.
console.log(myName);
// On line 7, change the value of myName to be just the first 2
// letters of your name.
myName = myName.substring(0,1);
// On line 9, use console.log to print out the myName variable.
console.log(myName);


#9

Just went back to the original code


#10

Your myName.substring() does not tell JavaScript to return the first and second letters.


#11

How should that be put in? I thought that the substring would make that happen


#12

I edited an earlier comment. Look back at what I wrote before in my third comment. That type of string indexing is typical in programming. Please let me know if you have a question.


#13

3 posts were split to a new topic: 26. It keeps saying to me that i did not declare my variable


#14

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