We should LOL @ this "no government" and "living free" guy's

UFOs, lost socks, discuss whatever you like here.

Moderators: Master_Kale, TNM Team

Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

Anybody care to help me with this one? It's Javascript but it's very similar to unrealscript.

Code: Select all

/*jshint multistr:true */                         //no idea why this preceding comment is there, it was already there in tutorial.

var text = "CyberP CyberP CyberP";
var myName = "CyberP";
var hits= [];

for (i=0;i<text.length;i++){                              //increments through var text.length 
    if (text[i]==="C"){                                      //executes second loop once "C" is found in var text.
        for (j=i;j===i+myName.length;j++){           //This loop I'm not certain. starts at "C", ends at myName.length. Increments by 1
            hits.push(myName);                            //Pushes the result to the array.
        }
    }
}
I need to Find "CyberP" among "CyberP CyberP CyberP".
What should I be pushing to the array, it's not myName, it's i or j, right? But those are not working either.
neuDialect
Thug
Posts: 11
Joined: Thu Jun 18, 2009 7:19 pm

Re: We should LOL @ this "no government" and "living free" g

Post by neuDialect »

Cybernetic pig wrote:Anybody care to help me with this one? It's Javascript but it's very similar to unrealscript.

Code: Select all

/*jshint multistr:true */

var text = "CyberP CyberP CyberP";
var myName = "CyberP";
var hits= [];

for (i=0;i<text.length;i++){ //increments through text.length 
    if (text[i]==="C"){ //executes second loop once "C" is found in var text.
        for (j=i;j===i+myName.length;j++){ //This loop does what?
            hits.push(myName); //Pushes the result to the array.
        }
    }
}
The second for loop & hits.push(), the error/s are in there somewhere.

I need to Find "CyberP" among "CyberP CyberP CyberP".
What should I be pushing to the array, it's not myName, it's i or j, right?
Ugh, javascript breaks my brain. Why the hell do they need two equality comparison operators?
But no, you should be pushing in myName, I'm pretty sure (since i and j are integers. in fact that is true almost always, since i,j,k are traditionally used for loop counters. [sometimes you may also see foo,bar,baz]).
Anyway replace the myName.length with (myName.length - 1) and it should work. But the code is pretty horrible, what exactly are you trying to do?

As to what happens:

Code: Select all

var text = "CyberP CyberP CyberP";                                                       
// you declare a String 'text' with contents "CyberP CyberP CyberP"
var myName = "CyberP";                                                                     
// you declare a String 'myName' with contents "CyberP"
var hits= [];                                                                                    
// you declare an empty array 'hits'

for (i=0;i<text.length;i++){ //increments through text.length                      

// you declare an Integer 'i' with contents "0". Afterwards you compare i to the length of text. Then if it is larger than i you ran the inner lop once. Finally, after you run the loop, you set the integer 'i' to be one larger

    if (text[i]==="C"){ //executes second loop once "C" is found in var text.  

// this is an additional condition on executing the for loop (note that you could move it into the for loop by doing i<text.length AND text[i]==="C"). What happens here is that you read the 'i'th character of text and see if it is "C". the issue is probably that the array is 0 indexed, meaning that it starts at 0
  
      for (j=i;j===i+myName.length;j++){ //This loop does what?       

 // you declare an integer 'j' with contents of 'i'. Afterwards you compare j to the length of myName + i and if it is equal you run the contents of the loop. finally, you increment j by one and repeat the condition
            
             hits.push(myName); //Pushes the result to the array.          

// This puts the myName text into hits
       
        }
    }
}
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

Well, I should be pushing in my name, but not the variable myName but the result of the loop code.
the variable myName is what the code is comparing when looking among var text. it's just there for .length comparison if I am not mistaken.

Please tell me things to get too much more complex than this :cry:
Last edited by Cybernetic pig on Tue May 21, 2013 4:20 pm, edited 1 time in total.
neuDialect
Thug
Posts: 11
Joined: Thu Jun 18, 2009 7:19 pm

Re: We should LOL @ this "no government" and "living free" g

Post by neuDialect »

Cybernetic pig wrote:Well, I should be pushing in my name, but not the variable myName but the result of the preceding code.
the variable myName is what the code is looking for among var text.

Please tell me things to get too much more complex than this :cry:
Again, what are you trying to do? I edited my previous post a bit.
edit: If you want I can help you with coding over skype or something, though I have a hard exam tomorrow and didn't quite study enough for it. PM with your skype name if you want.
Last edited by neuDialect on Tue May 21, 2013 4:25 pm, edited 1 time in total.
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

neuDialect wrote:
Again, what are you trying to do? I edited my previous post a bit.
Tell the computer to find just "CyberP" among "CyberP CyberP CyberP" using the loops and if statement then push what it finds to the empty array.

made edits to my previous posts also.

Here is what the tutorial says to do:
Your second "for" loop
Okay! Last loopy step: add another for loop, this time inside the body of your if statement (between the if's {}s).

This loop will make sure each character of your name gets pushed to the final array. The if statement says: "If we find the first letter of the name, start the second for loop!" This loop says: "I'm going to add characters to the array until I hit the length of the user's name." So if your name is 11 letters long, your loop should add 11 characters to hits if it ever sees the first letter of myName in text.

For your second for loop, keep the following in mind:

First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with

for(var i = 0; // rest of loop setup
your second should be something like

for(var j = i; // rest of loop setup
Second, think hard about when your loop should stop. Check the Hint if you get stuck!

Finally, in the body of your loop, have your program use the .push() method of hits. Just like strings and arrays have a .length method, arrays have a .push() method that adds the thing between parentheses to the end of the array. For example,

newArray = [];
newArray.push('hello');
newArray[0] // equals 'hello'
Last edited by Cybernetic pig on Tue May 21, 2013 4:36 pm, edited 2 times in total.
neuDialect
Thug
Posts: 11
Joined: Thu Jun 18, 2009 7:19 pm

Re: We should LOL @ this "no government" and "living free" g

Post by neuDialect »

Cybernetic pig wrote:
neuDialect wrote:
Again, what are you trying to do? I edited my previous post a bit.
Tell the computer to find "CyberP" among "CyberP CyberP CyberP" using the loops and if statement then push "CyberP" to the empty array.
Well what it does is that it looks for a capital C in text and if it finds it it pushes "CyberP" into 'hits'. Then it probably tries to read a character position that isn't allocated in memory and crashes. And the inner loop is completely irrelevant AFAICS

edit: based on your edit what you want is something like this:

Code: Select all

// inner loop
for(j=0;j<myName.length;j++) {
   hits.push(text[i+j]);                                  // Made a small typo here, it's fixed now
}
i = i + myName.length;
Last edited by neuDialect on Tue May 21, 2013 4:37 pm, edited 4 times in total.
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

neuDialect wrote:Well what it does is that it looks for a capital C in text and if it finds it it pushes "CyberP" into 'hits'.


Yeah, it's not meant to push var myName into hits, that's my fault. It's meant to push the result of the loops or whatever.

And the inner loop is completely irrelevant AFAICS
Probably my error also.
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

neuDialect wrote: edit: based on your edit what you want is something like this:

Code: Select all

// inner loop
for(j=0;j<myName.length;j++) {
   hits.push(text[i+j]);
}
i = i + myName.length;
Thank you, I'll try that.

Edit: Tutorial said "Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

How do I make it stop? it should stop at i<myName.length, which is 6.
Last edited by Cybernetic pig on Tue May 21, 2013 8:28 pm, edited 2 times in total.
neuDialect
Thug
Posts: 11
Joined: Thu Jun 18, 2009 7:19 pm

Re: We should LOL @ this "no government" and "living free" g

Post by neuDialect »

Cybernetic pig wrote:
neuDialect wrote: edit: based on your edit what you want is something like this:

Code: Select all

// inner loop
for(j=0;j<myName.length;j++) {
   hits.push(text[i+j]);
}
i = i + myName.length;
Thank you, I'll try that.
One thing I'd say is that you should probably try some other language than javascript to learn programming. It was actually my first programming language as well, but it's really muddy and such. Sometimes it'll let you get away with something it absolutely shouldn't, and you won't know until you start crashing.

re edit: It stops automatically, that's what the second part of the for loop is for. Really, send me your skype name or something, that'd be way faster.
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

I don't have Skype unfortunately, never used it before.

EDIT: That's it! I didn't write the code you gave me properly. Thank you very much.

This is hard :( I've found the lessons fairly easy up until around this point, and it can only get more complex.
neuDialect
Thug
Posts: 11
Joined: Thu Jun 18, 2009 7:19 pm

Re: We should LOL @ this "no government" and "living free" g

Post by neuDialect »

Cybernetic pig wrote:I don't have Skype unfortunately.

EDIT: That's it! I didn't write the code you gave me properly. Thank you very much.

This is hard :( I've found the lessons fairly easy up until around this point.
It really isn't, once you know how to do this stuff you'll find it pretty easy.

And (somehow) on topic, most of the people who have trouble with basic programming in the first year of computer science (what the article was about) don't really seem cut out for it. Like, if after 9 months of regular programming you don't understand how to do basic things, then you'd probably absolutely hate life once you get to the point when you have to actually program meaningful things; not to mention think about and analyze HOW programming works.
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

neuDialect wrote: edit: If you want I can help you with coding over skype or something, though I have a hard exam tomorrow and didn't quite study enough for it. PM with your skype name if you want.
A mentor? codingGenie worked!? 8)

I'll sign up to Skype then.
neuDialect wrote:
And (somehow) on topic, most of the people who have trouble with basic programming in the first year of computer science (what the article was about) don't really seem cut out for it. Like, if after 9 months of regular programming you don't understand how to do basic things, then you'd probably absolutely hate life once you get to the point when you have to actually program meaningful things; not to mention think about and analyze HOW programming works.
How am I doing so far? I have previous experience with a VPL (visual programming language, which I miss terribly because it's so easy) and fiddling with DX code (very minor stuff, just changing values and adding lines to default props, plus some other stuff), and the above Javascript is what is what I have learnt in probably roughly 8 hours worth of tutorials thanks to codecademy.com.

Does everybody struggle at times?
Last edited by Cybernetic pig on Tue May 21, 2013 5:12 pm, edited 1 time in total.
neuDialect
Thug
Posts: 11
Joined: Thu Jun 18, 2009 7:19 pm

Re: We should LOL @ this "no government" and "living free" g

Post by neuDialect »

Cybernetic pig wrote: A mentor? Coding Genie worked!? 8)

I'll sign up to Skype then.
I'll see what I can do, but I don't promise much. I really do have a hard exam tomorrow morning, and another one the day after that, so I won't have unlimited time or anything. That said, if you go on IRC channels about programming (synirc is a good server) you'll find plenty of people to help you.
Cybernetic pig wrote: How am I doing so far? I have previous experience with a VPL (visual programming language) and fiddling with DX code (very minor stuff, just changing values and adding lines to default props, plus some other stuff), and the above Javascript is what is what I have learnt in probably roughly 8 hours worth of tutorials thanks to codecademy.com.

Does everybody struggle at times?
You're doing fine, I was mostly talking about people who actually go and study computer science at university. If it only took you 8 hours than you're already better than most. Coding's not hard.
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

neuDialect wrote: I'll see what I can do, but I don't promise much. I really do have a hard exam tomorrow morning, and another one the day after that, so I won't have unlimited time or anything. That said, if you go on IRC channels about programming (synirc is a good server) you'll find plenty of people to help you.
Ok. You've been a great help, so thanks. I'll go somewhere dedicated to helping newbies.
You're doing fine, I was mostly talking about people who actually go and study computer science at university. If it only took you 8 hours than you're already better than most. Coding's not hard.
Codecademy.com, cannot recommend it enough to other newbies. Some of the instructions are vague but overall it's very helpful.
Cybernetic pig
Illuminati
Posts: 2284
Joined: Thu Mar 08, 2012 3:21 am

Re: We should LOL @ this "no government" and "living free" g

Post by Cybernetic pig »

Wait, why this?

Code: Select all

i = i + myName.length;
Wouldn't giving i the value of myName.length defeat the purpose of this lesson? I'm confused.

Also, in the if statement, within the parenthesis there is . Why is in square brackets and not:

Code: Select all

if (text (i)==="C"){
code code code;
}
I gave it square brackets but I have no idea why, cannot remember, yet the code still works....
In my lessons so far I have only been taught one use of square brackets: to hold the parameters of an array.
Post Reply