Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
  • Jan 07 03:10
    @bjorno43 banned @doctor-sam
  • Aug 17 2019 09:32
    User @bjorno43 unbanned @linkin-park
  • Jun 20 2019 21:19
    @bjorno43 banned @shenerd140
  • Apr 25 2019 17:13
    @mstellaluna banned @cmal
  • Jan 08 2019 22:05
    @mstellaluna banned @dertiuss323
  • Nov 07 2018 04:42
    User @texas2010 unbanned @ows-ali
  • Nov 02 2018 16:25
    @texas2010 banned @ows-ali
  • Oct 12 2018 05:50
    @bjorno43 banned @NACH74
  • Oct 05 2018 23:02
    @mstellaluna banned @JomoPipi
  • Sep 16 2018 12:21
    @bjorno43 banned @yash-kedia
  • Sep 16 2018 12:16
    @bjorno43 banned @vnikifirov
  • Sep 05 2018 08:12
    User @bjorno43 unbanned @androuino
  • Sep 05 2018 07:38
    @bjorno43 banned @androuino
  • Aug 23 2018 16:57
    User @bjorno43 unbanned @rahuldkjain
  • Aug 23 2018 16:23
    @bjorno43 banned @rahuldkjain
  • Jul 29 2018 14:13
    User @bjorno43 unbanned @jkyereh
  • Jul 29 2018 01:00
    @bjorno43 banned @jkyereh
  • Jul 10 2018 22:09
    @bjorno43 banned @manafn
  • Jul 06 2018 15:20
    @texas2010 banned @imlegend19
  • Jul 03 2018 12:28
    @bjorno43 banned @vbvmatta
So that function would filter values that have an odd remainder when divided by 2?
Thomas Tylek
@TonyRednil no it's not necessary, what you're doing works. I would at least remove Array.prototype and replace it with just arr, but maybe that's just my personal preference
@TonyRednil in regards to @moigithub 's example, think about what's going on with the return value
when 1 is passed in as num, is 1 % 2 === 0?
no, so false is being returned.
and as stated by in the MDN filter link: "Return true to keep the element, false otherwise..."
Thomas Tylek
@TonyRednil so yes, that function removes values that have any remainder after the remainder 2 operation is done. Reminder, % is not division, it is the remainder operator
Your filter method may be more complicated though because the test cases have arguments that are strings too
And I was hoping to keep this code simple....
Thomas Tylek
I would recommend something like return arr.filter(function(element) { return !args.includes(element)}
so ultimately between that and separating the arguments into an array to alter and the arguments to check the array against, it's only a few lines of code
So that is going to ultimately return arr with only values that aren't equal to either argument? What is element? Is that just a way to call each individual value in the array?
Thomas Tylek
filter runs through each element of the array you called it on
So then what is element?
Thomas Tylek
element can be renamed to anything, but essentially element is first arr[0], then arr[1], and so on...
Ah gotcha. So that code you posted is going to check each value in arr and if it's equal to either value in args it will remove it and return everything else? Am I understanding that right?
I'm still new to the while includes method
Thomas Tylek
Yup. Because it goes, "Oh lets filter. We're filtering arr, so let's start with arr[0]. arr[0] is 1. Okay, so let's run value one into the callback function (what's inside filter(...))
"okay, the callback runs. 1 is used as the value in that function (element)."
"the callback wants to know if !args.includes(element), so does the args array NOT include this element (1)?"
Do I need to declare anything I set in place of where you put "element"?
Thomas Tylek
no, it's implicitly being declared.
just as function foo(a) {return a;} doesn't need a to be declared
I clearly have much to learn.
Thomas Tylek
You'll get it man. I was just as confused as you at the start, if not more
Just keep practicing and asking questions
package addchain;

import java.util.*;

public class reeer {

    public static void main(String[] args) {

        int chainLength = Integer.parseInt(args[0]) + 1;
        int chainTarget = Integer.parseInt(args[1]);
        Random rng = new Random();
        List<Integer> chain = new ArrayList<>();

        while (true) {
            while (chain.size() != chainLength)
                chain.add(chain.get(rng.nextInt(chain.size())) + chain.get(chain.size() - 1));
            if (chain.get(chain.size() - 1) == chainTarget) {
i am getting this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at addchain.reeer.main(reeer.java:9)
can anyone help plase
Thomas Tylek
@TonyRednil Anyway, args.includes(1) evaluates to false. But that's the value you want to keep, so !args.includes(1) evaluates to true
@TonyRednil practice running through those steps and talking through each arr[0] and then arr[1] kinda like i started to show you
Ooh, @Devansh-Anhal , I don't really know Java at all but I'll try to help
@Devansh-Anhal , you do realize this is a front-end help chat room? (HTML, CSS, JS)
oh sorry :P
i am really sorry
Thomas Tylek
no need to be sorry! I just don't know how much I can help you buddy!
Mois├ęs Man
what this does ? rng.nextInt(chain.size()) @Devansh-Anhal
Santiago Fernandez
i always forget where the actual talking takes place here :/
Have a few Js doubts, im submitting some code for an interview, and some questions came up regarding style which i
d like to talk over if anyone's around :D
Style is great, since anyone can talk about style and no one is right XD
Santiago Fernandez
well, not style tbh
im just not sure about some things that i know work, but wonder if its the correct way to write it :/
like maybe im overextracting stuff into functions. i try not to compromise legibility and "ease of following wtf is going on"
but then again, DRY stabs me in the face and i HAVE to avoid repeating stuff :D