Need a little help at JS. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Need a little help at JS.

Let's see the code I need help at.... var Jake="Gybjo"; var Joke=Jake.lenght-1; alert (Joke); Output = 4 What I want is to print the string Gybj when variable Joke is printed by alert. How can I accomplish this?

5th Oct 2017, 3:23 PM
Isair Calhawk
Isair Calhawk - avatar
11 Answers
+ 7
var Jake = "Gybjo"; var Joke = Jake.substring(0, 4); https://code.sololearn.com/WaMjvuVF6fu0/#js
5th Oct 2017, 3:34 PM
AgentSmith
+ 11
var Jake = "Gybjo", Res = ""; for(var i = 0; i < Jake.length-1; i++) { Res += Jake[i]; } alert(Res); // EDIT -- Actually, @Netkos Ent has a better and simple way of doing it, of course.
5th Oct 2017, 3:31 PM
Dev
Dev - avatar
+ 7
@Forge Ice : I know its not for me, but you should use var e=jai.substring(0, x); instead of 'ja' so as to get the inner value of your <div>
6th Oct 2017, 10:19 AM
Dev
Dev - avatar
+ 4
Thanks guys.
5th Oct 2017, 3:34 PM
Isair Calhawk
Isair Calhawk - avatar
+ 4
@Forge Ice You're more than welcome bro. Btw, although the way I posted is the more efficient way, take a look at what Dev posted to you. It's a great example of how you can utilize loops to iterate through your string array, which is very useful for many other situations, so take note of what he did there and ask questions if you need to. So many ways of going about the same problems, and each way has its more useful scenarios, so obtain the knowledge from both methods. Best of luck!
5th Oct 2017, 3:38 PM
AgentSmith
+ 2
@Netkos Ent Got it!
5th Oct 2017, 3:41 PM
Isair Calhawk
Isair Calhawk - avatar
5th Oct 2017, 4:15 PM
Antony O. Onyango
Antony O. Onyango - avatar
+ 1
@Netkos niiice!! ive got to know what that substring () does... thats very few lines.. Im still new to JS :'(
5th Oct 2017, 4:17 PM
Antony O. Onyango
Antony O. Onyango - avatar
+ 1
@Netkos Ent But substring doesn't work with DOM :( https://code.sololearn.com/WrXqyMHDu06Q/?ref=app
6th Oct 2017, 9:38 AM
Isair Calhawk
Isair Calhawk - avatar
+ 1
Thanks dev :)
6th Oct 2017, 10:48 AM
Isair Calhawk
Isair Calhawk - avatar
0
or u can just tell me here :)
5th Oct 2017, 4:18 PM
Antony O. Onyango
Antony O. Onyango - avatar