|
If that answer was given to me in an interview you wouldn't get the job. But yeah js is terrible
|
# ? Jun 16, 2017 17:33 |
|
|
# ? May 15, 2024 04:07 |
|
Grump posted:God i just took a coding test for an interview and i'm only realizing now comparing two arrays is such a pain in JS Just curious could you be more specific? I can't think of why this would be unless you're talking about equality comparisons when you're looping through?
|
# ? Jun 16, 2017 17:39 |
|
I'm curious about the actual question and how it was worded. I can think of a few ways, but I'm still learning JS, so it would be a fun exercise for me...
|
# ? Jun 16, 2017 17:41 |
|
So basically i'm converting a array of ints into a multidimensional array with pairs that add up to 10. Then i had to filter out the duplicates and that's where i was running into trouble with comparing the nested arrays. Converting them to strings works fine as far as i'm concerned code:
teen phone cutie fucked around with this message at 18:16 on Jun 16, 2017 |
# ? Jun 16, 2017 18:13 |
|
The classic JSON.stringify(a) === JSON.stringify(b) is still valid for comparing objects. With arrays there is room for interpretation of what equality means, the Polymer project has this issue when detecting changes in an array: https://www.polymer-project.org/1.0/docs/devguide/model-data
|
# ? Jun 16, 2017 18:32 |
|
For this problem, I feel like you can just use the fact that you know you're dealing with pairs, and that the pairs add up to 10. To get the pairs, do what you do and make the array of length 2 arrays. You know that all the pairs add up to 10, so for each pair, any other pair that has the same first element, it's a duplicate. This won't touch reversed pairs. Once you have stripped out duplicates, for each pair, any pair whose second element is equal to the first element of the pair you are comparing to is a reversal of that pair.
|
# ? Jun 16, 2017 18:41 |
|
MrMoo posted:The classic JSON.stringify(a) === JSON.stringify(b) is still valid for comparing objects. With arrays there is room for interpretation of what equality means, the Polymer project has this issue when detecting changes in an array: I was gonna use json.stringify but everybody on stackoverflow was crucifying it since it's slow.
|
# ? Jun 16, 2017 19:05 |
|
Made me remember this: https://twitter.com/ID_AA_Carmack/status/844885670732685313
|
# ? Jun 16, 2017 19:18 |
|
Here's the implementation of my suggestioncode:
It's here in jfiddle if anyone wants to play with it. https://jsfiddle.net/352na4gd/1/ Like I said, I'm just learning JS, so if I didn't any real bad, please point it out. Thanks. edit: My spaces were converted to tabs when I pasted it here? or something?
|
# ? Jun 16, 2017 19:36 |
|
If you use tab in jsfiddle it's actually putting in tabs, but displays as 4? 2? spaces visually.
|
# ? Jun 16, 2017 23:45 |
|
That explains the confusion. Partially. I used only tabs when in jsfiddle, but when I pasted it, it was a mix of tabs and spaces, so I don't even know.
|
# ? Jun 17, 2017 00:25 |
|
Love Stole the Day posted:I really liked Ben Fhala's thing on Packt, which served as my intro to React. You can do their "1 month free trial"for the Mapt thing so that you can access it and all their other React stuff for free until the trial thing is up. I've been using it to cram with all sorts of stuff and it'll finish in 5 days. So where do I get the starting project for React now? The website's different than the starting video and the closest thing I can find on Facebook's github is the create-react-app repo which has a completely different project structure than what Ben's working with.
|
# ? Jun 17, 2017 18:41 |
|
Welcome to react
|
# ? Jun 17, 2017 18:44 |
|
The Fool posted:Welcome to react It's more like "welcome to tutorials on the internet". Things change and a tutorial not developed to take that into account is not being the best it can be. Granted, JS stuff changes a lot nowadays, but that puts even more of an onus on teaching material.
|
# ? Jun 17, 2017 19:03 |
|
Thats why whenever I search for tutorials on google, I set the search range to only show stuff from the past three months. Even then, sometimes whatever package they are using has completely changed in that time frame.
|
# ? Jun 17, 2017 20:54 |
|
The worst are tutorials that don't say what version they are for.
|
# ? Jun 17, 2017 21:00 |
|
Is there a Javascript library suited for creating print-ready images? I need to create an image with a lot of repeated elements, specifically 40 half-inch squares with some text centered vertically and horizontally in each. I'd rather not do all of this manually in an image editor, and ideally I could rig up a few inputs so I can vary things and get a live preview before saving and printing the file. I've looked at a few SVG libraries, but I'm not sure if that's the best approach. e: one important thing is being able to get the text centered based on the text's bounding box, not the baseline. TopherCStone fucked around with this message at 21:08 on Jun 17, 2017 |
# ? Jun 17, 2017 21:05 |
|
Hey dudes. Looking for API feedback on the datetime module I've been working on. And a re-attack on why I can't get it to import as an npm module (But can by placing the source file in the same folder as my project). I've decided to de-couple it from DateFns, since I'm unable to get around it not accepting these custom types as valid when using certain funcs, as well as API limitations I hadn't noticed with it earlier. (ie it's impossible to safely turn a string into a date with that module). I'd also like to verify that I'm not alone in thinking not having separate date, time, datetime, and delta types is absurd. Dominoes fucked around with this message at 00:38 on Jun 18, 2017 |
# ? Jun 18, 2017 00:36 |
|
Grump posted:God i just took a coding test for an interview and i'm only realizing now comparing two arrays is such a pain in JS When comparing two arrays I usually use lodash's isEqual function: code:
|
# ? Jun 18, 2017 02:53 |
|
Dreadrush posted:When comparing two arrays I usually use lodash's isEqual function: Yes, that's a cute answer. I'd then ask you to write out _.isEqual().
|
# ? Jun 18, 2017 04:33 |
|
Yeah i don't really get why i'm getting crucified. My answer works so who cares???
|
# ? Jun 18, 2017 05:16 |
|
No one's crucifying you? Your answer is good...
|
# ? Jun 18, 2017 05:26 |
|
Skandranon posted:Yes, that's a cute answer. I'd then ask you to write out _.isEqual().
|
# ? Jun 18, 2017 05:36 |
|
Grump posted:Yeah i don't really get why i'm getting crucified. My answer works so who cares??? Sorry, that was not my intent. I was more attacking the idea of answering such questions with library calls. The point of asking "do a sort" or "compare this" is to work through it. Calling "Superlib.doAnything()", while a practical thing to do while actually programming, is not the point of the question.
|
# ? Jun 18, 2017 05:52 |
|
Dominoes posted:Hey dudes. Looking for API feedback on the datetime module I've been working on. And a re-attack on why I can't get it to import as an npm module (But can by placing the source file in the same folder as my project). I've decided to de-couple it from DateFns, since I'm unable to get around it not accepting these custom types as valid when using certain funcs, as well as API limitations I hadn't noticed with it earlier. (ie it's impossible to safely turn a string into a date with that module). It's not clear to me what (if anything) webpack is adding to the build process here. Running the typescript compiler in the root directory will read your tsconfig file and follow those instructions. I've recently been over the hump of publishing a TS project to npm, and I don't see any immediate reason why this one shouldn't be working (if your build is running and your /dist folder gets populated with index.js and index.d.ts). e: I submitted a pull request, why not. Feel free to reject it, I mostly wanted to push some new buttons. As it stands I was able to npm install it from my local directory. Newf fucked around with this message at 06:53 on Jun 18, 2017 |
# ? Jun 18, 2017 06:34 |
|
Skandranon posted:Sorry, that was not my intent. I was more attacking the idea of answering such questions with library calls. The point of asking "do a sort" or "compare this" is to work through it. Calling "Superlib.doAnything()", while a practical thing to do while actually programming, is not the point of the question. Yeah, well the obvious reason you want the implementation as an interviewer is to verify the candidate: - knows which variables are compared by reference vs value - knows how to determine whether you have an object or an array, and how to compare said arrays and objects. Delving implementation indicates you understand fully why lodash has isEquals, which makes it clear you understand some basic facts about how JavaScript treats values.
|
# ? Jun 18, 2017 06:34 |
|
Skandranon posted:Yes, that's a cute answer. I'd then ask you to write out _.isEqual(). I'd then tell you that you and I are not in an interview and I'm merely giving someone an alternative solution when they come across the problem again.
|
# ? Jun 18, 2017 14:35 |
|
The entire conversation is about an interview question.
|
# ? Jun 18, 2017 14:39 |
|
Newf posted:It's not clear to me what (if anything) webpack is adding to the build process here. Running the typescript compiler in the root directory will read your tsconfig file and follow those instructions. I've recently been over the hump of publishing a TS project to npm, and I don't see any immediate reason why this one shouldn't be working (if your build is running and your /dist folder gets populated with index.js and index.d.ts). Dominoes fucked around with this message at 15:46 on Jun 18, 2017 |
# ? Jun 18, 2017 15:09 |
|
Skandranon posted:Yes, that's a cute answer. I'd then ask you to write out _.isEqual(). Lol if you expect your developers to copy existing functionality for the sake of it.
|
# ? Jun 18, 2017 18:20 |
|
Dominoes posted:You did it! Merged your pull, and now it works. Now if only TSC would offer bundling/imports, we could forget about webpack entirely. What is it that you mean by bundling / imports? TSC is using your imported date-fns library here. The 'outfile' flag on compiler options in your tsconfig.json can be used to specify a concatenated, single-file compiled js output, but that's moot in the current case, since your package only has one source file to begin with.
|
# ? Jun 18, 2017 18:31 |
|
Newf posted:What is it that you mean by bundling / imports? TSC is using your imported date-fns library here. The 'outfile' flag on compiler options in your tsconfig.json can be used to specify a concatenated, single-file compiled js output, but that's moot in the current case, since your package only has one source file to begin with. It sound slike from your 'outfile' comment that I'm wrong... I'm skeptical because I spent a good deal of time trying to get imports working with tsc alone, and failed / people on the internet told me I couldn't. How would you set up the equiv of this: ? Ie different scripts for different pages: From webpack.config.js JavaScript code:
Dominoes fucked around with this message at 19:02 on Jun 18, 2017 |
# ? Jun 18, 2017 18:38 |
|
9-Volt Assault posted:Lol if you expect your developers to copy existing functionality for the sake of it. necrotic posted:The entire conversation is about an interview question.
|
# ? Jun 18, 2017 18:39 |
|
Exactly, reimplementing isEqual is a great interview question for establishing whether someone knows basic facts about JavaScript equality and how to rigorously test it properly. . If that's the level of knowledge you'd like it makes sense as a question, and hardly fits within the realm of an ugly or excessive trick question.
|
# ? Jun 18, 2017 23:18 |
|
Is the trick to iterate over each element and comparing with ==, and recursively if the element's an array? Any plans in the next ES version to make array1 == array2 work? Why doesn't it in existing versions?
|
# ? Jun 18, 2017 23:30 |
|
Dominoes posted:Is the trick to iterate over each element and comparing with ==, and recursively if the element's an array? The problem with this is that == and === only compare values for primitive types (string, number, boolean). However, if you have 2 objects that have identical contents (ie: { "name": "Skandranon", "age": 9000 } === { "name": "Skandranon", "age": 9000 }) will be false, as when === is used for objects, it is only comparing the memory address it points at, basically seeing if they are LITERALLY the same object, not if they are equivalent. So for objects you need to recursively iterate over all properties and check that their names & values are the same.
|
# ? Jun 19, 2017 00:11 |
|
JS is an evolving language; no excuse for comparing the arrays directly not to work.
|
# ? Jun 19, 2017 00:14 |
|
Dominoes posted:JS is an evolving language; no excuse for comparing the arrays directly not to work. How do you propose to implement that without breaking all the code that depends on referential checks for high performance?
|
# ? Jun 19, 2017 00:27 |
|
Dominoes posted:JS is an evolving language; no excuse for comparing the arrays directly not to work. You can complain about JavaScript, or write web applications in some other language (good luck with that). Learn to love the one you are with.
|
# ? Jun 19, 2017 00:38 |
|
|
# ? May 15, 2024 04:07 |
|
Maluco Marinero posted:How do you propose to implement that without breaking all the code that depends on referential checks for high performance? Skandranon posted:Learn to love the one you are with. Dominoes fucked around with this message at 01:06 on Jun 19, 2017 |
# ? Jun 19, 2017 00:58 |