These are chat archives for FreeCodeCamp/HelpJavaScript

31st
Jan 2018
Claudio Restifo
@Marmiz
Jan 31 2018 02:15
@cmurphy580 do you have an .env file in your directory?
If so you need to copy those to heroku as well :)
plus you probably want to serve the builded (minified) version of your react app.
Claudio Restifo
@Marmiz
Jan 31 2018 02:25
if it may help you, this is how I serve one React app with express:
app.use(express.static(path.join(__dirname, 'build')));

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
Lean Junio
@leanjunio
Jan 31 2018 02:52
Would anybody happen to know if Hybrid code can be converted into Native code?
ehutchllew
@ehutchllew
Jan 31 2018 03:43
@leanjunio Are you talking React-Native or something like cordova?
Dipesh Sukhani
@DKSukhani
Jan 31 2018 07:43
Hello Guys. A quick help. Am trying to create a button which has a href link. I need this link to toggle between 2 sites on everynew request of the webpage. Any suggestions
_
Marianissimus
@Marianissimus
Jan 31 2018 07:45
@DKSukhani so if(something) {href = link1}; else {href = link2}?
I don't really know what you mean by every new request of the webpage. If the user refreshes the page, or what?
Dipesh Sukhani
@DKSukhani
Jan 31 2018 07:46
So, my webpage will be visited by various people
It will be open for all ...
Marianissimus
@Marianissimus
Jan 31 2018 07:47
ok... go on...
Dipesh Sukhani
@DKSukhani
Jan 31 2018 07:47
For every new visitor - the link (in the backend) changes
Two simultaneous visitors are sent to different pages (as they click this button)
Marianissimus
@Marianissimus
Jan 31 2018 07:49
Wow... Ok... I don't know how to do this. It can probably be done in the backend - a script that "remembers" what URL is served to an user, then changes URLs for the next user.
Markus Kiili
@Masd925
Jan 31 2018 07:54
@DKSukhani That can be done on the backend. You can keep count on the requests and serve html with different href as needed.
Dipesh Sukhani
@DKSukhani
Jan 31 2018 07:55
@Masd925 Would the count be reset on every new visitor?
Markus Kiili
@Masd925
Jan 31 2018 07:56
@DKSukhani No, you can keep count on the visitors.
But you need to use some backend language.
Claudio Restifo
@Marmiz
Jan 31 2018 08:07
You can simply do that with some cookie logic on the backend :)
Markus Kiili
@Masd925
Jan 31 2018 08:15
@Marmiz Maybe no need for cookies if he just needs to serve different html to different users (depending only on the request count). Hard to say what he actually is doing.
Claudio Restifo
@Marmiz
Jan 31 2018 08:36
@Masd925 maybe you win something if you are the million visitor :)
Markus Kiili
@Masd925
Jan 31 2018 08:42
@Marmiz I do like those flickering red pop-ups.
Blauelf
@Blauelf
Jan 31 2018 08:51
I really love the countdown. Offer only valid for 60 seconds. What happens when it's negative? No longer have won an iPhone? (When clicking, I'm redirected to a website that no longer talks about having won an iPhone, but being put into a lottery for winning one, at some point in a year, and only if I enter all my data and allow them to sell it, that way the site where I enter my data conforms to European/German data protection standards)
Best thing are the "testimonials". All stock photos, clearly using US devices while the page is in German.
abraham anak agung
@padunk
Jan 31 2018 09:16
Hi all, simple question. when to use == and when to use ===?
Marianissimus
@Marianissimus
Jan 31 2018 09:19
@padunk === means "strict equality", which is "equality both in value and in type"
5 === 5 (true, as both are numbers); 5 == "5" (true, but first is a number, second is a string) BUT 5==="5" (false => one is a number, other is a string)
the general recommendation is to always try to use === , and first convert strings to numbers or the other way around if you need to
Blauelf
@Blauelf
Jan 31 2018 09:24

@padunk In most cases, === as it is safer, it requires the two values to be equal in value and type.

== when you know what you're doing, and that you need the implicit type conversions (and know which one will happen). Sometimes, null and undefined have same meaning to you, then you could do value == undefined or value == null, but value === undefined || value === null is also an option, and more clearly shows your intention to match any of the two.

Here for some extra fun: https://dorey.github.io/JavaScript-Equality-Table/

Markus Kiili
@Masd925
Jan 31 2018 09:30
@padunk Shortly put, always use ===.
abraham anak agung
@padunk
Jan 31 2018 09:31
Hmmm, so why there is ==?
Marianissimus
@Marianissimus
Jan 31 2018 09:35
because sometimes you're lazy to make a conversion. so you write 10 == "10" //true; :)
Markus Kiili
@Masd925
Jan 31 2018 09:36
@padunk I would call it one of the design errors of the language. The abstract equality algorithm is so complex and partially arbitrary, that nobody actually remembers how it works in all the cases (I have tried...).
Leads to less readability and hides bugs imo.
abraham anak agung
@padunk
Jan 31 2018 09:38
Hahaa @Marianissimus . Thanks @Masd925 @Blauelf
CamperBot
@camperbot
Jan 31 2018 09:38
padunk sends brownie points to @marianissimus and @masd925 and @blauelf :sparkles: :thumbsup: :sparkles:
:cookie: 460 | @marianissimus |http://www.freecodecamp.org/marianissimus
:star2: 4680 | @masd925 |http://www.freecodecamp.org/masd925
:star2: 4682 | @blauelf |http://www.freecodecamp.org/blauelf
Markus Kiili
@Masd925
Jan 31 2018 09:40
@Blauelf Just two cookies and you are caught :imp:
Blauelf
@Blauelf
Jan 31 2018 10:09
@Masd925 But == allows so wonderful objects.
function MyNum(num) {
  "use strict";
  this.valueOf = function() { return num; }
  this.times = function(num2) { return new MyNum(num * num2); }
  this.plus = function(num2) { return new MyNum(num + num2); }
}
console.log(new MyNum(5).times(6).plus(12) == 42); // true
Markus Kiili
@Masd925
Jan 31 2018 10:14
@Blauelf Yes, == can be fun.
Blauelf
@Blauelf
Jan 31 2018 10:14
Not that I see any practical application, though.
Razvan Jackson
@RazvanJackson
Jan 31 2018 10:24
Is someone here who's using codewars?
Marianissimus
@Marianissimus
Jan 31 2018 10:25
me, sometimes @RazvanJackson
Blauelf
@Blauelf
Jan 31 2018 10:30
@Chrono79 and @forkerino work on creating katas, I think. But wrong time zone maybe, at least for Argentina. Any specific question?
Sharikzama
@ZamaSharik
Jan 31 2018 10:52
function printFarmInventory(cows,chickens){
  var cowString = String(cows);
  while(cowString.length < 3)
    cowString = "0"+cowString;
console.log(cowString + "cows");
var chickenString = String(chickens);
while(chickenString.length < 3)
  chickenString = "0" + chickenString;
 console.log(chickenString + "chickens");
}
printFarmInventory(7,11);
can I get help in the while loop
I am unable to understand what while loop does in the above code..
Aditya
@ezioda004
Jan 31 2018 10:58
@Sidsharik Its prepending "0" to the passed arguments till its length is 3.
Sharikzama
@ZamaSharik
Jan 31 2018 10:59
we pass 7 and 11 as arguments so will they converted into String becoz of String() method ?
@ezioda004
Aditya
@ezioda004
Jan 31 2018 11:00
Yes
Sharikzama
@ZamaSharik
Jan 31 2018 11:01
so cowString.length value is 1 ?
@ezioda004
Aditya
@ezioda004
Jan 31 2018 11:01
Initially yes "7".length //1
Adding "0" keeps incrementing it.
Sharikzama
@ZamaSharik
Jan 31 2018 11:03
Thanks a lot
@ezioda004
Aditya
@ezioda004
Jan 31 2018 11:04
np :)
Diego Mayer
@Chrono79
Jan 31 2018 11:25
@Blauelf Not so far of my waking up time, but I have other priorities when I do ;)
Blauelf
@Blauelf
Jan 31 2018 12:30
@Chrono79 Becoming human by inhaling a litre of hot coffee?
Pieter Stokkink
@forkerino
Jan 31 2018 13:03
I've done very little with regards to codewars since I got my job. Family slurps up most of my free time.
Still have a couple kata ideas to work out
I started on an algorithm solving a 3d sudoku
Diego Mayer
@Chrono79
Jan 31 2018 13:47
@Blauelf I don't drink coffee (I have enough insomnia already) I was referring to nature call ;)
@forkerino I've passed the 10000 honor points (good) but still 2 kyu (bad)
Pieter Stokkink
@forkerino
Jan 31 2018 13:52
w00t w00t!
Blauelf
@Blauelf
Jan 31 2018 14:58
4393 honor and 2kyu for me. Haven't done much lately, and especially no 1kyu stuff.
Kelechi Chinaka
@ke1echi
Jan 31 2018 15:14
:clap: @Chrono79
Diego Mayer
@Chrono79
Jan 31 2018 15:22
@kelechy Thanks, have you tried my kata?
CamperBot
@camperbot
Jan 31 2018 15:22
chrono79 sends brownie points to @kelechy :sparkles: :thumbsup: :sparkles:
:cookie: 325 | @kelechy |http://www.freecodecamp.org/kelechy
Blauelf
@Blauelf
Jan 31 2018 15:26

@Chrono79 There's no way to see the honor/rank breakdown on other people's profile like on your own, is there? I mean, sure I can go to https://www.codewars.com/users/Chrono79/site-events to see the honor points, still no way to see the rank percentages. The underlying information is available, just the aggregated isn't.

My progress of JavaScript is 2 kyu / 25%. Does that mean I need to solve another 3 or 6 1kyu katas?

Diego Mayer
@Chrono79
Jan 31 2018 15:32
@Blauelf I couldn't find that data either, even Red users doesn't show that percentage jhoffner profile I'm not so sure if solving 3 or 6 1kyu will get you there, if you do, let me know
Pieter Stokkink
@forkerino
Jan 31 2018 15:32
In my experience a 1 kyu adds about 10-15% when you are at 2kyu
don't remember exactly
Diego Mayer
@Chrono79
Jan 31 2018 15:33
It sure feels like climbing a negative slope in my case :P
Pieter Stokkink
@forkerino
Jan 31 2018 15:34
Maybe solving too many 7/8 kyu challenges makes you lose points
:P
Blauelf
@Blauelf
Jan 31 2018 15:36
I think solving things at or below your level gives honor, but no rank.
Diego Mayer
@Chrono79
Jan 31 2018 15:38
@forkerino I hope not, I would be in negative values by now :P
@Blauelf gives only a very little fraction
Kelechi Chinaka
@ke1echi
Jan 31 2018 15:38
@Chrono79 oh nice i would some hours after now
negative values?
i dont think so, it just adds little to nothing @Chrono79
Diego Mayer
@Chrono79
Jan 31 2018 15:43
@kelechy That's what I said, I was answering to @forkerino 's joke
Kelechi Chinaka
@ke1echi
Jan 31 2018 15:45
your honour increase is remarkable, do u ever sleep :smile: @Chrono79
Blauelf
@Blauelf
Jan 31 2018 15:46

@Chrono79 http://www.codewars.com/about

Earning Ranks

You can advance through the ranks by completing kata at or above your rank - the harder the kata the faster you advance.

So I'll have to do 2kyu and up.

Diego Mayer
@Chrono79
Jan 31 2018 15:46
@kelechy just once in a while, and that's because of a damn cough I have right now
@Blauelf From my experience solving many, many, many, many lower rank kata you advance too
Frozen snail speed, but you do
;)
Kelechi Chinaka
@ke1echi
Jan 31 2018 15:48
yes "many" :smile:
Diego Mayer
@Chrono79
Jan 31 2018 15:48
I missed some many there :P
Pieter Stokkink
@forkerino
Jan 31 2018 15:49
lol
Kelechi Chinaka
@ke1echi
Jan 31 2018 15:49
lol
Diego Mayer
@Chrono79
Jan 31 2018 15:49
only 35 js 2kyu katas (I've solved 3 of them)
16 1kyu, still have to solve 1 of them
Jason Luboff
@JLuboff
Jan 31 2018 16:10
I think I'm at 4 Kyu.. but...I struggle with kata's at that level heh
Razvan Jackson
@RazvanJackson
Jan 31 2018 16:15
@Chrono79 Lol, how the hell are you so fkin good :))
Blauelf
@Blauelf
Jan 31 2018 16:17
@Chrono79 Many, many, many lower rank kata should have an impact, as on another page they claim it doesn't matter in which order you solve the katas.
Pieter Stokkink
@forkerino
Jan 31 2018 16:21
@Chrono79 I solved 2 1kyus and 11 2kyus. They definitely require more time, but many of them are not that much harder than 3kyu katas
jusgoose
@gusd773
Jan 31 2018 16:39

HELP RANDOM QUOTE GENERATOR IS ALMOST DONE :(

<script>


$(document).ready(function() {

$("#getQuote").on('click', function(){
$.ajaxSetup({cache:false});
$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(data) {
$(".message").html(data[0].content + "—" + a[0].title)
});
});
});
</script>

<HMTL>

<div class = "container-fluid" id-"content">
<div style="text-align:center">
<h1> Random Quote Generator</h1>
</div>

<div style="text-align:center">
<div class = "col-xs-12 well message">
Quote Goes Here
</div>
</div>
<div class = "text-align:center">
<div class = "col-xs-12">

<button id = "getQuote" class = "center-block btn btn-primary">
Get Quote
</button>
</div>
</div>
</div>

</HTML>

Jason Luboff
@JLuboff
Jan 31 2018 16:43
You say help but you don't say about what... and also please format your code properly.
```
shift + enter for new line, paste code, shift+enter for new line
```
Pieter Stokkink
@forkerino
Jan 31 2018 17:07
"—" + a[0].title) looks a bit dodgy, @gusd773
what is a?
jusgoose
@gusd773
Jan 31 2018 17:08
lmao @JLuboff read the first line
thank you @forkerino
CamperBot
@camperbot
Jan 31 2018 17:08
gusd773 sends brownie points to @forkerino :sparkles: :thumbsup: :sparkles:
:star2: 2979 | @forkerino |http://www.freecodecamp.org/forkerino
Jason Luboff
@JLuboff
Jan 31 2018 17:08
@gusd773 HELP RANDOM QUOTE GENERATOR IS ALMOST DONE :worried: That says nothing of what your issue is.
Pieter Stokkink
@forkerino
Jan 31 2018 17:08
^ truth
jusgoose
@gusd773
Jan 31 2018 17:08
ok ok
@JLuboff @forkerino I don't know why this is not returning a new quote when I feel like it is setup to do so
Jason Luboff
@JLuboff
Jan 31 2018 17:12
Not sure if it'll make a difference as I'm unfamilar with the API, but try adding ? to the end of the url so it'll be callback=?
jusgoose
@gusd773
Jan 31 2018 17:13
@JLuboff it did not, I used the url string provided by the api site which is specifically written out for JSON get calls
Pieter Stokkink
@forkerino
Jan 31 2018 17:14
@gusd773 what does your console say when you click the button?
or.... did you include jQuery?
jusgoose
@gusd773
Jan 31 2018 17:14
PSA sorry if I come off as a donkey sometimes but I literally exhaust every resource I can find on my own (EVEN READING AND I HATE READING)
@forkerino yes I imported everything including bootstrap
although there are 2 versions of JQuery I imported as a precaution similarly to bootstrap as when I only loaded one bootstrap version it wouldn't read the "well" class tags
Sweet Coding :)
@SweetCodingInc
Jan 31 2018 17:16
@gusd773 show us some codezzzzz
jusgoose
@gusd773
Jan 31 2018 17:16
@gusd773 @SweetCodingInc
Tom
@moT01
Jan 31 2018 18:01
hey, im making a drum machine type thing - and i play a sound when hitting a button - but the sound wont play again until the first sound finished... so I click my drum -> it plays the sound... and if i click the same drum too quickly it won't play again. If I wait like a half second before I click the second time it will play again. Is there a way to make it so it doesnt need to finish. I thought I could probly make multiple audio variables or something - but i dont like that solution - any thoughts?
Stephen James
@sjames1958gm
Jan 31 2018 18:03
@moT01 How are you playing your sounds?
Tom
@moT01
Jan 31 2018 18:03
audio.play() - let me paste the code
i have a bunch of div.key
and js
const sounds = Array.from(document.querySelectorAll('.sound'));
const buttons = Array.from(document.querySelectorAll('.key'));

buttons.forEach((button, index) => {
  button.addEventListener('mousedown', function() {
    playDrum(index);
  });
});

function playDrum(index) {
  buttons[index].classList.add('playing');
  setTimeout(function() {
    buttons[index].classList.remove('playing');
  }, 50);
  sounds[index].play();
}
i tried creating the audio in the playDrum function - no difference
Matej Bošnjak
@mbosnjak01
Jan 31 2018 18:06
Sorry for interupting. I would just also remove all audio tags and generate them through a for loop, while having links to data and it's other info inside json object
:P
so I don't spam html too much inside the file and for more audio just add info inside json
Stephen James
@sjames1958gm
Jan 31 2018 18:07
@moT01 did you try sounds[index].pause() when you switch sounds
@moT01 of course you could just have on audio element and change out the source when changing sounds.
Tom
@moT01
Jan 31 2018 18:08
no - the index wouldnt be correct
Stephen James
@sjames1958gm
Jan 31 2018 18:08
@moT01 The point was to pause the last one before playing the next.
Tom
@moT01
Jan 31 2018 18:10
but if i added a pause after my mousedown - the index wouldnt be right - perhaps it would know or something
i suppose i could throw a variable in there to keep track
of last index
Matej Bošnjak
@mbosnjak01
Jan 31 2018 18:11
have you testes with other event listeners, like, click?
Tom
@moT01
Jan 31 2018 18:12
yea, click waits till the mouseup
Matej Bošnjak
@mbosnjak01
Jan 31 2018 18:13
i have an idea :|
when you get mouse on your audio, it could get an ID of it, if if had an ID. and then store is somewhere (var previousSound), and when you go with the mouse to the next desired sound, script reads previous ID, pauses it, adds current ID to the previousSound variable and starts it. And it should go on .... in my head it works :'D
Stephen James
@sjames1958gm
Jan 31 2018 18:15
@moT01 A single audio element wouldn't require you to remember the last play
Tom
@moT01
Jan 31 2018 18:16
@mbosnjak01 ha
perhaps it would
pause didnt work
@sjames1958gm @mbosnjak01 thanks for the tips - ill try some out and keep playing with it
CamperBot
@camperbot
Jan 31 2018 18:18
mot01 sends brownie points to @sjames1958gm and @mbosnjak01 :sparkles: :thumbsup: :sparkles:
:cookie: 224 | @mbosnjak01 |http://www.freecodecamp.org/mbosnjak01
:star2: 8923 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Matej Bošnjak
@mbosnjak01
Jan 31 2018 18:19
@moT01 I'll try to do that challenge now in my own way. Shouldn't take more than an hour or so :|
Tom
@moT01
Jan 31 2018 18:20
lets see it when your done
Matej Bošnjak
@mbosnjak01
Jan 31 2018 18:20
@moT01 it's on beta?
Tom
@moT01
Jan 31 2018 18:20
i think there is a version there
im on some other challenge
Matej Bošnjak
@mbosnjak01
Jan 31 2018 18:21
@moT01 could you just pass me the drums app link?
nvm found it
Tom
@moT01
Jan 31 2018 18:35
how can i make my google search so that site:stackoverflow.com is pre-typed for me
got it
Tom
@moT01
Jan 31 2018 18:41
my drums - not the google thing
removed the html audio elements and javascript looks like this now...
const buttons = Array.from(document.querySelectorAll('.key'));
const sounds = [
  './sounds/clap.wav',
  './sounds/hihat.wav',
  './sounds/kick.wav',
  './sounds/openhat.wav',
  './sounds/boom.wav',
  './sounds/ride.wav',
  './sounds/snare.wav',
  './sounds/tom.wav',
  './sounds/tink.wav'
];

buttons.forEach((button, index) => {
  button.addEventListener('mousedown', function() {
    playDrum(index);
  });
});

function playDrum(index) {
  buttons[index].classList.add('playing');
  setTimeout(function() {
    buttons[index].classList.remove('playing');
  }, 50);
  let sound = new Audio(sounds[index]).play();
}
not sure what's wrong with the last sound
Stephen James
@sjames1958gm
Jan 31 2018 18:57
@moT01 all the sounds work for me :+1:
Tom
@moT01
Jan 31 2018 19:04
:dancers:
jusgoose
@gusd773
Jan 31 2018 20:00

HELP I am not sure why my script is not changing my quote to get it from the api

https://codepen.io/gusd773/pen/PQwxRE

Jason Luboff
@JLuboff
Jan 31 2018 20:09
@gusd773 Your API is running over http, but codepen uses https. If you look in the console of your browser (F12) you'll see the error
@moT01 Dope
jusgoose
@gusd773
Jan 31 2018 20:10
@JLuboff gracias( thanks)
CamperBot
@camperbot
Jan 31 2018 20:10
gusd773 sends brownie points to @jluboff :sparkles: :thumbsup: :sparkles:
:star2: 2316 | @jluboff |http://www.freecodecamp.org/jluboff
Matej Bošnjak
@mbosnjak01
Jan 31 2018 20:38
@gusd773 "https://quotesondesign.com/wp-json/posts?filter[orderby]=rand" - this call works for me, inside getJSON
nevermind, yours works as well, but filter and callback args don't need to be there