These are chat archives for FreeCodeCamp/HelpJavaScript

18th
Nov 2017
方棉强
@miangqiang
Nov 18 2017 05:09
Stand in Line
I can't send the picture.
Ken Haduch
@khaduch
Nov 18 2017 05:11
@miangqiang - can you just post your code?
方棉强
@miangqiang
Nov 18 2017 05:12

function queue(arr, item) {
// 请把你的代码写在这里

return arr.push(item); // 请修改这一行
}

// 初始化测试数据
var testArr = [1,2,3,4,5];

// 控制台输出
console.log("Before: " + JSON.stringify(testArr));
console.log(queue(testArr, 6)); // 你可以修改这一行来测试你的代码
console.log("After: " + JSON.stringify(testArr));

Ken Haduch
@khaduch
Nov 18 2017 05:12
use markdown syntax - three backticks ``` on a line all by themselves before and after.
方棉强
@miangqiang
Nov 18 2017 05:12
写一个函数 queue ,用一个数组arr和一个数字item作为参数。数字item添加到数组的结尾,然后移出数组的第一个元素,最后队列函数应该返回被删除的元素。
Ken Haduch
@khaduch
Nov 18 2017 05:14
@miangqiang - you do not want to return the result of arr.push(item); because that will not be the element. It will be the new length of the array. You want to push the item into the array, and then .shift() an item out of the array, and return that.
方棉强
@miangqiang
Nov 18 2017 05:14
I'll try it, thank you
shivaprasad0372
@shivaprasad0372
Nov 18 2017 06:23
how to deploying angular js project into webhsot from loaclhost
Razvan Jackson
@RazvanJackson
Nov 18 2017 07:49
Who is good at hardware?
abraham anak agung
@padunk
Nov 18 2017 08:19
What is O(1) ?
Pieter Stokkink
@forkerino
Nov 18 2017 09:10
@padunk Big O is a way to categorize how long algorithms will take. O(n) means that it is linear compared to the number of inputs, O(1) means that it is constant, so each time you execute the algorithm, it takes the same amount of time, no matter how much input you have.
abraham anak agung
@padunk
Nov 18 2017 09:31
@forkerino great. Thank you.
CamperBot
@camperbot
Nov 18 2017 09:31
padunk sends brownie points to @forkerino :sparkles: :thumbsup: :sparkles:
:star2: 2950 | @forkerino |http://www.freecodecamp.org/forkerino
CamperBot
@camperbot
Nov 18 2017 14:09
:bulb: to format code use backticks! ``` more info
Yaroslav
@YariPL
Nov 18 2017 14:11

Hi
I have a problem. I want to disable "submit" button before the real email will be typed. How to do this?
As for now I have this form:

 <form action = "../includes/newsletter-signup-checker.php" method = "post" id="submit-email">

         <input autocomplete="off"  type="email" 
                   name="email-address" id ="txtmail" placeholder="<?php echo $lang['e-mail-footer']; ?>">

         <input disabled="true" class="btn-disabled btn-reset button button-blue" type="submit" id = "btn-reset"  value="<?php echo 
                  $lang['sign-up']; ?>">
</form>

and this where I tried to do this 'disable':

$(document).ready(function(){


// Reset Form:
    $("#submit-email").validate({
        rules: {
            txtmail: {
                required: true,
                email: true
            }

        },
        messages: {
            email: "Please enter a valid email address"
        }

    }); // end reset-form

  $("#txtmail").on("blur", function(){
    if($("#submit-email").valid())
   {
       $("#btn-reset").removeAttr("disabled");
   }
 }); 
});
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 14:51
i have an addEventListener which works with click on an element that has a specific class. but after i add the class and i want to remove the class (so the element cant be click anymore) i can still click on it. any reason why that happens?
ronald
@ronstarcool
Nov 18 2017 14:55
hey folks, anybody knows transition in react?
got a lil trouble with it
FlashHero
@FlashHero
Nov 18 2017 15:32
@thodorisanta I think you have to disable the button if don't want it to be clickable
Daniel Romero
@Ranacode
Nov 18 2017 15:38
what do you mean with transition in react? @ronstarcool
Stephen James
@sjames1958gm
Nov 18 2017 15:54
@thodorisanta The listener is attached to the element not to the class you will need to remove the listener or check in your handler that the class has been removed
abraham anak agung
@padunk
Nov 18 2017 16:25
Good evening, anybody know if i doing const result = [...a]; and do something with result. Do a get mutate?
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 17:07
@FlashHero @sjames1958gm how can i do that? cause apparently "remove class" didnt help me. (note that im using only plain js to this project, i dont wanna use jquery)
Diego Mayer
@Chrono79
Nov 18 2017 17:21
@padunk
let a = [1,2,3];
let result = [...a];
result.sort((a,b)=>b-a);
console.log(result); // [ 3, 2, 1 ]
console.log(a); // [ 1, 2, 3 ]
abraham anak agung
@padunk
Nov 18 2017 17:22
Hi @Chrono79 yes i know, but when i write a function, somehow a get mutate
Diego Mayer
@Chrono79
Nov 18 2017 17:23
Note: Spread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays as the following example shows (it's the same with Object.assign() and spread syntax).
is that your case?
var a = [[1], [2], [3]];
var b = [...a];
b.shift().shift(); // 1
// Now array a is affected as well: [[], [2], [3]]
abraham anak agung
@padunk
Nov 18 2017 17:27
@Chrono79 yes i think so... so what i suppose to do so the original array freeze?
deep copy or deep clone the original array, or try using non mutating functions, depends on what you're doing
abraham anak agung
@padunk
Nov 18 2017 17:30
@Chrono79 ha... thank you one more think to learn about copying / cloning
Diego Mayer
@Chrono79
Nov 18 2017 17:42
no problem
Razvan Jackson
@RazvanJackson
Nov 18 2017 18:44
Guys
What do you think about this laptop
Pieter Stokkink
@forkerino
Nov 18 2017 19:29
@RazvanJackson It's ok for the price they ask, although it wouldn't hurt to add 8 gigs of RAM to it.
I also would prefer a smaller - but solid state - HDD
Pieter Stokkink
@forkerino
Nov 18 2017 19:36
as opposed to a bigger classical one.
Stephen James
@sjames1958gm
Nov 18 2017 20:01
@thodorisanta removeEventListener
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:12
@sjames1958gm im kinda confused on how it works for instance i have the code document.addEventListener('click', e => { if (e.target.classList.contains('clickable')){ ...... how can i remove that?
FlashHero
@FlashHero
Nov 18 2017 20:16
@thodorisanta document.getElementById("element").disabled = true; or use css pseudo-class element:disabled{ . . . }
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:20
@FlashHero is there a reason on why after i place this line every piece of code beneath it doesnt run?
FlashHero
@FlashHero
Nov 18 2017 20:22
@thodorisanta what code???
I don't understand what you're trying to achieve
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:23
@FlashHero i placed this code document.getElementById("element").disabled = true; and under it i have a function call that doesnt run.
it was running before i added this line
FlashHero
@FlashHero
Nov 18 2017 20:24
@thodorisanta document.getElementById("element") here element is the input
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:31
@FlashHero which is the targets ID. i did it and still not works.basically what i did is i added a class to 4 elements so i can click on them with the event listener and then my plan was to remove the class from them to make them "unclicable" but it didnt work
FlashHero
@FlashHero
Nov 18 2017 20:32
@thodorisanta are you trying to make a button not clickable after it has been clicked?
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:34
@FlashHero im doing the simon game project so when the "computer" is showing the button clicks i want the user to be unable to click the buttons. (the class i added in css it makes the cursor a pointer, so it would be great if i could just remove the class) but it doesnt work that way
FlashHero
@FlashHero
Nov 18 2017 20:36
@thodorisanta ok so you would use document.getElementsByClass since you are targeting more than one elements
document.getElementsByClass('myClass').disabled=true; and when it's your turn then document.getElementsByClass('myClass').disabled=false;
it's actually document.getElementsByClassName not document.getElementsByClass
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:40

@FlashHero it still doesnt work :/ in line 198 i have the listener on click(for the user) and in line 116 i have the call for the bot to show the buttons that must be pressed.

https://codepen.io/oshikurou/pen/vWWaQL?editors=1011

i did a similar thing in another project with jquery and by adding and removing class everything worked fine. i thought the same logic would work with plain JS.
FlashHero
@FlashHero
Nov 18 2017 20:44
@thodorisanta what if you make two functions that enables and disables, so when it needs to be enabled you can call that function
instead of dealing with classlist and stuff
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:46
you mean place document.getElementsByClass('myClass').disabled=true; in the enable class and document.getElementsByClass('myClass').disabled=false; in the dissable class? @FlashHero
FlashHero
@FlashHero
Nov 18 2017 20:47
@thodorisanta opposite actually when it needs to be disabled document.getElementsByClass('myClass').disabled=true; you want to set the .disabled to true
@thodorisanta to be honest, I don't really understand this game , but i see that you don't have an class on your color divs, so maybe give it another class and leave that existing class there and then you can target it with document.getElementsByClass('myClass') I think that would be much easier
FlashHero
@FlashHero
Nov 18 2017 20:54
Oh nevermind i see the shapes class
Pieter Stokkink
@forkerino
Nov 18 2017 20:56
@thodorisanta the buttons have a clicable class initially, not a clickable class
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 20:57
@FlashHero i did it. i also did it in line 122 and called the disable function in line 118 and still nothing. basically the computer shows a "path" with the color buttons. and you need to click those buttons in the same order. basically memory game.
@forkerino yeah i just added those. it didnt get me anywhere
FlashHero
@FlashHero
Nov 18 2017 21:06
@thodorisanta so when do you want the div disabled ?
Pieter Stokkink
@forkerino
Nov 18 2017 21:17
@thodorisanta Open your dev console (not the codepen console, it hides too many things). You'll see Uncaught TypeError: document.getElementsByClass is not a function
because it is getElementsByClassName()
and this returns a element list, it probably doesn't have a disabled property
legendinraze
@legendinraze
Nov 18 2017 21:18
why dont I put quotations around property? ```

var hatValue = testObj.hat;      // Change this line
var shirtValue = testObj.shirt;    // Change this line
Pieter Stokkink
@forkerino
Nov 18 2017 21:21
with dot notation they are implied. In bracket notation they are needed (which means you can use variables in bracket notation, but not in dot notation). @legendinraze
FlashHero
@FlashHero
Nov 18 2017 21:27
@forkerino can you help me with the permutation algorithm
Pieter Stokkink
@forkerino
Nov 18 2017 21:27
what have you got now?
FlashHero
@FlashHero
Nov 18 2017 21:28
function permAlone(str) {
    var arrays=str.split('');
    var store=str.split('');
    var strlength=arrays.length;
  function swapLetters(a,b){
      var storeletter=store[a];
      store[a]=store[b];
      store[b]=storeletter;
      return store;
  }
  function rearrange(n){
      if(n==1){
                return store;
      }else{
          for(var i=0;i<n-1;i+=1){
          rearrange(n-1);
              swapLetters(n % 2 ? 0:i,n-1);
              console.log(store);
          }
      }
  }
      rearrange(arrays.length);
}

permAlone('abc');
I honestly don't understand what's happening but i just kind of researched on how people did it, but i have no idea how it's actually doing it
Pieter Stokkink
@forkerino
Nov 18 2017 21:30
That looks like Heap's algorithm, which is a great way to create permutations
only thing right now is that you don't create all permutations
or wait, you do
you just don't log all of them
FlashHero
@FlashHero
Nov 18 2017 21:31
how do i log them out
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 21:32
@FlashHero when its the computers "turn"
Pieter Stokkink
@forkerino
Nov 18 2017 21:32
in your if you also have to log @FlashHero
Tiago Correia
@tiagocorreiaalmeida
Nov 18 2017 21:33
hey @forkerino
FlashHero
@FlashHero
Nov 18 2017 21:33
@thodorisanta ok
@forkerino what do you mean in your if you also have to log
Pieter Stokkink
@forkerino
Nov 18 2017 21:33
@FlashHero hmm, it seems there's an error in there somewhere...
the for doesn't do enough iterations
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 21:34
@forkerino so what do you suggest? isnt there a way to make it work with "remove class" ?
Pieter Stokkink
@forkerino
Nov 18 2017 21:34
change i<n-1 to i<n
@thodorisanta of course there is a way
toggleClass is useful
or was that just jQuery?
classList.remove
FlashHero
@FlashHero
Nov 18 2017 21:36
@forkerino now there's 9 arrangements for 3 characters
Pieter Stokkink
@forkerino
Nov 18 2017 21:36
@FlashHero you're probably logging some of them twice
@thodorisanta when I change getElementsByClass to getElementsByClassName, it starts to function a lot better, maybe you can start from there and see what problems remain
FlashHero
@FlashHero
Nov 18 2017 21:42
@forkerino can you tell me how the heap's algorithm actually work, i don't feel too satisfied with this unknown solution
Pieter Stokkink
@forkerino
Nov 18 2017 21:44
It is a rather long explanation, but I found this article to be quite helpful: http://ruslanledesma.com/2016/06/17/why-does-heap-work.html
Brad
@bradtaniguchi
Nov 18 2017 21:45
@FlashHero the TLDR is you build a "heap" data structure, and then perform actions on that structure such as add/delete/search which are quick.
its all in the name of performance, as building the heap isn't cpu intensive, and once its setup all the manipulations perform very fast. (no fancy lingo about big O or anything here)
Pieter Stokkink
@forkerino
Nov 18 2017 21:46
Don't confuse Heap's algorithm with the heap data structure
Brad
@bradtaniguchi
Nov 18 2017 21:46
@forkerino am I confusing it? 80
damn your right
dudes name is heap :/ haha
Pieter Stokkink
@forkerino
Nov 18 2017 21:48
:joy:
yes
Good name
FlashHero
@FlashHero
Nov 18 2017 21:49
@forkerino @bradtaniguchi ok i'll try it thanks
@forkerino @bradtaniguchi thanks
CamperBot
@camperbot
Nov 18 2017 21:50
flashhero sends brownie points to @forkerino and @bradtaniguchi :sparkles: :thumbsup: :sparkles:
:cookie: 311 | @bradtaniguchi |http://www.freecodecamp.org/bradtaniguchi
:star2: 2951 | @forkerino |http://www.freecodecamp.org/forkerino
Pieter Stokkink
@forkerino
Nov 18 2017 21:50
yw
FlashHero
@FlashHero
Nov 18 2017 22:01
@forkerino you online
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 22:06
@forkerino i had it worked perfectly. the only problem was that i could still press buttons when it was the computers turn.
i changed my code as i had it before
it works perfectly now except for that i can press buttons while its the computers turn
and classList.remove doesnt work for some reason :/
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 22:12
toggle doesnt work either :/ @forkerino
line 116
FlashHero
@FlashHero
Nov 18 2017 22:13
@thodorisanta I had told you to remove document.getElementByclass, but i think you forgot to change it
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 22:14
@FlashHero where is that?
Stephen James
@sjames1958gm
Nov 18 2017 22:15
@thodorisanta You cannot remove an anonymous function, you need to make it a named function to be able to remove it by name
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 22:15
@sjames1958gm so the fact that i add it with js doesnt counts?
Stephen James
@sjames1958gm
Nov 18 2017 22:17
@thodorisanta to be able to remove a handler you have to have a name for the function.
element.addEventListener("click", clickFunction);
element.removeEventListener("click", clickFunction);
FlashHero
@FlashHero
Nov 18 2017 22:17
@thodorisanta there's no problem now that you have fixed it but when i said about disable and enable i told you the wrong thing and you added but forgot to delete it
@sjames1958gm you got time to look at my code the permutation one
Spyrantis Theodoros
@thodorisanta
Nov 18 2017 22:20
@sjames1958gm oh youre talking about the function. isnt there a way to just remove the class that i added to make the buttons unclicable(since the handler works with this class) and add the class again later?
Stephen James
@sjames1958gm
Nov 18 2017 23:31
@thodorisanta You used the class to find the element to add the handler, but it really has nothing to do with the class after that.
Your function could check if the element still has the class and if not just return?