These are chat archives for FreeCodeCamp/HelpJavaScript

1st
May 2018
Boris
@Boris1011
May 01 2018 02:08
Hi everyone, would anyone know anything about vue.js and defaults??
Norvin Burrus
@ndburrus
May 01 2018 02:31
Boris
@Boris1011
May 01 2018 02:41
Thanks @ndburrus
a slightly more specific question if I may...
Is there a way to default a v-select to another, when they are using the same :items? so when Fred gets selected for the first selection, Fred will default in the second select
if that makes some sense, hopefully...
Norvin Burrus
@ndburrus
May 01 2018 02:55
@Boris1011 are we setting the default value of a select tag, or creating two-way data bindings on form input elements?
Boris
@Boris1011
May 01 2018 03:05
well, a bit of both if that is possible...???
I suppose setting the default value, by using two-way data binding???
ehutchllew
@ehutchllew
May 01 2018 03:22
I was looking at some Vue today, reminded me too much of AngularJS and it gave me shivers.
anthonygallina1
@anthonygallina1
May 01 2018 03:28
@ehutchllew So you wouldnt recomend it. was on my to do BOOK LOL
list got a little unwieldy
:)
ehutchllew
@ehutchllew
May 01 2018 03:30
@anthonygallina1 Eh I dunno, i JUST scratched the surface. I am definitely going to revisit it, however, I can say that I absolutely love React.
anthonygallina1
@anthonygallina1
May 01 2018 03:30
:thumbsup:
Daniel Simeonov
@dbsimeonov
May 01 2018 08:06
Guys what options I have if I have a shopping list, and I want to filter it by price/name. I need to do html data attributes and to store them into array?
Michael S
@Ohshi60
May 01 2018 08:09
var http = require('http')

var counter = 0;
var results = [];

function printData(){
  for(var i = 0;i < 3; i++){
    console.log(results[i])
  }
}

function httpGet(index){
  var buffer = ""
  http.get(process.argv[2+index], function(response){
    response.on("data", function(chunk){
      buffer += chunk
    })
    response.on("error", function(err){
      return err
    })
    response.on("end", function(){
      results[index] = buffer.toString()
      counter++;
    })
    if (counter === 3){
        printData()
    }
  })
}

for (var i = 0; i < 3;i++){
    httpGet(i)
}
my counter++ doesnt work, any ideas?
Michael Grienauer
@mgrienauer
May 01 2018 08:35
hey can anyone explain the advantages of using get and set in js? why wouldnt i just define a function in an object as usual
Michael S
@Ohshi60
May 01 2018 08:44
@mgrienauer Getters and setters encapsulate the fields of a class by making them accessible only through its public methods and keep the values themselves private. ... Getter and setter method are used to get and set the value of x which is the way to achive encapsulation.
Michael Grienauer
@mgrienauer
May 01 2018 08:50
@Ohshi60 so if i just declared a function within an object, you're saying a user would be able to publicly access everything in the function, whereas with a getter/setter they'd only be able to see the name of the function?
Michael S
@Ohshi60
May 01 2018 08:51
i think youre confusing some things. You use getters and setters for fields not functions
Michael Grienauer
@mgrienauer
May 01 2018 08:53
from MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get:
The get syntax binds an object property to a function that will be called when that property is looked up.
Michael S
@Ohshi60
May 01 2018 08:57
@mgrienauer my bad, i learned to use them in conjunction with fields i C#. But i still imagine that their function is the same. You do it in order to encapsulate fields.
Michael Grienauer
@mgrienauer
May 01 2018 08:59
@Ohshi60 still not 100% on them but thanks
Michael S
@Ohshi60
May 01 2018 09:00
but say you have a function(or variable). If that function needs to be used by other parts of the program, the code needs to be public. But this exposes the whole field to errors. By using getters and setters you are defining how the function/field is exposed(and can thus also do validation) and properly encapsulating the field/function
Michael Grienauer
@mgrienauer
May 01 2018 09:01
@Ohshi60 by field, do you just mean the variable / function / whatever?
Michael S
@Ohshi60
May 01 2018 09:01
yes
Michael Grienauer
@mgrienauer
May 01 2018 09:04
@Ohshi60 so using get or set is kind of like validating how an object property is accessed (like checking values, truthy, falsy, etc) and either putting it in the object or retrieving the value based on the validation
kind of like using a function to validate a field, without having to explicitly write a function that could pollute the scope?
Michael S
@Ohshi60
May 01 2018 09:07
say we have a name attribute. If the field was public, anything would be able to access and thus change the field. So generally you want to encapsulate the field as private and then expose the field with a get and set function. So when we retrieve the field we use the getter and when we set a new value we use the setter. Since we write the code that the setter uses we can also put validation here.
Daniel Simeonov
@dbsimeonov
May 01 2018 09:09
guys weird question but that might be possible, any chance I can do the following - I have 10 boxes and each box has element which is nested deep with a value I need. Can I access the parent element with specific class I want in that case box ? Or I need to do it with for loop and keep track on the index?
Michael Grienauer
@mgrienauer
May 01 2018 09:10
@Ohshi60 so in that case lets say i only want the name attribute to be accessed by a function changeName(name). you're saying id use a getter to validate that the function accessing name is equal to changeName() and the setter to validate that the function changing the name attribute is changeName()
Michael S
@Ohshi60
May 01 2018 09:17
@mgrienauer I apologize im getting a bit confused. You would use a getter to retrieve information about the attribute for example getName(user) or something
setters are used for modifying the attribute
setName() or whatever
Michael Grienauer
@mgrienauer
May 01 2018 09:18
@Ohshi60 thanks
@Ohshi60 i think im wrapping my head around it
Michael S
@Ohshi60
May 01 2018 09:18
you can also define functions as setters i saw in the docs you linked
i mean getters
Daniel Simeonov
@dbsimeonov
May 01 2018 15:46
guys are there any easy/short ways with js to detect a click outside its container?
Christopher McCormack
@cmccormack
May 01 2018 15:53
@dbsimeonov onclick will actually propagate all the way up to the document level, what precisely are you trying to do?
Daniel Simeonov
@dbsimeonov
May 01 2018 15:54
@cmccormack I think I found a solution on stackoverflow. Simply I have few dropdown menus that been triggered on click, and would like to remove the class if user clicks outside the menu
Christopher McCormack
@cmccormack
May 01 2018 15:57
I'm guessing you add a click handler to document.body that clears it?
Daniel Simeonov
@dbsimeonov
May 01 2018 15:57
I think I can do a eventListener with click that has something likeif(!element) ?
Christopher McCormack
@cmccormack
May 01 2018 16:02
yeah you should be able to add an eventlistener to document and check event.target
Daniel Simeonov
@dbsimeonov
May 01 2018 16:03
Will try bit later cus need to go, tnx ;)
Christopher McCormack
@cmccormack
May 01 2018 16:05
@dbsimeonov this example should work for what you need to do I presume
Mois├ęs Man
@moigithub
May 01 2018 16:12
@dbsimeonov u could use event bubbling (or something like that)
just set on parent /container a click listener
Kaz Baig
@kbaig
May 01 2018 17:39
Anyone done Heap's permutation algorithm?
Christopher McCormack
@cmccormack
May 01 2018 17:41
@kbaig pretty sure I used it for one of the FCC exercises
I'll check
Brad
@bradtaniguchi
May 01 2018 17:41
It is, but idk how it works, I remmeber thinking it was heap sort haha
Christopher McCormack
@cmccormack
May 01 2018 17:42
yeah used it for No Repeats Please
// Using Heap's Algorithm for finding permutations.
//   https://en.wikipedia.org/wiki/Heap's_algorithm
function permAlone(str) {

  var arr = str.split(""),
      perms = [],
      noRepeats = [];

  function heaps (n) {
    if ( n === 1 ) {
      perms.push(arr.join(""));
    }

    for ( var i = 0; i < n; i++ ){
      heaps(n - 1);
      if (n % 2 === 0){
        swap(i, n-1, arr);
      } else {
        swap(0, n-1, arr);
      }
    }
  }

  function swap(fromi, toi){ 
    var temp = arr[fromi]; 
    arr[fromi] = arr[toi];
    arr[toi] = temp;
  }

  heaps(arr.length);

  for (var i = 0; i < perms.length; i++){
    if (!/(.)\1/.test(perms[i])){
      noRepeats.push(perms[i]);
    }
  }
  return noRepeats.length;
}
Daniel Simeonov
@dbsimeonov
May 01 2018 17:47
@ndburrus @cmccormack @moigithub thanks guys!
Kaz Baig
@kbaig
May 01 2018 17:52
@cmccormack thanks!
Christopher McCormack
@cmccormack
May 01 2018 17:52
np
Norvin Burrus
@ndburrus
May 01 2018 19:56
@dbsimeonov yw :+1:
anthonygallina1
@anthonygallina1
May 01 2018 20:10
:sparkles:
RicardoLFoco
@RicardoLFoco
May 01 2018 22:10
Hey guys, I'm stuck on the twitch API project. Trying to use a for of loop to call the API and push the channel ID onto a previously declared array. The array doesn't seem to exist outside of the loop though. Anyone encounter a similar problem?
Christopher McCormack
@cmccormack
May 01 2018 22:13
@RicardoLFoco what you are running into is asynchronous code. API calls are asynchronous (out of order from the rest of your code) so after you kick off an async call, your code continues running to the next lines in sequence. If you are printing your array and it is empty, it's because that array was printed before any of your asynchronous calls actually returned
RicardoLFoco
@RicardoLFoco
May 01 2018 22:24
@cmccormack Yes thank you for reminding me!