These are chat archives for FreeCodeCamp/HelpJavaScript

27th
Jan 2019
Pagnito
@Pagnito
Jan 27 00:53
cant understand why this is returning undefined, if i console log cameras inside the then block i get the filtered arr, but if console.log(this.getCameras()) i get undefined
getCameras = () => {
    let cameras;
    navigator.mediaDevices.enumerateDevices()
    .then(devices => {
      cameras = devices.filter(device=>{
        console.log(device.kind)
        return device.kind === 'videoinput';
      })
      return cameras;
    })
  };
getCameras = (cb) => {
    let cameras;
    navigator.mediaDevices.enumerateDevices()
    .then(devices => {
      cameras = devices.filter(device=>{
        return device.kind === 'videoinput';
      })
      cb(cameras)
    })
  };
solved it with a cb, is that good?
Pagnito
@Pagnito
Jan 27 01:29
it it cpu intensive if i have like a interval with 5min breaks
Riadh
@KWFE
Jan 27 03:22
anyone from europe here i want to check if https://www.udacity.com/google-scholarships started or not?
alpox
@alpox
Jan 27 07:57
@Pagnito callback works bur it would have been better to just return the promise (and return the caneras from the then
Pagnito
@Pagnito
Jan 27 07:57
@alpox ah true
Riadh
@KWFE
Jan 27 11:07
good morning anyone from europe or egype?
enestatli
@enestatli
Jan 27 12:06
@KWFE from Turkey, I do not know what is this scholarship
tundeiness
@tundeiness
Jan 27 13:04
Hi guys!. So I am attempting the drum-machine project here and I have been thinking and making research on how to approach it. Now I am getting conflicting feed backs with using AddEventListeners in React. some advise that using Event is best as event listeners are in built into react, while some are suggesting it usage. What advice do you guys have? Another thing is the state. should I still go ahead with using react to handle the state or this is the time to involve Redux? I am asking because of the number of sounds to fetch from a server and played simultaneously when the key to that sound is pressed. Looking forward to your advise.
Riadh
@KWFE
Jan 27 15:18
@enestatli i pm'd you
Kevin Peña
@kebien6020
Jan 27 17:58

@tundeiness Well if your are talking about RawElement.addEventListener(), then yes, you can use those with react. The react way of attaching an event listener to something is using the prop onClick, onChange, etc. of the component. But for the keyDown events that are probably better off attached to the whole document it may be fine to use document.addEventListener('keydown', ...).

On the state issue, I don't have experience with redux but performance wise you can do it with or without redux (I would do it without redux, there is not that much state). Thinking out loud on the design, you probably need state on the main app component to keep track of what is shown in the display, the state of the power button, the volume, and the currently selected bank. Also each key may have its own state tracking whether it should be rendering in a diferent style (for a short time when its pressed), though you probably don't need state for whether the sound is playing or not, you just trigger audioElem.play() and let it do its thing.

On the fetching of the audio files, lookup the preload attribute of the audio element, (here).

Idowu Wasiu
@Hoxtygen
Jan 27 18:12
Hello guys. I have a database problem
I need to create a database for storing users. There are going to be two types of users; Admin and normal user... I'm supposed to create a column named isAdmin which will be boolean. Is there a way I can create the column where everything default to false? Users will be filling a form to register, I want the isAdmin column to be false whenever a new user registers
Brad
@bradtaniguchi
Jan 27 18:36
@Hoxtygen Yes you can set the default value for a table when defining an SQL table. (in NOSQL you can too, but since there is no schema, you have to enforce it in code using mongoose or manually)
Idowu Wasiu
@Hoxtygen
Jan 27 18:37
@bradtaniguchi I'm using postgresql
Brad
@bradtaniguchi
Jan 27 18:37
Yea, thats SQL, check the docs on the syntax to set default values for columns when defining a table.
Idowu Wasiu
@Hoxtygen
Jan 27 18:43
@bradtaniguchi can the default value be changed for individual user later?
Pagnito
@Pagnito
Jan 27 18:47
hey, how would i got about building like a cluster of chatrooms with websockets
Kevin Peña
@kebien6020
Jan 27 18:50
@Hoxtygen Also if you are using some kind of ORM to access the database usually in the model or in the migration there is some way to specify default values (and constraints like UNIQUE or NOT NULL)
Pagnito
@Pagnito
Jan 27 18:51
oh nvm i figured it ot
Idowu Wasiu
@Hoxtygen
Jan 27 19:05
@kebien6020 i'm not using ORM
@kebien6020 What I really want to know now is if I set the default value of isAdmin column to false, can I flip the value to true for individual user later if I want to make a normal user an admin?
Kevin Peña
@kebien6020
Jan 27 19:07
@Hoxtygen Yes. That is how it works.
Idowu Wasiu
@Hoxtygen
Jan 27 19:08
@kebien6020 thanks man.
Kevin Peña
@kebien6020
Jan 27 19:11
@Hoxtygen The effect of making a column definition like isAdmin boolean DEFAULT FALSE is just so that when you INSERT INTO users (name, password, other) VALUES('a', 'b', 'c') it succeeds and the value inserted in the isAdmin colum is FALSE, if the column didn't any default value it would insert NULL. You can override the default value by specifying it in the INSERT query or in an UPDATE query.
Idowu Wasiu
@Hoxtygen
Jan 27 19:12
@kebien6020 oh man thanks
@kebien6020 people like you, @JLuboff @bradtaniguchi make this place a home for those with problems like me.... thanks to y'all
Brad
@bradtaniguchi
Jan 27 19:26
@Hoxtygen np :D
Idowu Wasiu
@Hoxtygen
Jan 27 20:05
@kebien6020 the default value works, and I actually add users without including the isadmin column and it defaulted to false automatically..... I havent gotten to the part where i have to edit individual user isAdmin column. Now as it stands i'm happy.... thank you
Kevin Peña
@kebien6020
Jan 27 20:06
@Hoxtygen np, glad I could help
tundeiness
@tundeiness
Jan 27 20:24
@kebien6020 that's quite a lot! And thank you as well. So i am crossing redux out of this project. However in my project i am not including volume or power button and bank as well. instead I have 2 new features: one will record the keys/drums pressed when the record button is selected and there is a play button which plays the recorded sounds. Now my approach is to save the sounds in an array when the drum pads a clicked and then run a loop over this array when the play button is selected to play these sounds. What do you think?
Kevin Peña
@kebien6020
Jan 27 20:32
@tundeiness Sounds really cool!. That is a bit harder though, to record sounds you are going to need the new MediaRecorder API, it is relatively new so the support is not quite there, but firefox and chrome have had support for it since a few years, chrome for android got it in december 2018. Here is an article explaining how you could do it, without a server even.
tundeiness
@tundeiness
Jan 27 20:49
@kebien6020 thanks for the heads up. Now let me dig in to see how I can implement this.
tundeiness
@tundeiness
Jan 27 21:05
@kebien6020 I read the document you posted but you know this project is not recording any sound from a microphone, instead it is just taking note of which sound was pressed by the user and then storing it to be played altogether when the command is given and as such I do not think it will be that windy like the approach used in the document you posted though.
Kevin Peña
@kebien6020
Jan 27 21:20
@tundeiness Oh I missunderstood you, my bad. That makes more sense. So a recording would be like an array of {key: 'Q', timeOffset: 300 /* in ms */}.
tundeiness
@tundeiness
Jan 27 21:28
@kebien6020 yeah...something like that
Riadh
@KWFE
Jan 27 22:33
hey
anyone is here?
@bradtaniguchi
Brad
@bradtaniguchi
Jan 27 23:00
@KWFE helo
@KWFE I see you've been asking help rather often for most challenges, I'd consider going back and redoing the ones you have asked help for to make sure you understand the concepts. Repeition is key to making sure you remember. Keep doing it and you'll ingrain it into memory.
Riadh
@KWFE
Jan 27 23:13
@bradtaniguchi i am redoing them and i asked just for the card counting one
Riadh
@KWFE
Jan 27 23:25
var count = 0;

function cc(card) {
  // Only change code below this line

  switch (card){

    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      return count+1;
    break;

    case 7:
    case 8:
    case 9:
      return count + 0;
    break;


    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      return count-1 ;
    break;

  }

  if (count > 0){
    return count + " Bet";
  }else if (count == 0 ){
    return count + " Hold"
  }else if (count < 0){
    return count + " Hold";
  }

  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
plz just a hint don't give me the solution
Brad
@bradtaniguchi
Jan 27 23:46
@KWFE Doing return right before the break is redundant, if you have a return you don't need break, as it will return from the function automatically
same goes for the if/else statements, you can just us if and returns for the exact same reason
so the back part of your code can be changed to:
if (count > 0){
  return count + " Bet";
}
if (count == 0 ){
  return count + " Hold"
}
if (count < 0){
  return count + " Hold";
}