![]() |
|
Re: Learning Javascript Thread
Code:
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10].sort(function(a, b) { return 0.5 - Math.random(); }); |
Re: Learning Javascript Thread
Code:
#IWasUnAwareOfThisFeature |
Re: Learning Javascript Thread
Quote:
|
Re: Learning Javascript Thread
Quote:
just a quick glance over, you're dealing infinity-deck blackjack. if you want to go single deck, you can do card = deck.pop(); or you can do card = deck[deckPosition]; and increment deckPosition. maybe i'll post poor man's blackjack later. |
Re: Learning Javascript Thread
Quote:
the project wasn't easy for me and im quite amazed i actually made it through tbh. Is my code in need of a lot of improvement or is that a pretty succinct effort at the task? |
Re: Learning Javascript Thread
Quote:
I would first look into your hit() and dealerHit() functions and try to re-factor that into 1 function and then pass isDealer as a parameter to handle the extra logic. |
Re: Learning Javascript Thread
i don't have much to say about your code as a console attempt, but you might want to have a look at this: http://google-styleguide.googlecode....criptguide.xml :).
also i whipped this up though haven't tested much. poor man's blackjack: Code:
Array.prototype.max = function() { |
Re: Learning Javascript Thread
be nice to always see the dealer's up card...
Code:
|
Re: Learning Javascript Thread
Quote:
|
Re: Learning Javascript Thread
For those new to JS, here's an excellent tool for debugging code:
http://www.jshint.com I actually like it a little better than JSLint b/c you can set it to assume jQuery. One other suggestion -- along with posting code in here, it can be a little easier for others to try out your code if you create a jsFiddle. Just go to http://www.jsfiddle.net, paste in your code, and click "Save". It will create a URL that you can paste in so people can immediately try out your code. This is a pretty cool thread idea, I'd definitely like to get involved. |
Re: Learning Javascript Thread
I write CoffeeScript on a daily basis so I guess I'll chime in here.
I've had a love/hate relationship with JavaScript for much of my web development life. While it is a very expressive language with some cool features and the syntax is easy and familiar, the language has so many ****ing issues its ridiculous. While JavaScript looks like C or Java based by it's syntax, it's actually more like Self and Scheme. It's extremely functional (functions are first-class objects) and it's object oriented implementation follows prototypal inheritance as opposed to the ever more popular classical inheritance. Lending itself to functional programming, lambdas, closures and first class functions gives you a TON of flexibility. What I said above is actually what I like about JavaScript. It's functional nature allows you to do really neat and interesting things but as a beginner to programming you need to understand that JavaScript is not the norm. The language also has many design nightmares and I'm not even talking about all the various web browser implementations. Some big issues with JavaScript in the tiny details that will turn into gotchas that you're trying to find for hours. A quick list off the top of my head: - object properties are not easily iterable... you end up pulling in inherited properties from the base Object class which all JavaScript objects are born from. They also seem to follow a random order - not instantiating all of the variables at the top of your script or code blocks (even if you aren't assigning them to anything right then and there) - constant switching of context and the keyword "this." You have to assign "this" to another variable in order to access outer context in closures. - easily polluting the global namespace (this becomes a huge bitch when you start writing your own or using other people's libraries if they aren't designed well) - comparison operators and type checking are all over the place. You really need to understand how JavaScript handles object references and literals - JavaScripts crappy object constructor (the "new" keyword) and what it actually does. - no private properties on objects (there's a hacky kind of way of doing this, I guess). Really when you dig into the history of JavaScript what you find are a lot of solutions to the quick list I made in various libraries and coding practices. However, these are all compromises done by people out of the necessity of having to use the language that is the lingua franca of the web. All this said, if I were to start over JavaScript today here is what I would do: - Take a couple free online JavaScript courses - Buy Modern Javascript: Develop and Design by Larry Ullman (this is a great primer on client-side JavaScript for Events, DOM manipulation and Ajax) - Buy Programming JavaScript Applications by Eric Elliott (this book is new and a work in progress but is absolutely fantastic at describing the short comings of current day JavaScript and the patterns to overcome them, its currently on chapter 5 or 6.) - Learn CoffeeScript That's it. Once you know the history of JavaScript and it's quirks you are safe to use CoffeeScript (or any other to-JS language of choice). It's so important to understand all the nuances of this dumb language before switching to one of them though. The reason for this is that you will get caught up in Clojure, CoffeeScript, Dart, etc. and the only way to see the bugs sometimes is to inspect the compiled JavaScript source and understand what's really going on. You'll notice I didn't recommend Douglas Crockford's JavaScript: The Good Parts book . I find him to be a bit of an ego stroker and that book is from 2008. Not to say it isn't still relevant but JavaScript's best practices are changing almost yearly. He's also a "JavaScript purist" so his ideals are a bit extreme. I also didn't recommend The Definitive Guide as I think that reads more like a reference guide than a book to learn from. |
Re: Learning Javascript Thread
Quote:
never had much luck book learning, but i do find myself reading blogs. there's a good list here: http://stackoverflow.com/questions/4...vascript-blogs i will probably check out resig's javascript ninja book though. |
Re: Learning Javascript Thread
There's a certain elegance to JavaScript until you start using an actually elegant language :p I really do recommend checking out CoffeeScript. If you come from a Ruby or Python background it will be cake. People bitch about it being strict on indentation, but c'mon it's 2012 - if you aren't properly formatting your code it isn't the language's fault.
Its compiler takes care of 95% of those "gotchas". Some of my favorite features: - You don't have to declare variables explicitly with the var keyword - You can declare variables anywhere in the script and it will automatically declare them for you at the top of the respective "block" (quoted because JavaScript doesn't technically have code blocks, just scopes) - A classical style implementation for OOP equipped with real inheritance (I do thank JavaScript for its expressiveness to allow CoffeeScript to emulate this). It's really neat, you get to write things out like classes, and they work like classes, but when it compiles it does it using the prototypal chain. - Fat arrow functions to help lock context - Super simple hash rocket (->) for declaring a function, so great when dealing with callbacks - All files are automatically wrapped in an anonymous function that gets executed - Higher level loops that actually compile to low level for loops - unless conditional statements - existential operator The way I look at it is this: can I write better JavaScript than some of the very best minds in the JavaScript scene? For me it's no and when you combine that with the other awesome features it's a no brainer. Even trivial jQuery things become expressed so much more clearly. This: Code:
$(document).ready(function() {Code:
$ -> |
Re: Learning Javascript Thread
There's some bad stuff with coffee script too.
- If you plan to do any open source work you're instantly cutting off a huge majority of people who would use or contribute to your project. The JS that CS generates is JS in the end but it's really verbose and definitely doesn't resemble how most people would write JS by hand. - If you ever get stuck on a problem and need to post something on stackoverflow you'll only get help by people who write CS unless you manually convert it to JS. - Most reasonable code editors support snippets. You don't save that much time with CS. You might type -> for a function but in my editor I type "fn" and hit tab and I get the same thing. Speaking on code editors, that's another thing too. You'll be restricted to using an editor that has proper support for CS (syntax highlighting, indention rules, possibly snippets). - If you work locally with multiple developers they will have to use CS. If you try to get a job as a JS developer you will very restricted. - CS really doesn't do anything except change the syntax of JS and enforce their standards. JS is tricky because there's usually a few ways to do anything, CS picks one for you and you can't opt out. In other words, everything that CS does can be done in regular JS if you stick to using those standards. The only difference is you will be writing JS instead of some other syntax that compiles down to JS. |
Re: Learning Javascript Thread
Those were great arguments against CoffeeScript early on. Syntax highlighting is available for all the popular editors. CoffeeScript incredibly wide spread now and even a lot of companies are using it over JavaScript for productivity purposes alone. You can learn CoffeeScript in a weekend if you are an intermediate (doing more than DOM manipulation and Ajax) JavaScript developer. That said, 99% of the gotchas people run into with CoffeeScript are context issues, which you're going to have in JavaScript anyway. Also interesting is that ECMAScript 6 is adopting a couple of features from CoffeeScript.
|
Re: Learning Javascript Thread
I wouldn't really say it's wide spread at all tbh.
Stackoverflow tag search: JS: 240,641 -vs- CS: 1,798 (JS+CS: 747) That's not even close. That's less than 1% if you were to compare JS vs CS. On a popular code distribution site like github which hosts code from any language JS is being used by 20% of the entire code base and is the #1 used language. You can't even see the % for coffee script because it's so low. It's the 11th most popular language but to put that into perspective that's likely going to be 2-3% usage based on the 10th most popular language being 3%. |
Re: Learning Javascript Thread
Oh c'mon those are lame ass metrics and I don't even need to explain why but I will if you want me to.
|
Re: Learning Javascript Thread
Yeah I do want you to explain why because it's hard to disagree with github and stackoverflow's stats. Those are both extremely popular and extremely developer oriented web sites.
Here's another fun metric on raw search results from Google: javascript: About 2,160,000,000 results "coffee script": About 259,000 results coffeescript: About 515,000 results That's multiple orders of magnitudes lower. |
Re: Learning Javascript Thread
I'm a huge fan of Coffeescript. Those numbers appear to me as opportunity, old developers stuck where they are either out of momentum, complacency, or corporate/client dictate. Rails 3.1 is shipping with Coffeescript, and I've heard/seen several well known developers discuss it. It's just so much nicer not looking at all the clutter while trying to think.
|
Re: Learning Javascript Thread
Quote:
- JavaScript is ~15 years old, CoffeeScript is barely two years old (in regards to your Google search) - A lot of the JS libraries on GitHub are jQuery fart plugins and libraries dealing with the short comings of JS - Most of the CS libraries on GitHub are to add utility to the language - Obviously StackOverflow is going to have more JS questions than CS questions. JS has issues that a lot of JS developers get caught up on. And more importantly, the average programming skill level of JS developers is far lower than the average CS programmer. I bet if you were to compare your metrics to only include the higher level of JS programmers, I would say the top 1% of the language because there are so many crappy JS programmers, to CoffeeScripters then your metrics would be a lot more close than you think. |
Re: Learning Javascript Thread
Quote:
There's no way to do a filter on use case + language breakdowns on github but you can kind of do this on stackoverflow by chaining tags: javascript+node.js: 3,272 coffeescript+node.js: 291 That's still over 10x less and that's using coffee script in basically the best environment you can ask for because you can seamlessly integrate it into your stack. |
Re: Learning Javascript Thread
Again, probably 95% of JavaScripters don't understand Asynchronous programming and callbacks (outside of client-side event handlers) so naturally they are going to have TONS of issues when trying to learn Node.js which results in more SO questions.
I'm also a Node developer, by that I mean actually working somewhere using it in production. In almost a year of using Node and CoffeeScript none of us have ever had an issue of an npm package not working with CoffeeScript and we have not felt limited at all by our choice to use CoffeeScript. How is JS more flexible than CS? lol... All CS is doing is using the more natural classical syntax that all JS programmers are emulating anyway by more or less using Prototypes as classes. Literally the _only_ argument that JSers have left against CS is that the CS compiler doesn't give you line numbers in compile errors in CS but instead gives you them in the compiled JS. That's only an issue for the client-side anyway. This is going to be fixed soon. I wonder what they will find to complain about next... I'd just like to say I'm supportive of JS and all to-JS programming languages. But in terms of efficiency and time to market, once you have the important concepts and understanding of JS gotchas down I consider it to be a no brainer to move onto a to-JS language of your choice. I think after using CS for ~3 months I'm pretty much able to port any JS code I see in my head into CS anyway so digging through JS source and docs isn't hindering me in any way. |
Re: Learning Javascript Thread
Quote:
None of that has anything to do with CS vs JS but it does cause segregation of the community. CS by itself adds even more separation on top of that as a completely different layer. I've looked at some modules on github and saw they were written in CS. I simply said "fk this, I'll find a JS version". I haven't been let down yet. Also it took you 3 months to get almost used to using CS without having to mentally context switch. Wouldn't it be easier to have just not used CS and not have to deal with context switching? Part of the reason why node is so nice is because you alleviate your brain of having to context switch but CS does the opposite of that. Also I'd argue to the death that CS saves pretty much no time when actually coding either because you if you want to throw the "modern IDE" argument at me, I'll agree with it and leverage their advanced tooling too. I spend most of my time thinking about logic compared to actually typing code out. Typing code is the easy part. Things like auto complete or snippets make typing code almost instant. Having work flows either automated or second nature is what makes for rapid development. Saving a few key strokes is irrelevant in the big picture. It would be like trying to optimize a loop to do 5 million ops/s when it already does 3.8 million ops/s but in each iteration it's writing to the dom which is performing at like 50 thousand ops/s. Nearly useless. |
Re: Learning Javascript Thread
I find transforming logic into code via CS is a bit more intuitive than JS. I'm not fighting for the world to adopt CS... You'll find most CSers don't really care who uses the language. It's almost always the JS people bitching that we aren't using JS.
|
Re: Learning Javascript Thread
It's because there's no purpose to it and it just splits people apart, that is why the JS community who doesn't use CS is against it.
Something like less, sass or stylus has a great purpose. It compiles down to css but it offers things that css is not capable of doing. |
| All times are GMT -4. The time now is 04:27 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive