These are chat archives for FreeCodeCamp/HelpJavaScript

18th
Jan 2018
Nerando Johnson
@Nerajno
Jan 18 2018 00:13
greetings members of the Jedi council
i need you assistance to figure out something
var channels = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx",
  "RobotCaleb", "noobs2ninjas", "brunofin", "comster404", "RealKraftyy?"
];


var apiSegmentOne = "https://wind-bow.glitch.me/twitch-api/streams/";

var freecodecampChecker = apiSegmentOne + channels[0];
$.getJSON(freecodecampChecker, function(data) {
  console.log(freecodecampChecker);
  var streamStatus = data.stream;
  if (streamStatus === null) {
    console.log("Aint Wrking");
  } else {
    console.log("Working");
  $(" fa fa-times-circle").css("color","green");// Correct method to change color of Css
  }
});
can someone point out the correct method to change the color of my css
via Js
FlashHero
@FlashHero
Jan 18 2018 00:24
@Chrono79 thanks, forgot you can do that in es6 lol
CamperBot
@camperbot
Jan 18 2018 00:24
flashhero sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4606 | @chrono79 |http://www.freecodecamp.org/chrono79
Stephen James
@sjames1958gm
Jan 18 2018 00:24
@Nerajno aren't fa and fa-times-circle classes so you need .
@Nerajno $(".fa-times-circle").css("color","green"); probably all you need is the second class
Nerando Johnson
@Nerajno
Jan 18 2018 00:44
thanks @sjames1958gm
CamperBot
@camperbot
Jan 18 2018 00:44
nerajno sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8866 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Kaz Baig
@kbaig
Jan 18 2018 06:33

Interesting JS snippet making the rounds:

const a = {};
a.abuseCoercion = 0;
a.valueOf = function() {
  this.abuseCoercion++;
  return this.abuseCoercion;
}

console.log(a == 1 && a == 2 && a == 3); // true

It was from an interview question asking, "Can (a == 1 && a == 2 && a == 3) ever evaluate to true?"

Pieter Stokkink
@forkerino
Jan 18 2018 08:15
@kbaig I remember using a similar trick in a codewars kata.
Blauelf
@Blauelf
Jan 18 2018 11:06
@forkerino Is that the one where you can simply freeze an object and defuse ten times?
Blauelf
@Blauelf
Jan 18 2018 11:14
Not that simple it seems.
Pieter Stokkink
@forkerino
Jan 18 2018 11:21
I took the 'official' route, but am sure there's a shortcut.
Blauelf
@Blauelf
Jan 18 2018 11:24
@forkerino Did the stormy weather hit you in the Netherlands, too? Just read there are no trains at the moment here in the west of Germany. There's a tram in front of the house not moving for hours, sirens all the time, a bit apocalyptic feeling.
Markus Kiili
@Masd925
Jan 18 2018 11:28
@forkerino Doing Codewars katas make me blow off even without any such kata. Descriptions on edge cases are often vague and I have no patience to chase it down.
Ankur sharma
@ankur1163
Jan 18 2018 11:36
btw
is it benefecial to learn c?
considering, if i create web apps
I am happy man
i am not talking only about learning, but mastering it to some level
which might require hours and hours of practice
Markus Kiili
@Masd925
Jan 18 2018 11:43
@ankur1163 I don't think that C is used much in web apps.
Ankur sharma
@ankur1163
Jan 18 2018 11:44
yeah
a true coder is someone who knows c or c++ , atleast?
web app developers are not real developer
as they dont know whats going under the hood?
is this true ?
Markus Kiili
@Masd925
Jan 18 2018 11:46
Doing some C might give you some general knowledge and understanding of how computers work, but doing it as a job means that you need to put in a lot of effort so you can produce good code and secure applications without memory leaks.
So either taste it or give your soul to it .
:imp:
Stephen James
@sjames1958gm
Jan 18 2018 11:48
by that argument if you haven't programmed in assembly you aren't a real developer?
Blauelf
@Blauelf
Jan 18 2018 11:54

@ankur1163 C is uncommon in web apps. C# is somewhat common in backend (where I am it's less used than Java, but much more common than PHP or Python). C++ is often used in regular software, many GUI libraries are written in C++ and have adapters for other languages.

C definitely tells you something about memory, efficient structures, scopes (why not to clutter them) and so on. But for example JavaScript with its closures has its own category of memory leaks, and you simply cannot dereference rogue pointers as there are no pointers, so while you can transfer some knowledge to another language, it would help you most if you had projects actually using C/C++ or a similar language.

Markus Kiili
@Masd925
Jan 18 2018 11:54
@sjames1958gm Assemblers are for wimps that don't understand how computers work.
Stephen James
@sjames1958gm
Jan 18 2018 11:54
@Masd925 lol
Blauelf
@Blauelf
Jan 18 2018 11:54
@sjames1958gm Bah, writing binary by pushing the bits under the microscope :P
Stephen James
@sjames1958gm
Jan 18 2018 11:55
@Blauelf Well in my day I did program a computer with the switches on the front panel, does that count?
Blauelf
@Blauelf
Jan 18 2018 11:56
I only had microswitches on the ISA cards (to select the memory range they'd map their memory into), and microswitches on SCSI drives (to select communication mode and ID). We called those (most closely translated) "mouse piano".
But yeah, that's pretty close. Probably as close as one can get.
Pieter Stokkink
@forkerino
Jan 18 2018 12:01
@Blauelf yeah, we got 120km winds here as well. No air traffic, lots of trains cancelled, roads are a mess. But it seems to quiet down a bit now.
Blauelf
@Blauelf
Jan 18 2018 12:02
Here, too. Train went to the depot. Hope they will resume service today, otherwise it's a 1.5h walk :D
Pieter Stokkink
@forkerino
Jan 18 2018 12:03
@Masd925 I prefer the katas that actually teach you something about programming, I've done a couple that were really cool, like coding an evolutionary algorithm which implements some simple machine learning concepts.
@Blauelf 1,5 hours in this weather can be a challenge.
@Blauelf in December I had a 1,5 hour bike ride through thick snow. That was not fun.
instead of the usual 45 mins
Blauelf
@Blauelf
Jan 18 2018 12:53
Oh, umm... guess we're lucky then.
Pieter Stokkink
@forkerino
Jan 18 2018 13:40
:smile:
I'm lucky I don't have to go outside today.
Then again, during our last storm, they interviewed a couple of American tourists (from one of the Hurricane states) during the peak and they asked when it was going to start. When told that this was it, they just started laughing.
Long Nguyen
@longnt80
Jan 18 2018 14:25
how do I print the event object to html document (inside a div for example)?
Naris R
@KittenHero
Jan 18 2018 14:27
If you're using chrome dev console then you can inspect objects printed through console.log?
Long Nguyen
@longnt80
Jan 18 2018 14:27
yeah I know
Naris R
@KittenHero
Jan 18 2018 14:27
which events are you trying to print?
Long Nguyen
@longnt80
Jan 18 2018 14:27
I want to print it on the document though
any event
Marianissimus
@Marianissimus
Jan 18 2018 14:27
you want to change the content of the document? @longnt80
Naris R
@KittenHero
Jan 18 2018 14:28
document.write(JSON.stringify(event))
Long Nguyen
@longnt80
Jan 18 2018 14:28
yeah, it only write the first property
@Marianissimus no I want to print out the event object to the document
Naris R
@KittenHero
Jan 18 2018 14:31
well, you can just do document.write(event) but then you can't see the contents
Long Nguyen
@longnt80
Jan 18 2018 14:31
@KittenHero it only prints out the first property
Mois├ęs Man
@moigithub
Jan 18 2018 14:32
if i recall..... there are something named "enumerables" properties/methods ...
Long Nguyen
@longnt80
Jan 18 2018 14:32
what is that?
Naris R
@KittenHero
Jan 18 2018 14:41
I see, the problem is that the properties defined in object.prototypes aren't being printed
Naris R
@KittenHero
Jan 18 2018 14:55
try this

for (let prop in event) {
    document.write(`${prop}: ${event[prop]}`);
}
Christopher
@bradley1492
Jan 18 2018 16:31
@Blauelf Thanks for the explanation of the dollar to cent conversion :)
CamperBot
@camperbot
Jan 18 2018 16:31
bradley1492 sends brownie points to @blauelf :sparkles: :thumbsup: :sparkles:
:star2: 4675 | @blauelf |http://www.freecodecamp.org/blauelf
Nazar
@IsaakNazar
Jan 18 2018 16:32
hey guys currently I'm struggling on calculator project, and how to add two numbers in one input field?
Jason Luboff
@JLuboff
Jan 18 2018 16:32
@IsaakNazar Personally I used an array to store all values and then upon the user pressing the = button, I would run .eval Forewarning, .eval is not very secure but I don't think its an issue for this project
Nazar
@IsaakNazar
Jan 18 2018 16:40
@JLuboff ok :+1:
Christopher
@bradley1492
Jan 18 2018 16:44

In the https://www.freecodecamp.org/challenges/exact-change challenge.
Would a concept of converting dollars to pennies and then back as a result, look essentially like this?

function checkCashRegister(price, cash, cid) {
  let change = [];
  let base = 0;
  for(let i = 0; i < cid.length; i++){
   let test = Math.round(cid[i][1]*100);
   base += test;
  }

  console.log(base/100);

  return change;
}

First multiplying by 100 and then for the resulting value dividing by 100?

jusgoose
@gusd773
Jan 18 2018 17:04

any body help? I am not sure why it won't return false

function mutation(arr) {
var compare1 = arr[0].toLowerCase();
var compare2 = arr[1].toLowerCase();

var arr2 = compare2.split("");
var empty= [];
for (var i=0; i<arr[1].length; i++){ if (compare1.indexOf(arr2[i])>-1){

      empty.push(arr2[i]);
      }
    else {
      return;
      }

}

if (empty.length==compare2.length){
return true;
}
else {
return false;
}

}

mutation(["hello", "hey"]);

Ken Haduch
@khaduch
Jan 18 2018 17:06
@gusd773 - for code posting, you can go back and edit your post, and put three backticks ``` on separate lines
before and after your code. And what @sjames1958gm posted about the return; - you don't want to exit the function before you've completed the loop, unless some loop condition that you test allows you to return the correct value, that is, return true; or return false; if you don't find a character, etc.
Stephen James
@sjames1958gm
Jan 18 2018 17:06
@gusd773 Your return inside the for loop will return undefined (exit the function immediately)
@bradley1492 I converted each denomination in cid separately (and the amount of change)
and then converted each separately back at the end
jusgoose
@gusd773
Jan 18 2018 17:13
OMG THANK YOU SO MUCH CODE HACK OF THE YEAR, I FINALLY UNDERSTAND RETURN LOL
THANK YOU @khaduch && @bradley1492 I feel alot better about my code now lol
CamperBot
@camperbot
Jan 18 2018 17:14
:cookie: 307 | @bradley1492 |http://www.freecodecamp.org/bradley1492
gusd773 sends brownie points to @khaduch and @bradley1492 :sparkles: :thumbsup: :sparkles:
:star2: 3683 | @khaduch |http://www.freecodecamp.org/khaduch
Christopher
@bradley1492
Jan 18 2018 17:17
@sjames1958gm What is a denomination?
Stephen James
@sjames1958gm
Jan 18 2018 17:18
@bradley1492 the individual coin or paper money values.
1 cent, nickel, dime, quarter, etc.
Christopher
@bradley1492
Jan 18 2018 17:19
@sjames1958gm okay but how do you mean you converted each denomination seperately?!
@sjames1958gm I can only think of a for-loop or I guess reduce would be possible?1
Stephen James
@sjames1958gm
Jan 18 2018 17:20
@bradley1492 - something like this where the values in the array are times 100
     for(let i = 0; i < cid.length; i++){
     cid[i][1] = Math.round(cid[i][1]*100);
  }
Christopher
@bradley1492
Jan 18 2018 17:22

@sjames1958gm Okay I understand but what is the diffeerence to

let base = 0;
  for(let i = 0; i < cid.length; i++){
   let test = Math.round(cid[i][1]*100);
   base += test;
  }

From a concept point of view?

Stephen James
@sjames1958gm
Jan 18 2018 17:23
@bradley1492 You are combining all the values into one value, how will you figure out how much of each
denomination to return?
Christopher
@bradley1492
Jan 18 2018 17:24
@sjames1958gm Ohh goodness, sorry. My code is just a test in order to acess the array etc..
Sorry for wasting your time here:)
It was just to see how the floating inaccuracy can be dealt with
Stephen James
@sjames1958gm
Jan 18 2018 17:24
@bradley1492 Oh. I misunderstood your question.
Christopher
@bradley1492
Jan 18 2018 17:27
@sjames1958gm But I think it still answered my question. I guess than this is how it's sort of done, just multiplying by 100 and rounding and then at the end dividing by 100...
Stephen James
@sjames1958gm
Jan 18 2018 17:27
@bradley1492 Yes.
Christopher
@bradley1492
Jan 18 2018 17:27
@sjames1958gm Alright thx a lot
CamperBot
@camperbot
Jan 18 2018 17:27
bradley1492 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8868 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Jason Luboff
@JLuboff
Jan 18 2018 17:40
To those who use VS Community...any reason to keep 2015 and 2017 versions?
jusgoose
@gusd773
Jan 18 2018 18:10

can somebody help? I tried reading but the instructions for syntax/examples are bogus lol

/*function bouncer(arr) {

var falsyArr = [ {id: false}, {id:null},{id:0}, {id:""}, {id:undefined}, {id:NaN}];
arr.filter(function(val){
return val==true;
});
return arr;
}

bouncer([7, "ate", "", false, 9]); */

I get the jist it's just always SINtax
Kudzu
@czhower
Jan 18 2018 18:32
@JLuboff Not unless you are doing C++ and certain things. 2017 can run 2015 stuff fine
Jason Luboff
@JLuboff
Jan 18 2018 18:40
@czhower Thanks. I'm doing C# but the application I'm working on didn't throw up any errors or anything upon opening it. Just don't want to waste space by having both
CamperBot
@camperbot
Jan 18 2018 18:40
jluboff sends brownie points to @czhower :sparkles: :thumbsup: :sparkles:
:cookie: 76 | @czhower |http://www.freecodecamp.org/czhower
jusgoose
@gusd773
Jan 18 2018 18:41

HELP
can somebody help? I tried reading but the instructions for syntax/examples are bogus lol

/*function bouncer(arr) {

var falsyArr = [ {id: false}, {id:null},{id:0}, {id:""}, {id:undefined}, {id:NaN}];
arr.filter(function(val){
return val==true;
});
return arr;
}

bouncer([7, "ate", "", false, 9]); */
I get the jist it's just always SINtax

Kudzu
@czhower
Jan 18 2018 18:41
@JLuboff In C# you'll be fine and you dont need 2015 unless you have 3P libs that dont support 2017 yet.
Jason Luboff
@JLuboff
Jan 18 2018 18:52
@czhower :+1:
Diego Mayer
@Chrono79
Jan 18 2018 19:00
@gusd773 arr.filter returns a new array, it doesn't mutate the array in place
So you either store what it returns in a var and return that, or directly return arr.filter...
Kaz Baig
@kbaig
Jan 18 2018 19:03
Also, val == true isn't going to be useful for checking truthiness, unless the element happens to be true
Pieter Stokkink
@forkerino
Jan 18 2018 19:04
^ == does coerce, though
Kaz Baig
@kbaig
Jan 18 2018 19:04
oh does it
Jason Luboff
@JLuboff
Jan 18 2018 19:04
@forkerino I figured you had been blown away
Pieter Stokkink
@forkerino
Jan 18 2018 19:04
hahaha
Stayed indoors all day
Jason Luboff
@JLuboff
Jan 18 2018 19:04
I saw some gifs...looks pretty intense
Kaz Baig
@kbaig
Jan 18 2018 19:05
w0t
Is it just me or does "inventor" in the headline give him less legitimacy than "computer scientist", which he also is
Jason Luboff
@JLuboff
Jan 18 2018 19:07
Interesting. I don't understand half of it, but interesting haha
Kudzu
@czhower
Jan 18 2018 19:11
Seems a lot more complicated than fixed point math or BCD, both of which have been around since as lont as floating point.
Aside from lost of data density, BCD is really simple, blazingly fast, and there is even hardware support for it on some CPUs.
jusgoose
@gusd773
Jan 18 2018 19:13
@Chrono79 ok cool the challenge made me think it wanted the exact same array var we passed through the function I was going to store it into but I'm still stuck

@Chrono79
/* function bouncer(arr) {
var filteredArr = arr.filter(function (val){
return val==true;
});
return filteredArr;
}

bouncer([7, "ate", "", false, 9]); */

Diego Mayer
@Chrono79
Jan 18 2018 19:16
the code is commented
delete both /* and */
jusgoose
@gusd773
Jan 18 2018 19:17
@Chrono79 i did that to make the copying /pasting easier
Diego Mayer
@Chrono79
Jan 18 2018 19:17
@forkerino nice figure (not that I know what it means)
@gusd773 what's the problem now then?
jusgoose
@gusd773
Jan 18 2018 19:18
@Chrono79 not sure it keeps returning an empty array
Diego Mayer
@Chrono79
Jan 18 2018 19:20
@gusd773 it seems == true doesn't work either remove that and let js evaluate val or try !=false
The latter isn't good practice
Kaz Baig
@kbaig
Jan 18 2018 19:21
arr.filter(el => el) works
Jason Luboff
@JLuboff
Jan 18 2018 19:23
Or using the Boolean object works
jusgoose
@gusd773
Jan 18 2018 19:24
@Chrono79 nope just works for false as a falsy value not the others fml
Am i crazy for thinking my last version of the code should work?
Kelechi Chinaka
@ke1echi
Jan 18 2018 19:27
:wave: guys
Diego Mayer
@Chrono79
Jan 18 2018 19:32
@JLuboff Not object, function
Kaz Baig
@kbaig
Jan 18 2018 19:34
@JLuboff right but ya gotta filter for this algo anyways
jusgoose
@gusd773
Jan 18 2018 19:34
PERFECT @Chrono79 THANK YOU
CamperBot
@camperbot
Jan 18 2018 19:34
gusd773 sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4607 | @chrono79 |http://www.freecodecamp.org/chrono79
jusgoose
@gusd773
Jan 18 2018 19:35
I always forget return evaluates the objects as booleans
@kbaig Correct, you use filter with Boolean
Diego Mayer
@Chrono79
Jan 18 2018 19:36

@JLuboff

Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:

var x = Boolean(expression);     // preferred
var x = new Boolean(expression); // don't use
I meant that
Maybe we wanted to say the same
Jason Luboff
@JLuboff
Jan 18 2018 19:38
@Chrono79 Ah. Ya, same idea. But you're correct that they indicate its used as a function instead of an object
Diego Mayer
@Chrono79
Jan 18 2018 19:38
:+1:
Kelechi Chinaka
@ke1echi
Jan 18 2018 19:41
congrats on your first kata @Chrono79
Diego Mayer
@Chrono79
Jan 18 2018 19:43
Thanks, I'm going to great lengths translating it in every language I can :P (currently 7 of 12 languages are mine)
Kelechi Chinaka
@ke1echi
Jan 18 2018 19:44
nice job :smile:
Markus Kiili
@Masd925
Jan 18 2018 20:04
@JLuboff The problem is that MDN has no resource on Boolean function. It is only mentioned on the resource (that is mainly) about Boolean object. Then when they refer to that on the helpful links, people use Boolean object.
Brad
@bradtaniguchi
Jan 18 2018 20:17
Man and here I would be thinking booleans would be simple haha
jusgoose
@gusd773
Jan 18 2018 20:31

can somebody explain why this filters out falsy elements?

function bouncer(arr) {
return arr.filter(Boolean);
}
bouncer([7, "ate", "", false, 9]);

I had two other solutions that worked but this one is so pretty
Kaz Baig
@kbaig
Jan 18 2018 20:32
@gusd773 Boolean is a function that returns true if entered param is truthy
jusgoose
@gusd773
Jan 18 2018 20:37

so that's more or less this right @kbaig

"BOOLEAN " =

function bouncer(arr) {
return arr.filter( "FUNCTION(VAL)
RETURN VAL == TRUE;
}" );
}
bouncer([7, "ate", "", false, 9]);

correct?

I know it's super explicit but I just wanna understand that shortcut
Markus Kiili
@Masd925
Jan 18 2018 20:45
@gusd773 x==true is not a test for truthy value.
@gusd773 Filter passes each array element into the callback function you provide, and keeps those that return a truthy value from it. When you pass Boolean, each truthy element return true and every falsy return false. Therefore filter keeps truthy element in that case.
Same would happen with a callback function(elem){return elem;}.
jusgoose
@gusd773
Jan 18 2018 20:50
I did the latter but I don't get why it onlys keep the truthy elements and not the falsy ones, that doesn't make sense to me
Markus Kiili
@Masd925
Jan 18 2018 20:51
@gusd773 Do you mean this? function(elem){return elem;}
jusgoose
@gusd773
Jan 18 2018 20:53
yea @Masd925 I just don't why by default these methods are keeping only the truthy elements to filter when I didn't specify it to like i did in my interpretation above @ 14:37
don't get why*
Kaz Baig
@kbaig
Jan 18 2018 20:57
@gusd773 Boolean does it by default because that's what it function does. If you pass Boolean into Array.filter, Boolean will run on each element, return whether or not it is truthy, and Array.filter will use that result to determine whether or not to drop the element from the resulting array
jusgoose
@gusd773
Jan 18 2018 20:57
"that's just the way it is" fook me
thank you @Masd925 & @kbaig
CamperBot
@camperbot
Jan 18 2018 20:57
gusd773 sends brownie points to @masd925 and @kbaig :sparkles: :thumbsup: :sparkles:
:cookie: 536 | @kbaig |http://www.freecodecamp.org/kbaig
:star2: 4659 | @masd925 |http://www.freecodecamp.org/masd925
Kaz Baig
@kbaig
Jan 18 2018 20:58
You can imagine Boolean as
function Boolean(val) {
  return /* some code here saying "is val truthy?" */;
}
Markus Kiili
@Masd925
Jan 18 2018 21:12
@gusd773 Filter method actually coerces the return values from the callback function into type Boolean before checking if they are true. So that is why it keeps those elements that return a truthy value from the callback. That is just how filter works. You can read it up on the specification. Also knowing that, passing Boolean is a bit illogical, because filter always uses a function like Boolean under the hood.
Kaz Baig
@kbaig
Jan 18 2018 21:16
I would speculate the Array.filter uses the same source of logic as Boolean under the hood
Markus Kiili
@Masd925
Jan 18 2018 21:20
@kbaig Yes, both use the same internal coercion function (ToBoolean).
jusgoose
@gusd773
Jan 18 2018 21:22
dope
that made it alot clearer thank you, these concepts are finally getting engraved into my head so little details like that make my life easier
Jason Luboff
@JLuboff
Jan 18 2018 21:29
@Masd925 Ya..I get it. But MDN is infinitely better than MSDN. I hate the MSDN docs (at least for C#...and at least the docs I keep stumbling upon)
Markus Kiili
@Masd925
Jan 18 2018 21:30
@JLuboff Yeah. I can only remember a couple of errors or really bad code on MDN. And they are corrected nowadays.