These are chat archives for FreeCodeCamp/HelpJavaScript

26th
Nov 2017
Spyrantis Theodoros
@thodorisanta
Nov 26 2017 01:16

guys in line 215 in my JS i have an event listener (code is just plain JS) and im building the simons game so i want to "dissable" the event listener when its the computers turn, so that the player cant click on the buttons. I tried alot of things and i couldnt make it work. while doing the same thing with jquery was kind of easy. can you help me please? its the last thing i have to do for this project and i just cant find the way.

https://codepen.io/oshikurou/pen/qVormw?editors=0011

Stephen James
@sjames1958gm
Nov 26 2017 01:57
@thodorisanta The easiest thing would be to set a flag that says when the computers turn is and simply return from that handler if the flag is true.
Spyrantis Theodoros
@thodorisanta
Nov 26 2017 01:58
@sjames1958gm i did it and it didnt work for some reason
Stephen James
@sjames1958gm
Nov 26 2017 02:00
You have one click handler for the entire page?
Moisés Man
@moigithub
Nov 26 2017 02:08

soo .... on playerTurn() u add those class... and
ur click handler check if those "colors" have "clickable" class
where u remove em ? cant see on ur code...

so i want to "dissable" the event listener when its the computers turn

Luke Al-Saba
@LukeAlSaba
Nov 26 2017 02:54
Hi, is anyone here good with react?
Ogundele Olumide
@Lumexralph
Nov 26 2017 04:24
@LukieID ask what you need cleared, it's like you asking if there's a javaScript expert here, if anyone can answer they surely would though the traffic here is not as it used to be
koder3
@koder3
Nov 26 2017 08:57
why does this code give an infinite loop
```
function sumFibs(num) {
  var g = []
  for (var i = 0; i < num; i++){
    g.push(i)
  }
  return g
}

sumFibs(4);
Markus Kiili
@Masd925
Nov 26 2017 09:02
@koder3 It doesn't.
koder3
@koder3
Nov 26 2017 09:05
the page freezes after i hit ctrl+enter
its like i just ran a infinite loop but i didnt
Spyrantis Theodoros
@thodorisanta
Nov 26 2017 09:29
@moigithub thats the first thing i tried. removing and adding again the classes. it didnt work
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 11:11
gm everyone
Stephen James
@sjames1958gm
Nov 26 2017 11:29
@tiagocorreiaalmeida :wave:
@koder3 you are supposed to calculate the fibonacci up to num not num values
one of the tests has an extremely large num
Christopher
@bradley1492
Nov 26 2017 11:48
Hey guys,
I hope this question is not too compact, but is the following a valid code in getting to know which number can be divided by 1,2,3,4,5 without remains?
for (let i = 0; i <= 100; i++) {
    if ((5 && 4 && 3 && 2 && 1) % i == 0) {
      console.log(i);
    }   
  }
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 11:57
how's everything? @sjames1958gm
that wont work @bradley1492
try to console log
console.log((5 && 4 && 3 && 2 && 1));
and you will find out why it wotn work
in this case you are forced to check if all them return 0
5 % i == 0 && 4 % i == 0 and ...
but what's the final idea in here?
@bradley1492 what the code is doing is trying to check for a boolean state in literal numbers you will end up with the last number on the if only being divided by i
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 12:04
1%1= 0
that's the only time it will run as a true statement
truthy
Stephen James
@sjames1958gm
Nov 26 2017 12:09
@tiagocorreiaalmeida Good still early here - 6am
Christopher
@bradley1492
Nov 26 2017 12:11

@tiagocorreiaalmeida Hey thanks, just had a look at the console. Now I understand my issue here.

I am working on https://www.freecodecamp.org/challenges/smallest-common-multiple challenge.

I want to know which number can be divided by the range of the two numbers between arr[0] and arr[1]. So I just wanted to hardcode a solution in the meanwhile and then make something more dynamic.

Heres my code:


function smallestCommons(arr) {

  let maximum = Math.max.apply(null, arr);
  let minimum = Math.min.apply(null, arr);
  let place = [];
  for (let i = minimum; i <= maximum; i++) {
    place.push(i);
  }
  // the numbers between and inclusive arr[0] and arr[1] get pushed to the place array 

  place.sort(function(a, b){return b-a;});
  // this reverses the order of the place array

  for (let i = 0; i <= 100; i++) {
    if ((5 && 4 && 3 && 2 && 1) % i == 0) {
      console.log(i);
    }   
  } 

// Here I test if I can find the number by which there is no remain for the range of the array.

  return arr;
}


smallestCommons([1,5]);

My "real" Issue here is to find out how to find the number that can be divided by the whole range in a dynamic way. :)

CamperBot
@camperbot
Nov 26 2017 12:11
bradley1492 sends brownie points to @tiagocorreiaalmeida :sparkles: :thumbsup: :sparkles:
:cookie: 452 | @tiagocorreiaalmeida |http://www.freecodecamp.org/tiagocorreiaalmeida
Stephen James
@sjames1958gm
Nov 26 2017 12:14
You will need two loops. One to select candidate numbers the other to test each candidate
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 12:15
@sjames1958gm you wake up really early :D
Stephen James
@sjames1958gm
Nov 26 2017 12:15
Yes :)
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 12:17
im not sure if its something from portugal here we say that wake up early gives health and makes you grow :D
Christopher
@bradley1492
Nov 26 2017 12:18
@tiagocorreiaalmeida alrriight. I already had a play with nestet loops but sort of got stuck so this was already a thought. Many thanks :)
I'll give that a go
CamperBot
@camperbot
Nov 26 2017 12:18
bradley1492 sends brownie points to @tiagocorreiaalmeida :sparkles: :thumbsup: :sparkles:
api offline
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 12:20
@sjames1958gm do you have any idea how to check if an url is an image on backend?
Stephen James
@sjames1958gm
Nov 26 2017 12:20
@bradley1492 how to select the candidate numbers is important for efficiency (incrementing by 1 is too slow). The answer has to be a multiple of the largest number
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 12:21
on frontend we have the onerror trick on img tags
but on the backend its another thing
Christopher
@bradley1492
Nov 26 2017 12:24
@tiagocorreiaalmeida Hey I just checked the following in my console. Relating to the challenge of "smallestCommons([1, 5]) should return 60." Shouldn't I get 60 as a logged number?
But I get no number..
for (let i = 0; i <= 100; i++) {
  if (5 % i == 0 && 4 % i == 0 && 3 % i == 0 
  && 2 % i == 0 && 1 % i == 0 && i != 1) {
    console.log(i);
  }
}
Stephen James
@sjames1958gm
Nov 26 2017 12:29
@bradley1492 It should be i % 5 not 5 % i
Christopher
@bradley1492
Nov 26 2017 12:29
@sjames1958gm omg thanks hahahah
As math lessons in high school are a few years away I find myself sometimes to struggle with veeery easy concepts ^^
CamperBot
@camperbot
Nov 26 2017 12:29
bradley1492 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8713 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Stephen James
@sjames1958gm
Nov 26 2017 12:29
@tiagocorreiaalmeida Other than fetching the URL and looking at the mime type ...
There is the HEAD request which fetches the information without the actual data
AbrisM
@AbrisM
Nov 26 2017 16:18
function getSize() {
return Math.floor((Math.random()*25)+35)+"px";
} could someone explain what getSize does? Is it presenting the size of a font?
GlobalDevelopment
@GlobalDevelopment2016
Nov 26 2017 16:20
@AbrisM it appears to be generating a random number, multiplying it by 25 then adding 35. Then it's rounded down to the nearest full number and returned as a string with px appended to the end of the value so something like: "150px" would be outputted
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 16:20
^
AbrisM
@AbrisM
Nov 26 2017 16:21
@GlobalDevelopment2016 Interesting, so I'm guessing it has something to do with actually displaying perhaps the size of the background colors for the text
GlobalDevelopment
@GlobalDevelopment2016
Nov 26 2017 16:21
@AbrisM I guess but it isn't targeting an element or anything? The number is random...
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 16:58
I think web design leads alot of people into suicide
alpox
@alpox
Nov 26 2017 17:00
@tiagocorreiaalmeida nooo :D
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 17:01
hry @alpox !!!
alpox
@alpox
Nov 26 2017 17:05
@tiagocorreiaalmeida heeyo
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 17:05
so how's life?
alpox
@alpox
Nov 26 2017 17:05
good good :D
Finally a bit freetime
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 17:07
time for some true detective I would say
alpox
@alpox
Nov 26 2017 17:10
@tiagocorreiaalmeida true detective?
I'm playin OW :D
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 17:10
never played it :D only lol
I would like to play the shadow of war since I played shadow of mordor but the computer probably would be declared dead on the first 2 minutes
alpox
@alpox
Nov 26 2017 17:12
Because of the graphics? ^^
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 17:24
yeah
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 17:42
do you play lol?
alpox
@alpox
Nov 26 2017 17:56
No i dont :)
Adel
@AdelMahjoub
Nov 26 2017 19:00
OW is Overwatch ?
AbrisM
@AbrisM
Nov 26 2017 19:14
Shovel Knight? Was that the game where your in a castle and you have to make your way up?
Adel
@AdelMahjoub
Nov 26 2017 19:17
@AbrisM more like wonder boy , or zelda2, you go through zone to zone and can visit villages along the way, but mostly you are somewhere and make your way up
AbrisM
@AbrisM
Nov 26 2017 19:18
I think I have that from an RPMaker site
Was it an individual who made this game or a group?
Adel
@AdelMahjoub
Nov 26 2017 19:19
it is yacht club games, I think it is an inde studio
alpox
@alpox
Nov 26 2017 20:11
@AdelMahjoub yea it is
sabahatullah
@sabahatullah
Nov 26 2017 20:14
could someone please help me with this. I am getting an error "Cannot read property 'innerHTML' of null"
var menPDP = document.querySelector('.breadcrumb-container').innerHTML.indexOf("Men") > -1
how can I avoid that
alpox
@alpox
Nov 26 2017 20:15
@sabahatullah An element with class breadcrumb-container was not found on your page - thats why you get that error
sabahatullah
@sabahatullah
Nov 26 2017 20:17
@alpox is there anyway I can avoid this error. basically my code would be running on many pages where this element would be present
Moisés Man
@moigithub
Nov 26 2017 20:19
querySelector return the first element found right ?? (i forgot )
alpox
@alpox
Nov 26 2017 20:20
@moigithub yep
sabahatullah
@sabahatullah
Nov 26 2017 20:21
@alpox sorry there was a typo in my msg. my code would be running on many pages where this element would NOT be present
alpox
@alpox
Nov 26 2017 20:21
@sabahatullah You can avoid the error by going sure that the element is on the page. If you want to just do error case handling:
var elem = document.querySelector('.breadcrumb-container');
if(elem && elem.innerHTML.indexOf("men") != -1 && elem.innerHTML.indexOf("shoes") != -1) ....
else ....
sabahatullah
@sabahatullah
Nov 26 2017 20:25
there is a caveat, the .breadcrumb-container element is present on many pages but I want to assign the value only when .breadcrumb-container text is shoes and men
alpox
@alpox
Nov 26 2017 20:27
@sabahatullah May I ask what kind of pages?
sabahatullah
@sabahatullah
Nov 26 2017 20:36
just saw your code. all good now :)
thanks
@alpox thanks
CamperBot
@camperbot
Nov 26 2017 20:36
sabahatullah sends brownie points to @alpox :sparkles: :thumbsup: :sparkles:
:star2: 1467 | @alpox |http://www.freecodecamp.org/alpox
Bryan Reese
@breese8009
Nov 26 2017 21:17
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 21:33
@JLuboff around mate?
FlashHero
@FlashHero
Nov 26 2017 22:24
How can I stop and resume a timer with setInterval?
Stephen James
@sjames1958gm
Nov 26 2017 22:29
@FlashHero You have to assign the result of setInterval call to a variable and then later call clearInterval with that variable.
FlashHero
@FlashHero
Nov 26 2017 22:32
@sjames1958gm and after that do i call setinterval with remaining seconds?
when they want to resume the timer
Stephen James
@sjames1958gm
Nov 26 2017 22:33
@FlashHero Yes, you would have to keep track of how much time passed before you stopped.
FlashHero
@FlashHero
Nov 26 2017 22:34
@sjames1958gm ok i did that but failed and I'll try it again and see if it works thanks
CamperBot
@camperbot
Nov 26 2017 22:34
flashhero sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8714 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 22:47
https://tiagocorreia-pinterestclone.glitch.me/ if you guys can test and someone spot why margins wont work on the cards, followed masonry "rules" to make it work
thanks
@sjames1958gm if you can try to catch the mistake :p
FlashHero
@FlashHero
Nov 26 2017 23:20
@sjames1958gm I want to stop and resume with one button and why wouldn't this work because it updates the div text return $('#show').text('break');but then it continues with the timer
var click=0;
$('#start').click(function () {
    click++;
    $('#minus-break').prop('disabled', true);
    $('#plus-break').prop('disabled', true);
    $('#minus-time').prop('disabled', true);
    $('#plus-time').prop('disabled', true);
    var track = 'current_time';
    var count = parseInt($('#show').text() * 60);
        function counter() {
        var minutes = (count / 60);
        var seconds = count % 60;

        if (Math.floor(minutes) == 0 && seconds == 0) {
            if (track !== 'current_break') {
                count = parseInt($('#break-count').text() * 60);
                track = 'current_break';
            } else {
                count = parseInt($('#time-count').text() * 60);
                track = 'current_time';
            }
        } else if (minutes < 10 && seconds < 10) {
            $('#show').text('0' + Math.floor(minutes) + ' : ' + '0' + Math.floor(seconds));
            count--;
        } else if (seconds < 10) {
            $('#show').text(Math.floor(minutes) + ' : ' + '0' + Math.floor(seconds));
            count--;
        } else if (minutes < 10) {
            $('#show').text('0' + Math.floor(minutes) + ' : ' + Math.floor(seconds));
            count--;
        } else {
            $('#show').text(Math.floor(minutes) + ' : ' + Math.floor(seconds));
            count--;
        }
}
if(click % 2 ==0){
    clearInterval(stop); 
    return $('#show').text('break');
}else{
    var stop= setInterval(counter, 1000);
}
});
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 23:27
If you console log the stop variable ibside the if what does it return? :p
FlashHero
@FlashHero
Nov 26 2017 23:29
@tiagocorreiaalmeida oh undefined that's why
why is it undefined though ?
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 23:31
Declare the variable at the top or the code and change its value only on the else statement
FlashHero
@FlashHero
Nov 26 2017 23:35
@tiagocorreiaalmeida didn't work
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 23:39
do you know how set interval works tough?
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 23:39
with the code you have above it will run the same function every second
what's the point of the stop variable?
FlashHero
@FlashHero
Nov 26 2017 23:40
you said to declare it at the top
Tiago Correia
@tiagocorreiaalmeida
Nov 26 2017 23:40
but you want to stop the clock it's that?
the countdown
?
FlashHero
@FlashHero
Nov 26 2017 23:41
stop the countdown