Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 505 506 [507] 508 509 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 815059 times)

Sappho

  • Bay Watcher
  • AKA Aira; Legendary Female Gamer
    • View Profile
    • Aira Plays Games
Re: if self.isCoder(): post() #Programming Thread
« Reply #7590 on: July 26, 2015, 01:58:40 am »

--Awesome Answer--

Thanks so much! That all makes much more sense now. Several new concepts have finally clicked into place. Are you a teacher? If not, consider becoming one, because you're good at breaking stuff down and explaining it.

So is there any way to take an array and convert its contents into a string? IE to take [N, a, m, e] and turn it into "Name"? If not, then I think I'm wasting my time trying to do what I was trying to do. The entire basis of this "project" on Code Academy is kind of stupid. Literally all it does is search a long string for a single letter, then push a certain number of letters after it into an array, then print the contents of the array. They call it "searching for your name" but that's not even close to what it does, and in any case, why would anyone ever want to search for a word and have its letters individually pushed into an array? If they gave me this task *without* the explicit instructions to do it this way, I would have done it completely differently.

I guess they're just trying to get us to practice the code we know so far, but man, couldn't they have come up with something less stupid for this one? The thing is that at the end of the exercise they say "as extra practice, see if you can find a way to return only exact matches for your name rather than anything starting with the first letter," but they offer no guidance for this, and since the method they gave us was kind of stupid to begin with, I think it's pretty hard to figure out a way to do that without jumping through an insane number of hoops. Also, everything I'm learning is reinforcing my feeling that Javascript is incredibly messy and sloppy and unnecessarily complicated and I wish I didn't have to learn it!

Anyway, now I'm on "while" loops, which are a lot simpler and easier to understand. Baby steps... I'm looking forward to finishing this course and switching back to Python!

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7591 on: July 26, 2015, 02:00:33 am »

MDN for .join()!

tldr: yourArray.join("") returns a string where everything in the array is joined with the "", which is nothing.

You can also try "," for commas and " " for spaces.


Bonus fact: You can do the same in Python! "".join(list)
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7592 on: July 26, 2015, 02:06:28 am »

This reminds me of that screenshot someone posted a while ago with a stackoverflow question about addition in Javascript. Everyone was telling him to download some library for a function therein. The one dude who mentioned a + operator was downvoted to oblivion.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7593 on: July 26, 2015, 02:13:49 am »

This reminds me of that screenshot someone posted a while ago with a stackoverflow question about addition in Javascript. Everyone was telling him to download some library for a function therein. The one dude who mentioned a + operator was downvoted to oblivion.

I think it was a joke, but it was pretty funny :D
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Sappho

  • Bay Watcher
  • AKA Aira; Legendary Female Gamer
    • View Profile
    • Aira Plays Games
Re: if self.isCoder(): post() #Programming Thread
« Reply #7594 on: July 26, 2015, 02:20:43 am »

MDN for .join()!

tldr: yourArray.join("") returns a string where everything in the array is joined with the "", which is nothing.

You can also try "," for commas and " " for spaces.


Bonus fact: You can do the same in Python! "".join(list)

Awesome, thanks! You are also a great teacher. :D

EDIT: IT WORKS!!!!!

Code: [Select]
var text = "as erfndfga aNegafaerfaersmrfah aifuvadr Nameasdmasef \
efmegaasd jwefaf Name sacfsjf";
var myName = "Name";
var test = [];
var hits = [];
var testStr = "";

for (var i = 0; i < text.length; i++){
    if (text[i] === myName[0]) {
        for (var j = i; j < i + myName.length; j++) {
            test.push(text[j]);
            testStr = test.join("");
            }
        }
        if (testStr === myName) {
                hits.push(testStr);
                test = [];
                testStr = "";
            } else {
                test = [];
                testStr = "";
           
        }
}

if (hits === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

It now returns ['Name', 'Name']. Actually somewhat functional! Huzzah!!!
« Last Edit: July 26, 2015, 02:37:51 am by Sappho »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7595 on: July 26, 2015, 03:29:20 am »

A couple of tips: you're using the join every time a character is added to the array. You don't need to do that: you can put the join outside the for loop, and it will have the same end result, but be faster and more efficient (only doing the join once per word).

Also, there's no real reason to also set testStr = "" when you set test to an empty array. Since testStr is overwritten anyway before you use it, you only need to set "test" to empty each time.

anyway, you can clean things up a lot more by just ditching the push / join stuff altogether. Just use concatenation:

Code: [Select]
test += text[j]; // this is the same as writing test = test + text[j]
and just initialize test as a string (var test = "")
« Last Edit: July 26, 2015, 03:36:52 am by Reelya »
Logged

Sappho

  • Bay Watcher
  • AKA Aira; Legendary Female Gamer
    • View Profile
    • Aira Plays Games
Re: if self.isCoder(): post() #Programming Thread
« Reply #7596 on: July 26, 2015, 03:48:20 am »

At first I didn't reset the testStr every time, and the end result was that it printed the name like 100 times in the end. I don't know why, but for some reason it seems it was appending rather than overwriting the string. When I added the testStr= "" lines, the result was what I wanted. I don't really understand why, but for some reason this seems to be necessary. (Actually, if someone can explain why it was necessary, that would be very helpful.)

I was using the array because the task was to use an array to do this. However, this is clearly a very inefficient and kind of stupid way to do this. I suppose I should try to concatenation way to make sure I understand how it works, even though this course doesn't teach that command at all. (Actually, it doesn't teach a LOT of things. It's a very basic course for beginners.)

By the way, is there a way to check a string without case sensitivity? I know how to do it in Python, but I can't find a way to do it in Javascript. It's checking if someone types "yes" but it considers "Yes" to be a different word, for example. There *must* be some way to check it without case sensitivity, right? Or at least to convert strings into all lower-case, or something?

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7597 on: July 26, 2015, 04:06:09 am »

you can wrap the two things you comparing inside functions that force them to be the same case.

http://www.w3schools.com/jsref/jsref_tolowercase.asp

so instead of "if(str1 === str2)" you have "if(str1.toLowerCase() === str2.toLowerCase())"

this won't actually change the strings, it will just make temporary variables and compare those to each other.

Sappho

  • Bay Watcher
  • AKA Aira; Legendary Female Gamer
    • View Profile
    • Aira Plays Games
Re: if self.isCoder(): post() #Programming Thread
« Reply #7598 on: July 26, 2015, 04:17:48 am »

That's exactly what I was looking for, thanks. This course doesn't include the "toLowerCase", and I was trying to find it but I'm not good at finding this stuff yet. :P Glad it exists, and now I know what it is! :D

EDIT: Another question - is there a way to convert numbers to text? Like, if a variable === 4, instead of printing "4" tell it to print "four"?
« Last Edit: July 26, 2015, 04:26:30 am by Sappho »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7599 on: July 26, 2015, 04:27:39 am »

mostly i just google things. It's quite easy if you already know the command in another language, e.g. in c++ the command is "toLower" and in Python the command is just "lower". You can throw either one of those into google e.g. "javascript lower" and it will 99/100 times give you the equivalent command in that language. so just google the name of the language you are learning with a few keywords for the function you need to know, even if the only keyword you know is from a totally different language.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7600 on: July 26, 2015, 05:09:56 am »

That's exactly what I was looking for, thanks. This course doesn't include the "toLowerCase", and I was trying to find it but I'm not good at finding this stuff yet. :P Glad it exists, and now I know what it is! :D

EDIT: Another question - is there a way to convert numbers to text? Like, if a variable === 4, instead of printing "4" tell it to print "four"?
Probably easiest to make a lookup table for smaller numbers below twenty
Logged

Moghjubar

  • Bay Watcher
  • Science gets you to space.
    • View Profile
    • Demon Legend
Re: if self.isCoder(): post() #Programming Thread
« Reply #7601 on: July 26, 2015, 05:56:00 am »

EDIT: Another question - is there a way to convert numbers to text? Like, if a variable === 4, instead of printing "4" tell it to print "four"?

Should be a library out there that does it already, if you want to go looking for code, but afaik no native libraries do that.  (try http://javascript.about.com/library/bltoword.htm )
Logged
Steam ID
Making things in Unity
Current Project: Demon Legend
Also working on THIS! Farworld Pioneers
Mastodon

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7602 on: July 26, 2015, 06:06:06 am »

If you want to code that yourself for learning, the trick is to be able to make a function that correctly outputs any number from zero to 999 as words. So you have a single function which can generate any number up to 999 correctly. Then, you take your original bigger number and chop it up into 3 digit segments (starting from the end). Send each segment to your words function (treat them like a regular small number), and append "thousand", "million", "billion" and so on, in the correct locations.

Of course, this won't do for years at all. You want a whole other algorithm for turning years into meaningful words. So it's definitely context-specific, which may explain why there are no universal native functions for this.
« Last Edit: July 26, 2015, 06:13:20 am by Reelya »
Logged

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #7603 on: July 26, 2015, 07:12:25 am »

In C#, an array of objects is possible, right?

Because this:

Code: [Select]
class TopicData
{
    public string TopicID;
    //some other stuff
}

class program
{
    TopicData[] Topic = new TopicData[1];

    void SomeProcedure
    {
        Array.Resize<TopicData>(ref Topic, 8);
        for (int I = 0; I < 8; I++)
        {
            Topic[I].TopicID = "Foo"; //Nope. NullReferenceException.
        }
    }
}

This keeps throwing NullReference Exceptions.

I attempted using:
Code: [Select]
Topic = new TopicData[8];Instead of resizing the array, but it still fails in the same place.
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7604 on: July 26, 2015, 07:17:37 am »

You're only declaring it of size 1. Then you're trying to reference slots 0-7. Make it 8 slots.

EDIT: Sorry didn't read the bottom bit before writing that. I compiled it in Unity/C# without problems, but I did have to reorganize it a bit. Let me try it in VS instead.
« Last Edit: July 26, 2015, 07:24:09 am by Reelya »
Logged
Pages: 1 ... 505 506 [507] 508 509 ... 795