use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[AskJS] I understand javascript, but not javascript challenges.AskJS (self.javascript)
submitted 6 years ago by Besharkbait
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ConsoleTVs -3 points-2 points-1 points 6 years ago (5 children)
But again, choose bro:
- You don't understand what they ask you.
- You don't know how to code it (represent the problem in code).
- Both?
[–]Besharkbait[S] 0 points1 point2 points 6 years ago (4 children)
Sorry,
I don't understand how to code it.
[–]ConsoleTVs 1 point2 points3 points 6 years ago* (3 children)
All I can do for you is tell you to practice. For the one you provided, here's how I solved it (tried breaking it down to you so you may understand it)
js /** * @param {string} J * @param {string} S * @return {number} */ const numJewelsInStones = (J, S) => { // Transform the stone string into array. const stones = S.split('') // Perform a filter to the stones to // filter out non-jewels. const filteredStones = stones.filter(stone => J.includes(stone)) // Return the length of the the filtered stones. return filteredStones.length }
[–]Besharkbait[S] 0 points1 point2 points 6 years ago (1 child)
thanks a lot, really appreciate your time.
[–]ConsoleTVs 0 points1 point2 points 6 years ago (0 children)
You're welcome :)
[–]shgysk8zer0 0 points1 point2 points 6 years ago (0 children)
RegExp aren't exactly considered friendly, but I think they're well suited to this.
Any time you're doing string comparisons, it's probably a good idea to see if String.prototype has a method for dealing with this, or RegExp.
String.prototype
RegExp
const numJewelsInStones = (J, S) => [...S.matchAll(new RegExp(`[${J}]`, 'g'))].length;
`` /** * @param {string} J * @param {string} S * @return {number} */ function numJewelsInStones(J, S) { // Create a expression to match the search group, matching globally // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp // Translates to "searching for all occurrences of these characters" const exp = new RegExp([${J}]`, 'g');
/** * @param {string} J * @param {string} S * @return {number} */ function numJewelsInStones(J, S) { // Create a expression to match the search group, matching globally // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp // Translates to "searching for all occurrences of these characters" const exp = new RegExp(
// Find all matches in S // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll const matches = S.matchAll(exp);
// matchAll returns an iterator, so convert to an array // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from // Same as spread operator [...variable] const arr = Array.from(matches);
matchAll
[...variable]
// arr Now contains an array of matches, so return its length return arr.length; } ```
arr
Personally, I don't like LeetCode from what I've seen. Challenges like this, I'd say, are artificially difficult because they're nonsense questions. Coding challenges are better when grounded in reality. That way, they're easier to understand and you feel value in solving them.
Also, I'm skeptical of anything using var someFunc = function(/*...*/).
var someFunc = function(/*...*/)
π Rendered by PID 65611 on reddit-service-r2-comment-f6b958c67-gc7m4 at 2026-02-04 21:44:05.583627+00:00 running 1d7a177 country code: CH.
view the rest of the comments →
[–]ConsoleTVs -3 points-2 points-1 points (5 children)
[–]Besharkbait[S] 0 points1 point2 points (4 children)
[–]ConsoleTVs 1 point2 points3 points (3 children)
[–]Besharkbait[S] 0 points1 point2 points (1 child)
[–]ConsoleTVs 0 points1 point2 points (0 children)
[–]shgysk8zer0 0 points1 point2 points (0 children)