|
Nolgthorn posted:You want something like this. To avoid hoisting and save you some sanity, plz use `let` and `const` instead of `var`. Super easier. They're forcing jquery on 'im, I'd half expect them to not have let and const support.
|
# ? Oct 8, 2017 01:01 |
|
|
# ? May 16, 2024 18:53 |
|
Man these classes using old javascript make me feel like Javascript is the new PHP. With all of these alternate old ways of doing things, that are still supported, that you're not supposed to use, that are taught in classes. I can see learning programming in school to start, but if I had taken a class for everything I wanted to know I'd have never made it anywhere.
|
# ? Oct 8, 2017 01:13 |
|
Skandranon posted:Imagine you want to encapsulate some code that does something for you. Specifically, calculate PI. Lets call that getPI(). Nolgthorn posted:
Christ this is what I needed. A simple example that explains what a return does, and a "for your code this is what you have wrong. This is an outline for how it should go". Thank you guys so much. I've been so frustrated with how this class explains poo poo.
|
# ? Oct 8, 2017 02:42 |
|
Coco Rodreguiz posted:Thank you guys so much. I've been so frustrated with how this class explains poo poo. Yeah, there are a few red flags about that class, least of which is jquery when they can't even teach you what return does. Also there's nothing wrong with: code:
Ranzear fucked around with this message at 03:17 on Oct 8, 2017 |
# ? Oct 8, 2017 03:13 |
|
Coco Rodreguiz posted:Can someone help me out here? I'm taking a JavaScript class and I hosed this assignment up enough it dropped me a letter grade but I'm still not 100% on what I did wrong. Your instructor is bad and I hope you didn't spend too much on this course because this code they gave you is real bad and completely broken. I genuinely don't know where you are at in terms of learning how to code generally so if any of this sounds condescending please don't take it that way. I was totally where you are 10 years ago and would've killed for an explanation like this. Really, I promise, I'm quite dumb, it took me a long time to grasp any semi complex code. It is complicated, despite what any goon spergs might say, but believe me, if my dumb rear end can learn it anyone can. So here is my heavily commented version of what you posted: JavaScript code:
Dogcow fucked around with this message at 05:39 on Oct 8, 2017 |
# ? Oct 8, 2017 05:30 |
|
code:
code:
Nolgthorn fucked around with this message at 17:48 on Oct 8, 2017 |
# ? Oct 8, 2017 17:41 |
|
Nolgthorn posted:
Yep, it is totally broken that way too. The second prompt does nothing. I'm also assuming it's suppose to be a while loop (while intNewLetter.length > 1) so you have to enter a single letter to continue.. but who knows.
|
# ? Oct 8, 2017 17:46 |
|
And here's a quick idea of what all that code looks like in a saner world, using all the relevant built-ins and chucking unnecessary stuff:JavaScript code:
The annotations before each function aren't necessary, but some editors (for example: Visual Studio Code, available free) will use them to automatically check that what you're doing with parameters and return values is right and will show you errors if you do something wrong (e.g. returning a number on a function marked with a string return), as well as supplying documentation popups when you reference one function somewhere else. Javascript stuff generally uses JSDoc, but there's very similar stuff for other languages. As already mentioned, const ("this value never never changes")/let ("this value can change") are generally better to use than var because of annoying, complicated reasons with scope (var does different weird legacy things based on where you use it, while const/let always act consistently). However, it does depend on having a more-or-less modern browser that supports them properly. Triple equals (===) are almost always better than double equals (==), because double equals will implicitly convert different types, which results in assorted weird dumb stuff. For example, 0 == "0", null == undefined, and 0 == new String("0") are all true. This may seem convenient at first, but it inevitably sets something on fire when one function or another accepts bad input because of it. Arrow functions (using the value => { whatever } format instead of function (value) { whatever } format inside the forEach) are super convenient both for their shorter syntax, and because they fix some weird legacy issues with this. For now you don't really need to worry about that, but the short explanation is that with normal function declarations, if you ever need to use the special this value from the outside on the inside, you need to do weird workarounds; with an arrow function, the this on the inside is automatically the same as the this on the outside. However, like const/let, you need to be using a browser that supports them. (One catch: If you have more than one param, you need to write it like (param1, param2) => { whatever } instead.) Ternary if statements (x === y ? whatHappensIfTrue : whatHappensIfFalse) are super useful for small inline stuff. Don't overuse them because it can make your code nigh unreadable, but since you can use them inline (unlike normal if statements), for cases like this it's an easy way to simplify things that would otherwise be identical except for a single value. Some other languages (like Ruby) treat even a normal if statement as an inline value and so don't have anything like a ternary if. Edit: And for funsies, the minimalist version: JavaScript code:
Roadie fucked around with this message at 21:08 on Oct 8, 2017 |
# ? Oct 8, 2017 20:18 |
|
Dude not even God himself understands bitwise operations.
|
# ? Oct 8, 2017 23:03 |
Roadie posted:Don't you just love bitwise operations? In a strongly typed language I try to not use them, but don't mind them, since behaviour is well-defined. In one where types are more of a hint than an instruction, I tend to avoid them like the plague. There's a massive difference between the char '1', the int 1 and the float 1.0, and I have no idea where or when JS might decide it needs to use on or the other.
|
|
# ? Oct 8, 2017 23:23 |
|
Assuming that you are working with a JavaScript number - i.e. a 64-bit float - bitwise operators (potentially lossily) convert/cast/truncate all of their operands to signed 32-bit integers prior to operation, and (losslessly now) convert the result back to a JavaScript number to return. E: Do not perform bitwise operations on non-numbers and then complain about the results.
|
# ? Oct 9, 2017 00:48 |
|
What am I missing here? In React, I have a component that receives props, calls an ajax request on those props with componentDidUpdate(), and then sets the state after the ajax request is done. I see that I've created an infinite loop but I'm not sure how to stop it. Thoughts?
|
# ? Oct 9, 2017 02:10 |
|
huhu posted:What am I missing here? It's not a react problem, but it's time to refactor and figure out calls in an order that's sane. Not always easy, but in cases like this, it's so you don't end up with circular logic.
|
# ? Oct 9, 2017 02:31 |
|
huhu posted:What am I missing here? Compare this.props to prevProps (first argument to componentDidUpdate) to see if you actually got new props and need to re-call the endpoint
|
# ? Oct 9, 2017 02:43 |
|
Ranzear posted:
I was using an iconv node module and was calling it identically to what you have there with the same parameters. Was getting that odd apostrophe, so I went towards a more brute force solution which strips all diacritics from text and replaces them with their standard ascii counterpart. Your suggestion for storing searchable text was right on the money. I'm sure I'll use that pattern elsewhere if I bump up against a similar issue.
|
# ? Oct 9, 2017 07:41 |
|
prom candy posted:Compare this.props to prevProps (first argument to componentDidUpdate) to see if you actually got new props and need to re-call the endpoint Possibly the better approach is to do the compare on componentWillReceiveProps rather than DidUpdate, and WillReceiveProps does not trigger on internal state updates, whereas DidUpdate does.
|
# ? Oct 9, 2017 07:45 |
|
Osmosisch posted:Heads up for those still using bower: they are advising you to gtfo. Yeah this is pretty funny. I was just applying to a job that listed bower as a requirement and then went to bower's homepage where it says to just use yarn. I was p much thinking "....wait what?"
|
# ? Oct 9, 2017 13:56 |
|
Maluco Marinero posted:Possibly the better approach is to do the compare on componentWillReceiveProps rather than DidUpdate, and WillReceiveProps does not trigger on internal state updates, whereas DidUpdate does. Very true yeah, I was assuming componentDidUpdate was being used on purpose but if not then use componentWillReceiveProps. In that case your first arg is nextProps (the incoming props) instead of prevProps.
|
# ? Oct 9, 2017 14:46 |
|
I always prefer code where intent is very explicit so I avoid bitwise operations.
|
# ? Oct 9, 2017 14:53 |
|
Nolgthorn posted:
Try that with an emoji
|
# ? Oct 9, 2017 15:35 |
|
What kind of things can I make with Javascript? Besides boring front-end crap?
|
# ? Oct 9, 2017 16:01 |
|
With Node.js you can make anything.
|
# ? Oct 9, 2017 16:13 |
|
tricksnake posted:What kind of things can I make with Javascript? Besides boring front-end crap? Boring games, boring mobile apps, boring desktop apps,, boring back-end crap: a whole world of boring crap is open to you with JavaScript.
|
# ? Oct 9, 2017 17:16 |
|
tricksnake posted:What kind of things can I make with Javascript? Besides boring front-end crap? I really should get going on Boats Game already. Edit: Some older crap, pre-WebAudio: https://caliber.online/SuperTanks.htm The things I did to have stereo sound... Ranzear fucked around with this message at 18:22 on Oct 9, 2017 |
# ? Oct 9, 2017 18:00 |
Not sure if this is the right thread to ask this, but if I compile some C++ to webassembly and am using multithreading (e.g. std::thread, std::future etc.) will that execute on multiple threads, or is webassembly stuck with one thread like JS? If so, to get parallel execution when compiling with gcc for native execution I have to add the compiler flag -pthread to get mutexes and such to act like they're supposed to, do I have to do something similar for webassembly compilation?
|
|
# ? Oct 9, 2017 18:09 |
|
The Unity3D game engine used to be a modified version of javascript. But, you can use it for all kinds of cool stuff due to the callstack and singlethreadedness.
|
# ? Oct 9, 2017 18:11 |
|
Guess who's back back back? I am doing some promises mixed with some piping and binding and I am trying something with a try catch but the promise is not catching it and it is saying its an unhandled error. Not sure why: code:
|
# ? Oct 9, 2017 18:41 |
|
Knifegrab posted:Guess who's back back back? My guess would be proc.stdout or that transform function that is defin d somewhere else catching it instead, but I don't know what the non-standard stuff Q is doing with fcall and fail so I can't be sure. For fun, throw on the first line of sendResponse. That code seems... odd as well. Many nested functions is generally a code smell, but again, I don't know Q.
|
# ? Oct 9, 2017 18:51 |
|
Knifegrab posted:So ignoring why it might catch an error or what might be causing an error, why isn't my code approprietly handling this error I throw in the .fail portion of the promise chain? The throw is getting eaten by the multiple nested function definitions. You need to refactor it. I also don't understand why you're using promises, since I don't see anything async there. Plus, if you're going to use promises, you should use real promises, not the Q library (unless you're totally stuck with it for legacy reasons).
|
# ? Oct 9, 2017 18:55 |
|
Roadie posted:The throw is getting eaten by the multiple nested function definitions. You need to refactor it. Sorry so there is async stuff above but that is cut out for brevity, this is pretty far into the promise chain. I've also replaced teh stupid Q poo poo with real promises now. Knifegrab fucked around with this message at 19:39 on Oct 9, 2017 |
# ? Oct 9, 2017 19:15 |
|
Joda posted:Not sure if this is the right thread to ask this, but if I compile some C++ to webassembly and am using multithreading (e.g. std::thread, std::future etc.) will that execute on multiple threads, or is webassembly stuck with one thread like JS? If so, to get parallel execution when compiling with gcc for native execution I have to add the compiler flag -pthread to get mutexes and such to act like they're supposed to, do I have to do something similar for webassembly compilation? Multi-threading is supposed to be landing this https://wasmdash.appspot.com Kinda meh page: https://github.com/WebAssembly/threads MrMoo fucked around with this message at 20:27 on Oct 9, 2017 |
# ? Oct 9, 2017 19:40 |
|
code:
|
# ? Oct 9, 2017 19:59 |
|
Knifegrab posted:
First off: fail is not a thing in promises. Use catch. Secondly: your logic is still confusing and has many places where an exception could get squashed. Do this: code:
|
# ? Oct 9, 2017 20:42 |
|
Once you get it working move the `throw 'buttes'` into the catch block since you know it'll never get thrown again anyway.
|
# ? Oct 9, 2017 21:02 |
|
Knifegrab posted:
Looking at the docs for your promise library, .fail(failureHandler) is just a shortcut for doing .then(iDontCareAboutThisSuccessHandler, failurehandler) Since you're using .then(successhandler), it doesn't looks like your subsequent .fail is going to be invoked unless the successHandler in your .then returns another [rejected] "Q" promise You could either add a failureHandler callback to your .then (after your existing successHandler, or add a .catch after the .then ROFLburger fucked around with this message at 21:43 on Oct 9, 2017 |
# ? Oct 9, 2017 21:40 |
|
porksmash posted:With Node.js you can make anything. Yeah; apart from an actual OS (don't even think about trying to create NodeRTOS! ), the sky's the limit.
|
# ? Oct 10, 2017 01:29 |
|
Knifegrab posted:Changed it to this but it is still saying that it is an unhandled error. I am quite confused by this. Your errors are still getting eaten by your inner function scope. Don't do that, it's silly. JavaScript code:
|
# ? Oct 10, 2017 02:04 |
|
I am very, very new to using Javascript and sometimes have to write little snippets for my workplace. I am trying to have a function that reads something from the url and then uses it to pass along some info in a url when the participant clicks on a link. The code I wrote works on most modern web browsers but I just had someone testing the website out come back to me and say that on their iPhone 5 with Safari they got an error message. This is the code:code:
|
# ? Oct 12, 2017 20:39 |
|
Mobile safari is very unpredictable. You are defining the city variable twice, you only need to define it the first time.code:
|
# ? Oct 12, 2017 21:01 |
|
|
# ? May 16, 2024 18:53 |
|
Rosalind posted:I am very, very new to using Javascript and sometimes have to write little snippets for my workplace. I am trying to have a function that reads something from the url and then uses it to pass along some info in a url when the participant clicks on a link. The code I wrote works on most modern web browsers but I just had someone testing the website out come back to me and say that on their iPhone 5 with Safari they got an error message. This is the code: Mobile Safari does not pass the referrer header, so that's why it does not work.
|
# ? Oct 12, 2017 21:11 |