These are chat archives for FreeCodeCamp/HelpJavaScript

14th
Jan 2018
Andrew Dowling
@LBOD2000
Jan 14 2018 02:48
function diffArray(arr1, arr2) {
  var newArr = [];
  var funny = [];

  for (i=0; i<arr1.length; i++){
    var arr1Test = arr1[i];
    for (j=0; j<arr2.length; j++){
      if (arr1Test == arr2[j]){
        newArr.push(arr2[j]);
      } 
    }
      }

  arraysConcat = arr1.concat(arr2);

    filteredArr = arraysConcat.filter(
      function(val) {
      for (l=0; l<newArr.length; l++) {
        if (val == newArr[l]){
         return false;
         }
        else {return true;}
         }
      }     
    );


  funny.push(filteredArr);

  return funny;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
I know the problem is in the filter function. I have tried it three different ways. It currently returns [2,3,4,5,2,3,5]. Can someone please give me a hint.
Gersho
@Gersho
Jan 14 2018 02:53
could you link the challenge ? i don't remember what you're supposed to do on that one
Andrew Dowling
@LBOD2000
Jan 14 2018 03:38
yea the challenge is Diff two arrays https://www.freecodecamp.org/challenges/diff-two-arrays
Ken Haduch
@khaduch
Jan 14 2018 05:12
@LBOD2000 - I did something like this - find the unique values by comparing (filtering) arr1 against arr2, then doing the reverse - filtering arr2 against arr1. After that, concatenate the two arrays that result (with the unique values from each input array) and return that.
With the code that you are using, you're comparing each element of arr2[j] against arr1Test (for example, in your first loop) so you are finding the values that are common in each one, instead of the differences. Your technique will not be correct for finding the differences.
OOPS - i just looked at the second half of your code - you are still doing a similar thing there - you are comparing each element in the newArr against each element in arraysConcat, so you have multiple chances to find things that match and things that don't. You really only want one answer - you want to find the numbers that either do exist or don't exist (depending on whether or not you're trying to remove elements or keep them in your final array). You don't want to get multiple answers for each value. Using something like .indexOf() to search for a value is a better option.
Andrew Dowling
@LBOD2000
Jan 14 2018 07:42
thanks
Onome Sotu
@onomesotu
Jan 14 2018 12:19
haha.. i just combined both arrays , sorted and checked if two consecutive items were the same :)
Justin
@justoX
Jan 14 2018 12:46

What do I add to this :

function getIndexToIns(arr, num) {
  arr.sort(function(a, b) {
  return a - b;});

  for(var i=0; i<arr.length; i++){
    var expr = true;
    switch (expr) {
      case (num>=arr[i] && num<=arr[i+1]):
        num=i+1;
      break;
      case (num>=arr[arr.length-1]):
        num = arr.length;
      break;
}  

  }
  return num;
}

to get this:

0

when I run this:

getIndexToIns([3, 10, 5], 3)
?
Fabien SHAN
@X140hu4
Jan 14 2018 12:48
return 0 at the end?
Can you provide the link to the challenge?
Gersho
@Gersho
Jan 14 2018 12:49
those switch/case...what are you trying to do ?
mitchell369
@mitchell369
Jan 14 2018 13:06
Hey guys, I have a question about using the .every method. Does that only access values in an array or can it be used to evaluateindividual characters within a string?
Gersho
@Gersho
Jan 14 2018 13:10
hmm i think you'll have to use for...of and itterate the string, as far as i know .every is for arrays
Fabien SHAN
@X140hu4
Jan 14 2018 13:59
@justoX How would you do it if you did it by hand?
Breakdown your thought process
Stephen James
@sjames1958gm
Jan 14 2018 14:05
@mitchell369 You can steal .every from an array and apply it to a string
[].every.call("test", function(e) { console.log(e); return true })
omgallen
@omgallen
Jan 14 2018 14:23

Hi guys, please help with code? I really don't know how to modify it.function multiplyAll(arr) {
var product = 1;
// 请把你的代码写在这条注释以下
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j <arr.length; j++)
product *= arr[i][j];
}
// 请把你的代码写在这条注释以上
return product;
}

// 你可以修改这一行来测试你的代码
multiplyAll([[1,2],[3,4],[5,6,7]]);

Gersho
@Gersho
Jan 14 2018 14:30
you are missing some { }
Stephen James
@sjames1958gm
Jan 14 2018 14:32
@omgallen Your {} are okay.
Your second for loop is using the wrong value to test
@omgallen You need to use arr[i].length
Gersho
@Gersho
Jan 14 2018 14:34
you don't need braces on the for j if there's only one statement ?
omgallen
@omgallen
Jan 14 2018 14:34
@sjames1958gm , I see, clever
Stephen James
@sjames1958gm
Jan 14 2018 14:34
@Gersho You don't but it is not recommended as it causes confusion, esp. if you later add another statement and forget the {}
Gersho
@Gersho
Jan 14 2018 14:35
i see, thanks @sjames1958gm
CamperBot
@camperbot
Jan 14 2018 14:35
gersho sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8854 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Stephen James
@sjames1958gm
Jan 14 2018 14:36
@Gersho You could even do this: because the second for loop is one statement inside the first for loop
for (var i = 0; i < arr.length; i++)
   for (var j = 0; j <arr[i].length; j++) 
      product *= arr[i][j];
Gersho
@Gersho
Jan 14 2018 14:37
eww
i'll keep it in mind...and never use it :D
Ken Haduch
@khaduch
Jan 14 2018 14:50
@gersho - Yes, the recommendation from places where you read about coding practices is to use the curly braces, because as @sjames1958gm says, it can cause problems if you add another statement and were expecting it to be part of the "if" conditional clause. It's one of the things that are probably best avoided...
Francois Beyers
@Blusoup
Jan 14 2018 16:49
Hello everyone! Can anyone recommend learning material for JavaScript? What has been the best way for you to learn and understand JavaScript? The reason I’m asking, I have just started a Web Developer role and it’s a completely new field. I knew the basics of JavaScript, but this is a whole new level for me. I have a bit of imposter syndrome and just want to do as good as I can at this job. I’ve always wanted to be a web developer and now that I am I feel like I can’t do the job. Any advise on learning would be great! Thank you 🙏🏼
Gersho
@Gersho
Jan 14 2018 16:58
i haven't read it but people often recommend "you don't know JS"
Francois Beyers
@Blusoup
Jan 14 2018 16:58
@Gersho I’ve also heard of this book
Ayush Bahuguna
@relentless-coder
Jan 14 2018 17:04
@relentless-coder
Hey, I need some help here. I was browsing the gsap library, and I came across this example, can anyone tell the concept behind this scroll?
http://www.yebocreative.com/en/hey/
how are they able to show different sections as if the page is being scrolled?
Christopher
@bradley1492
Jan 14 2018 17:57
Hello,
just out of interest, is it common and possible, to remove all loops in favor of filter, map and reduce in everyday coding life?
Stephen James
@sjames1958gm
Jan 14 2018 18:18
@bradley1492 I think it is more common these days to use these in place of loops. Of course they require arrays, so if you have a loop
that is not array based you probably need a different loop, although functional programming purists would probably disagree
FlashHero
@FlashHero
Jan 14 2018 18:23
Finally done with simon game https://codepen.io/flash_boy/pen/PEBGym would appreciate if you checked if everything works and I'm curious where you got your wrong click sound
Aditya
@ezioda004
Jan 14 2018 18:24
@bradley1492 Majority of developers prefer to use FP concepts since they are pure function so less errors in future, also less lines of code most of the time. This was a good watch on this subject.
Christopher
@bradley1492
Jan 14 2018 18:29

@sjames1958gm @ezioda004 Okay thanks for your answers, @ezioda004 I already know the funfunfunction video :), it sort of brought me to my question about this topic. It just seems difficult from time to time, to imagine that some people don't use loops at all. At least for the fcc challenges, with a beginner to indermediate skillset in mind :)

I am just stuck in a situation in which I don't know on how to come from loops to reduce. I think I have to ask this anyway here in a few minutes...

CamperBot
@camperbot
Jan 14 2018 18:29
bradley1492 sends brownie points to @sjames1958gm and @ezioda004 :sparkles: :thumbsup: :sparkles:
:cookie: 382 | @ezioda004 |http://www.freecodecamp.org/ezioda004
:star2: 8855 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Aditya
@ezioda004
Jan 14 2018 18:38
@bradley1492 I'm also in the same situation, I try to use as much higher order functions as possible, but sometimes I end up using good'ol for loop, but I think with time (hopefully) I'll be more comfortable using them.
Christopher
@bradley1492
Jan 14 2018 18:42

In the https://www.freecodecamp.org/challenges/symmetric-difference challenge I managed to work out the criteria for two arrays.

Here's my pen on codepen:
https://codepen.io/bradley1492/pen/jYKXRe

In my code I work with the includes-method in combination with filter to determine if one array includes a value of the other one.
Now I know that my next step in the challenge is to find out, how I can "scale" these steps to multiple arrays.
But I don't really have a clue on how to continue.
How can I check values of arrays against each other with the includes method no matter how many arrays are involved?

I guess, it comes down to reduce, but I am not quite sure, on how I would construct this...

@ezioda004 Yeah, in the pen I just posted, there is a for-loop included that I couldn't avoid :)

for(let i = 0; i < args.length; i++){
    let remove = args[i].filter(function(item, pos){
      return args[i].indexOf(item) == pos;
    })
    place.push(remove);
  }

This is sort of the thing in which I don't know how to avoid a loop.

Just scanning over multiple arrays.

Stephen James
@sjames1958gm
Jan 14 2018 18:55
@bradley1492 You can replace the for loop with
args.forEach()
To make it more functional
Christopher
@bradley1492
Jan 14 2018 19:04
@sjames1958gm Oh, isn't forEach() considered a loop as well? But still better than the plain extended for-loop then
But thanks I will certainly do that! :)
Stephen James
@sjames1958gm
Jan 14 2018 19:05
@bradley1492 They are all loops in a sense just whether you want functional loops or not.
You could use reduce on arguments and eliminate both
[].reduce.call(arguments, function(a, c) {})
https://github.com/sjames1958gm/freecodecamp/blob/master/symdiff2.js
If you want to look at a reduce implementation
Christopher
@bradley1492
Jan 14 2018 19:07

@sjames1958gm Okay, than I'll take the functional :)

[].reduce.call(arguments, function(a, c) {})is for my challenge example is it? :)
Yes I have to look into the that. What is call?

@sjames1958gm OMG this is all it is? How much less code it is in comparison to my approach
hahahahaha :)
Stephen James
@sjames1958gm
Jan 14 2018 19:09
@bradley1492 in this case call invokes reduce (it is a function on the reduce function object)
and the first value passed to call is the "this" value for the call
arguments looks enough like an array that reduce can process it as an array (has length, and properties accessed by numbers
@bradley1492 As you get more comfortable with reduce, filter and map, the amount of code you write will shrink
Aditya
@ezioda004
Jan 14 2018 19:12
@sjames1958gm Thats really neat solution
Christopher
@bradley1492
Jan 14 2018 19:13
@sjames1958gm Yes it's just really impressive seeing this for the first time, and realizing how much one can do with experience.
Aditya
@ezioda004
Jan 14 2018 19:13
Mines a lot messy compared to yours
function sym(args) {
  var newArr;
  var finalArr = [];
  var arr = Array.prototype.slice.call(arguments);
  arr.reduce(function(a, b){
    newArr = [];
    a.forEach(function(val){
      if (b.indexOf(val) < 0 && newArr.indexOf(val) < 0){
        return newArr.push(val);
      }
    });
    b.forEach(function(val1){
      if (a.indexOf(val1) < 0 && newArr.indexOf(val1) < 0){
        return newArr.push(val1);
      }
    });
  return newArr;
  });
If I hadnt seen your solution I couldnt refactor more
Christopher
@bradley1492
Jan 14 2018 19:15
@sjames1958gm I'll work on it tomorrow, thanks for the share :) bye
CamperBot
@camperbot
Jan 14 2018 19:15
bradley1492 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
api offline
Aditya
@ezioda004
Jan 14 2018 19:15
@bradley1492 Oh I saw your code, you can least remove sort() since you dont need it in this challenge :D
Stephen James
@sjames1958gm
Jan 14 2018 19:23
@ezioda004 Anytime I see a loop with a push in it I think map or filter (filter if there is an if condition around the push)
Aditya
@ezioda004
Jan 14 2018 19:28
@sjames1958gm Thats a good suggestion. I'll refactor foreach part, thanks.
CamperBot
@camperbot
Jan 14 2018 19:28
ezioda004 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8856 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Christopher
@bradley1492
Jan 14 2018 19:36
@ezioda004 Thx for that tip
CamperBot
@camperbot
Jan 14 2018 19:36
bradley1492 sends brownie points to @ezioda004 :sparkles: :thumbsup: :sparkles:
:cookie: 383 | @ezioda004 |http://www.freecodecamp.org/ezioda004
FlashHero
@FlashHero
Jan 14 2018 20:55
Where did y'all get your wrong click sound for simon game ? Freecodecamp only provides four sounds
iso
@iso1048
Jan 14 2018 23:30
@FlashHero I didnt use one (just had the flashing !!)
Robert Marić
@Mariciano95
Jan 14 2018 23:39
function mutation(arr) {
  var letters = arr[1].toLowerCase().split(''),
      word = arr[0].toLowerCase();

  for (var i = 0; i < letters.length; i++) {
    if (word.indexOf(letters[i]) !== -1) {
      console.log('good');  
    } else {
      return false;
    }
  }
  return true;
}

mutation(["hello", "hey"]);
Guys this works but I feel it can be better
Aditya
@ezioda004
Jan 14 2018 23:47
@Mariciano95 You dont need to split() arr[1], you can just loop through arr[1]since its a string
Robert Marić
@Mariciano95
Jan 14 2018 23:52
so I just need to make sure they are all lower case?
Aditya
@ezioda004
Jan 14 2018 23:54
Yep, otherwise its good solution
Robert Marić
@Mariciano95
Jan 14 2018 23:55
it works, thanks! Yeah my other was awful :confounded: I guess I can be pleased with this one