Substrings


#1

<I don't understand the substrings? how does that even work its telling me that the first letter begins with 0 and when I count from 0 some words are wrong and some are right???


Replace this line with your code.


#2

Please post a link to the exercise that you are working on.
Also, give us your exact code and the error messages that you see.


#3

Getting the positioning of substring letter positions is tricky! Let's make sure we really have it nailed down.

Remember that each character in a string is numbered starting from 0. So for the word "hello", The letter h is in position 0, the letter e is in position 1, and so on.

Instructions

Using console.log, on three separate lines, print out the substrings for the following strings.

a. "Jan" in "January"
b. "Melbourne is" in "Melbourne is great" (note the space!)
c. "burgers" in "Hamburgers"

and this is what I got when I did this code.

// Use console.log( ) to print out the substrings.
// Here is an example of the 1st to 4th letter of "JavaScript":
// console.log("JavaScript".substring(0,4));
console.log ("January".substring (0,3))
console.log ("Melbourne is great".substring (0,17))
console.log ("Hamburgers".substring (3,9))

Jan
Melbourne is grea
burger

??????

and yet it told me that the first letter of a word is 0 and the second is 1, which I flowed this very well and still end up with some letters short.

can someone explain in detail how this works.

many thanks


#4

Substrings works like that:
As we had in lesson 21, let's take for example the word "Laptop" and find the 4th letter up to 6th(including).

So the word "laptop" has 6 letters.
Lets say we want to find the 3th letter.
To count a substring word, you start by 0 like:
L = 0
a = 1
p = 2
t = 3
o = 4
p = 5

So if you want to find the 4th letter up to the 6th (including) you start from 3 and end with 6 like:

("laptop".substring(3,6))

So as your code says, lets look at Melbourne is great
You need to log into the console Melbourne is so lets see:
M = 0
e =1
l = 2
b = 3
o = 4
u = 5
r = 6
n = 7
e = 8
= 9
i = 10
s = 11

So why (0, 17)? it should be (0,12)

The some to Hamburgers
after you save & submit your code you can see that you actually typed burger instead of burgers so just change (3,9) to (3.10)

I really hope you understand what i wrote here (and ofc, I hope I'm not misleading you or something) but if you still didnt understand, feel more than free to content me and i'll be happy to help you. :slight_smile:

*Remember, if you want to start counting from the 3th letter, you put (2,X)
if you want to count from the 6th letter, you put (5,X)

*If you want to end at the 7th letter, you put (X,8)
if you want to end at the 15th letter, you put (X,16)


#5

3 posts were split to a new topic: Am not sure what I should be doing here


#6

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