These are chat archives for FreeCodeCamp/HelpJavaScript

19th
Nov 2017
Ronique Ricketts
@RoniqueRicketts
Nov 19 2017 01:33
Hey guys how do i add a secondary file that handle router with the fcc glitch boilerplate?
Ogundele Olumide
@Lumexralph
Nov 19 2017 03:55
@RoniqueRicketts do you have a file handling routing there?
if you don't write your routes with the route handlers/controllers on the file and export it to the entry point of your app or where the app starts if I understand what you meant
Sherlock Holmes
@GGX666
Nov 19 2017 05:32
hi
Henry
@GitHub-Henry
Nov 19 2017 07:59
@GGX666 :wave:
Aizaz Shahid
@aizazshahid
Nov 19 2017 09:27
Is there any way to merge previous repository to new one ?
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 10:02
@sjames1958gm yeah but how can i do that?
Henry
@GitHub-Henry
Nov 19 2017 10:08
@thodorisanta Perhaps this will help
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 10:42
@GitHub-Henry i did the same thing with jquery ( adding a class to an element so i can handle that class on click! and then remove when i wanted to make the click handler to stop) the thing is that by removing the class with plain js (i wanna se just plain js to this project) the handler still works.So can i just stop the handler to work by somehow removing the class from the element?
Henry
@GitHub-Henry
Nov 19 2017 10:54
@thodorisanta you can remove the event handller with this
Henry
@GitHub-Henry
Nov 19 2017 11:05
hope that helps, gtg :shipit: :dizzy:
Ronique Ricketts
@RoniqueRicketts
Nov 19 2017 13:15
@Lumexralph oh that's what I don't have. you mean app.router?
@Lumexralph thanks I will try that. I didn't get all the routing aspect down pack with FCC challenges
CamperBot
@camperbot
Nov 19 2017 13:17
roniquericketts sends brownie points to @lumexralph :sparkles: :thumbsup: :sparkles:
:star2: 2059 | @lumexralph |http://www.freecodecamp.org/lumexralph
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 13:34
@GitHub-Henry either im doing it wrong or it doesnt work in my project :/
Dovydas Stirpeika
@Giveback007
Nov 19 2017 13:43
@forkerino hey man how you been
Ronique Ricketts
@RoniqueRicketts
Nov 19 2017 13:46
question when using express and routers do we bundle all our requests based on types. Eg 1 file for GET, 1 for POST etc.
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 13:57
I would say so @RoniqueRicketts
about the question above you got it solved or?
here is an example @RoniqueRicketts
on the app file or main file or how do you call it
app.use("/user",user);
now for all the paths that start wit huser a I got a file that handle those
const express = require("express"),
    router = express.Router();
router.post("/profile", auth, (req, res) => {
    let fullName = req.body.fullname;
    let city = req.body.city;
    let state = req.body.state;
    User.findByIdAndUpdate(req.user.id, { $set: { fullName, city, state } }, { _id: 0, username: 1, photo: 1, fullName: 1, city: 1, state: 1, new: true }).then((user) => {
        if (user) {
            req.flash("info", "Your personal data was updated with success");
            res.render("user", { user });
        }
    }).catch((e) => {
        console.log(e);
    })
});
by this way the only thing you need to export in the end its the router
module.exports = router;
Ronique Ricketts
@RoniqueRicketts
Nov 19 2017 14:04
@tiagocorreiaalmeida Yes Man I solved it already. Thanks anyways.
CamperBot
@camperbot
Nov 19 2017 14:04
roniquericketts sends brownie points to @tiagocorreiaalmeida :sparkles: :thumbsup: :sparkles:
:cookie: 445 | @tiagocorreiaalmeida |http://www.freecodecamp.org/tiagocorreiaalmeida
Hi guys
what's wrong with this code
why the output is not displaying
its just appear for 1 second
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:17
can someone help me? i add a class to an element so i can handle it with my handle( :P ) on click. and then when i wanna make the button unclickable i tried to remove the class but the user can still click it. im using only plain js for the project. i did a similar thing with jquery and it worked. :/ please help
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:31
what's the part that's not working @thodorisanta ?
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:32
@tiagocorreiaalmeida i tried to remove the class when i wanted to user to be unable to press the button with element.classList.remove("className"); but it diddnt work
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:39
doesnt remove the class or?
isnt element an array ?
im not familiar with vanilla js work over dom sadly
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:40
@tiagocorreiaalmeida i dont know if it removes the class but im sure that the click handler still works while it shouldnt
Stephen James
@sjames1958gm
Nov 19 2017 15:40
@thodorisanta removing the class wont remove the handler.
You will need to use element.removeEventListener("click", fn);
where fn is the same function that you used in addEventListener
@thodorisanta The class only gives you a reference to the element where you add the handler
removing the class has no affect on the added handlers
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:41
or in css
pointer-events: none;
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:41
and element the class that i added right?
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:42
doesnt matter how much you click it wont click :D
Stephen James
@sjames1958gm
Nov 19 2017 15:42
Whichever element you added the listener to
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:44
@sjames1958gm i try to remove it in line 137 and the listener is in line 223. im not sure if i did it correctly
https://codepen.io/oshikurou/pen/vWWaQL?editors=1011
Stephen James
@sjames1958gm
Nov 19 2017 15:46
@thodorisanta your addEventListener needs to use a non-anonymous function so that you can reference in the remove
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:46
@sjames1958gm umm. what does that mean
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:46
is the idea disable and avaible the buttons?
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:46
@tiagocorreiaalmeida yeah
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:46
pointer-events:auto
pointer-events:none
Stephen James
@sjames1958gm
Nov 19 2017 15:47
document.addEventListener('click', e => { << this is an anonymous function
you need
function clickHandler(e) { }
then you can use
document.addEventListener('click', clickHandler);
and
document.removeEventListern('click', clickHandler);
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:47
probably @sjames1958gm feels my solution is not the greatest :D
Stephen James
@sjames1958gm
Nov 19 2017 15:48
There are many ways to do the same thing -
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:49
just kidding :D, how are you?
Stephen James
@sjames1958gm
Nov 19 2017 15:49
relaxing on a SUnday morning :)
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:51
does the @tiagocorreiaalmeida pointer-events:auto also puts a cursor and does pointer-events:none removes it?
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:51
cursor:pointer;
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:51
yeah
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:52
its one thing
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:52
what do you mean
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:52
you always have a cursor?
none
The element is never the target of mouse events; however, mouse events may target its descendant elements if those descendants have pointer-events set to some other value. In these circumstances, mouse events will trigger event listeners on this parent element as appropriate on their way to/from the descendant during the event capture/bubble phases.
none
The element is never the target of mouse events; however, mouse events may target its descendant elements if those descendants have pointer-events set to some other value. In these circumstances, mouse events will trigger event listeners on this parent element as appropriate on their way to/from the descendant during the event capture/bubble phases.
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:53
no its in the class i added for the listener that i tried to remove later
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 15:53
you can use the process or removing the class
the class that says the button is active has pointer-events:auto;
when you remove the default has to be defined has pointer-events:none;
as*
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 15:57
so if i understand correctly you mean that i have to put a class in the buttons in my html so the listener will take it from there. and then just play with css with( pointer-events:auto - pointer-events:none) and make the button clickable or unclickable? @tiagocorreiaalmeida
David
@David-Moreira
Nov 19 2017 15:57
Hello guys. I'm working on the Exact Change Challenge.
But I'm having difficulties, could you give me some tips. Here's my code atm.
// 
var register =
    {
      currency:[{
  name: "PENNY",
  value: 0.01,
  amount: 0}, 
                {
  name: "NICKEL",
  value: 0.05,
  amount: 0},
                {
  name: "DIME",
  value: 0.10,
  amount: 0},
                {
  name: "QUARTER",
  value: 0.25,
  amount: 0},
                {
  name: "ONE",
  value: 1.00,
  amount: 0},
                {
  name: "FIVE",
  value: 5.00,
  amount: 0},
                {
  name: "TEN",
  value: 10.00,
  amount: 0},
                {
  name: "TWENTY",
  value: 20.00,
  amount: 0},
                {
  name: "ONE HUNDRED",
  value: 100.00,
  amount: 0}],
      total:0
      };





function checkCashRegister(price, cash, cid) {

  var totalCash = 0;
  cid.forEach(function (item) {
  totalCash += item[1];
  });
  register.total = totalCash;
  var change = cash - price;

  //Fill the register with every amount
  cid.forEach(function(item) {

    register.currency.forEach(function (x) {
      if (item[0] == x.name){

        x.amount = item[1];
      }

    });  
  });
  var payChange = register.total - change;
    if (payChange == 0 ) { // Equal to change due
    return "Closed";
  }



    var arrayChange = [[]];
            for (i=register.currency.length-1; i>=0; i--) {

              var currencyValue = register.currency[i].value;
              var currencyName = register.currency[i].name;
                    while (currencyValue > 0) {
                    var changeVerify = payChange - currencyValue;

                      if (changeVerify  >= 0 ) {


                                arrayChange.forEach(function(item) {

                                  var index = item.indexOf(currencyName);
                                  if (index != -1) {

                                     item[index][1] += currencyValue;

                                    }  else {


                                    item.push(currencyName,currencyValue);


                                    }
                                });

                                     payChange -= currencyValue;                    
                                     register.currency[i].amount -= register.currency[i].value;
                        } else {
                          break;
                        }
                     }

            }

      if (payChange > 0) {
        return "Insufficient Funds";
      }




return arrayChange;

  }

  // Here is your change, ma'am.



// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 16:04
@thodorisanta exactly
you can do with a class or not but since you said you had a class you add and remove I told to do with the class
you can just style the elemtns with js
either one or the other will work
Stephen James
@sjames1958gm
Nov 19 2017 16:06
@David-Moreira I don't see where you make sure that the change amount is greater than the value in the current till
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 16:08
@tiagocorreiaalmeida how can i add style to all elements with a class with plain js? cant find it
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 16:15
do they have some class in common?
document.getElementsByClassName('shapes')
im not sure if this returns an array
anyway easier way
add a class called active to css
all it does is pointer-events:auto;
define the default of shape to pointer-events:none;
when you want you add the class when youy dont need it you remove it
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 16:20
for what I saw with vanillajs you need to get elements with the class shape and loop trough them
nothing google can't tell you ;)
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:06
so when i add active i dont have to remove the other one right?
i guess i can try it
and also make the listener to listen on shapes?
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 17:15
or maybe on the entire board?
disable the entire board click when the sounds are playing an example
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:17
isnt this the way to remove the class?
var x = document.getElementsByClassName('shapes'); x.classList.remove("active");
cause i did it and it doesnt works
Pieter Stokkink
@forkerino
Nov 19 2017 17:21
You'd have to iterate over the list you get and remove the class on each one separately
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:24
@forkerino so x is an array now?
Pieter Stokkink
@forkerino
Nov 19 2017 17:25
@thodorisanta it's an array like object, but I think you can use .forEach on it without problems.
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:26
@forkerino cant i just use for loop?
Pieter Stokkink
@forkerino
Nov 19 2017 17:27
@thodorisanta perhaps. It doesn't behave completely like an array, so not 100% sure.
But I think x[0] would be the first element
so inside a loop it would be something like x[i].classList.remove("active")
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:39
i added the class in line 207 in js and removed it in 130 but still doesnt work :/ can you check it out?
https://codepen.io/oshikurou/pen/QOazwO?editors=1111
@forkerino
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 17:48
hey @forkerino
anyone around who has used the passport-facebook?
the only thumbnail it returns its too small for the porpuse I need it, if someone has got into the same issue and found a solution would be great to know what was the one
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:50
thats the last thing that i have to do for the project and i just cant find the way :P
Moisés Man
@moigithub
Nov 19 2017 17:52
how u know if it doesnt work ? @thodorisanta
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 17:52
it doesnt remove the class
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:53
if you turn the game on and you press play while the computer shows the buttons you can still press the buttons. @moigithub
Moisés Man
@moigithub
Nov 19 2017 17:54

u doing

turn game on
computer turn
removeClasses("active");
player turn
addClasses("active");

then.. check if active is there or not <-- at this point ofc u can still "press the buttons"

temporary comment out

player turn
addClasses("active");

and u will see it dont have that class

Pieter Stokkink
@forkerino
Nov 19 2017 17:56
@thodorisanta yeah, the code is just executing too fast for you to notice that the class has been removed and added back in.
@tiagocorreiaalmeida :wave:
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 17:57
@forkerino @moigithub but i can still press the buttons while its the computers turn. why that happens?
Moisés Man
@moigithub
Nov 19 2017 18:00
...adding classes wont prevent u be able to click stuff
that would require extra code
probably a flag/variable
computerTurn (or playerTurn) which would be true or false
and on ur "click events" check
if (computerTurn) return <-- soo it will kick out the user when not his turn (do nothing)
Spyrantis Theodoros
@thodorisanta
Nov 19 2017 18:03
@moigithub okay i will try that now
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 18:14
@forkerino been a while :D how are you ?
Pieter Stokkink
@forkerino
Nov 19 2017 18:14
@thodorisanta you could also return if the active class is not set, but you just have to make sure that it will only be set after the computer's turn ends. Only go to playerturn when the computer is done playing the sequence.
@tiagocorreiaalmeida I'm ok, although my calendar is quite full. How are you?
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 18:16
I think it's better that way than being empty and feel bored :D, that way whn you have some free time you will enjoy it alot more I guess, all good
Pieter Stokkink
@forkerino
Nov 19 2017 18:17
Agreed. Although I'm not bored too easily. Always find something to learn or do.
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 18:19
have you joined some type of martial art?
Pieter Stokkink
@forkerino
Nov 19 2017 18:21
I did one aikido lesson, but not too sure I will continue, as it is quite strict. Will go check out Tai Chi coming thursday (not very martial, but I like the movements).
Markus Kiili
@Masd925
Nov 19 2017 18:21
@tiagocorreiaalmeida Does Beyonce-Yoga count as a martial art?
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 18:24
oh ok, I used to do aswell it's called self defense but you learn a bit of 2 martial arts(can't remember the name) you learn keys to block your oponents.ground figthing and others did it for 3months or more stopped for a while since I needded to focus on programming more and going to the classes would make me come home really late and tired
@Masd925 if you move your body like she does on the videos I would say it still would count as a martial art
I would run from it
Markus Kiili
@Masd925
Nov 19 2017 18:26
@tiagocorreiaalmeida As far as I know, in Beyonce-Yoga, they don't so much imitate the movements. Beyonce is kind of a force animal they draw power from.
But any power animal can be of use in self-defence.
Pieter Stokkink
@forkerino
Nov 19 2017 18:28
hahaha!
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 18:28
I would pick shakira in that situation then
Pieter Stokkink
@forkerino
Nov 19 2017 18:28
Nice
Markus Kiili
@Masd925
Nov 19 2017 18:29
@tiagocorreiaalmeida Shakira would suit military training in that respect. Ability to crawl in mud etc.
DistinctWolf
@DistinctWolf
Nov 19 2017 18:30
lol
Tiago Correia
@tiagocorreiaalmeida
Nov 19 2017 18:31
@Masd925 wjat are the others options? im getting interested in this might join it
Margaret Greaney
@greaneym
Nov 19 2017 19:07
hello, has anyone tried to get a hexi or pixi.js game working on codepen.io? I think I might have made a bad decision to try a game without getting it working on codepen first. I removed all the react from it and still can't get it to work. Any help would be appreciated. I have most of it working on my local system. I would be sad if I had to start all over again.
here is the not working pen,https://codepen.io/greaney/pen/jabMoV
I will come back later or tomorrow. thank you.
David
@David-Moreira
Nov 19 2017 19:19
Hello guys.
I need a little help please!! I'm not the Inventory Update Challenge
I'm having some difficulties sorting a 2d array. Why is it not working? :( Code Below. Any tips!? Appreciate your help!
function updateInventory(arr1, arr2) {
    // All inventory must be accounted for or you're fired!

    function sortFunction(a, b) {
        a = a[0];
        b = b[0];

        return (a === b) ? 0 : (a < b) ? -1 : 1;
    }


  var count = 0;
  arr2.forEach(function(newItem){
    count = 0;
    arr1.forEach(function(currentItem){
        if (currentItem.includes(newItem[1])){
        currentItem[0] += newItem[0];
        count++;

      }
    });
    if (count == 0) {
      arr1.push(newItem);
    }

  });

arr1 = arr1.sort(sortFunction());

return arr1 ;
}



// Example inventory lists
var curInv = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"],
    [1, "Hair Pin"],
    [5, "Microphone"]
];

var newInv = [
    [2, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [67, "Bowling Ball"],
    [7, "Toothpaste"]
];

updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]);
Pieter Stokkink
@forkerino
Nov 19 2017 19:53
@David-Moreira don't put () in arr1.sort(sortFunction)
at this point you are calling the function, instead of passing a reference to .sort
Kelechi Chinaka
@ke1echi
Nov 19 2017 20:05
evenCheck=n=>Number.isInteger(n/2);
35 charaacters... needs to be <=19
elpatron17
@elpatron17
Nov 19 2017 20:12
hi guys
could you help me, please
Brad
@bradtaniguchi
Nov 19 2017 20:17
@elpatron17 hello
this is my code
I am trying to store user input into array and display it
but it does't work
@bradtaniguchi
Brad
@bradtaniguchi
Nov 19 2017 20:25
@elpatron17 So you don't really use the displayStackArr variable right?
besides "printing" in the fowardOutpuit

For starters the page is refreshing for me every "forward" button click. Add

type="button"

to all buttons on the page to prevent them from "submitting" the form and reloading the page

you already have it in some places, but the forward button doesn't have it
elpatron17
@elpatron17
Nov 19 2017 20:31
thank you @bradtaniguchi
CamperBot
@camperbot
Nov 19 2017 20:31
elpatron17 sends brownie points to @bradtaniguchi :sparkles: :thumbsup: :sparkles:
:cookie: 314 | @bradtaniguchi |http://www.freecodecamp.org/bradtaniguchi
elpatron17
@elpatron17
Nov 19 2017 20:31
thats fixed the problem with displaying
but I want to make a list
from the user inputs
Brad
@bradtaniguchi
Nov 19 2017 20:32
In that case you will need to have to change your forward function so it goes through all the strings in the displayStackArr. Right now your just showing the input
elpatron17
@elpatron17
Nov 19 2017 20:33
How I can do it?
Brad
@bradtaniguchi
Nov 19 2017 20:36
I would use a loop and add onto a larger string.
I would also combine the forward function and my function.
once the loop is finished assign the larger string to the same thing your assigning the input to right now in the forward function.
elpatron17
@elpatron17
Nov 19 2017 20:44
thank you very much @bradtaniguchi
CamperBot
@camperbot
Nov 19 2017 20:44
elpatron17 sends brownie points to @bradtaniguchi :sparkles: :thumbsup: :sparkles:
api offline
elpatron17
@elpatron17
Nov 19 2017 20:44
I will try
Brad
@bradtaniguchi
Nov 19 2017 20:44
@elpatron17 np, and goodluck :)
Kelechi Chinaka
@ke1echi
Nov 19 2017 20:49
evenCheck=n=>Number.isInteger(n/2);
35 charaacters... needs to be <=19 @chrono @sjames1958gm
David
@David-Moreira
Nov 19 2017 20:50
@forkerino Oh my god.... Thank you
CamperBot
@camperbot
Nov 19 2017 20:50
david-moreira sends brownie points to @forkerino :sparkles: :thumbsup: :sparkles:
:star2: 2952 | @forkerino |http://www.freecodecamp.org/forkerino
DistinctWolf
@DistinctWolf
Nov 19 2017 22:18
I need help please, It's returning more results than expected
function permAlone(str) {
  var array = str.split('');
  function swap(a, b) {
    var first = str.slice(a, a + 1);
    array[a] = array[b];
    array[b] = first;
    return array;
  }
  function generate(s, n) {
    if (n == 1) {
      return s;
    } else {
      for (var i = 0; i < n; i++) {
        generate(s, n - 1);
        if (n % 2 === 0) {
          swap(i, n - 1);
          console.log(array);
        } else {
          swap(0, n - 1);
          console.log(array);
        }
      }
      generate(s, n - 1);
    }
  }
  return generate(array, array.length);
}
permAlone('abc');
Stephen James
@sjames1958gm
Nov 19 2017 22:49
@FlashHero Heaps algorithm calls for the for loop to go from 0 to i < n - 1
DistinctWolf
@DistinctWolf
Nov 19 2017 22:50
@sjames1958gm i did set it to i < n-1 but it didn't work there were less arrangements than expected
Stephen James
@sjames1958gm
Nov 19 2017 22:52
@FlashHero Yeah, I am seeing the same, but at least closer. Also your swap is wrong. You shouldn't be using str after you split it any more
@FlashHero
  function swap(a, b) {
    var tmp = array[a]
    array[a] = array[b];
    array[b] = array[a];
  }
DistinctWolf
@DistinctWolf
Nov 19 2017 22:54
@sjames1958gm now it's repeating for example bbc when there's no two b's
Stephen James
@sjames1958gm
Nov 19 2017 22:54
@FlashHero Der - that second assignment should use tmp
array[b] = tmp
DistinctWolf
@DistinctWolf
Nov 19 2017 22:55
@sjames1958gm now it's closer 5 arrangements
Stephen James
@sjames1958gm
Nov 19 2017 22:55
It is not getting the input arragment
DistinctWolf
@DistinctWolf
Nov 19 2017 22:57
nope i have abc and there's 5 arrangements total and i'm also curious why the return s doesn't return anything when n equal 1
Stephen James
@sjames1958gm
Nov 19 2017 23:00
@FlashHero You are only console.log values you are not getting the results.
generate doesn't return anything except when n = 1
So as you return back up the stack you get nothing.
You are getting undefined as a result -
DistinctWolf
@DistinctWolf
Nov 19 2017 23:01
@sjames1958gm if i return those values then i have less values
Stephen James
@sjames1958gm
Nov 19 2017 23:02
@FlashHero You have to accumulate the values you get when n == 1
DistinctWolf
@DistinctWolf
Nov 19 2017 23:03
@sjames1958gm accumulate ??
Stephen James
@sjames1958gm
Nov 19 2017 23:04
@FlashHero store them (possibly in an array) so you can return them generate ultimately finishes.
DistinctWolf
@DistinctWolf
Nov 19 2017 23:05
@sjames1958gm so store the array after swapping or when n==1
Stephen James
@sjames1958gm
Nov 19 2017 23:05
only when n == 1
DistinctWolf
@DistinctWolf
Nov 19 2017 23:05
ok
@sjames1958gm it gives me the right amount of arrangements which is 6 but they're all cba
Stephen James
@sjames1958gm
Nov 19 2017 23:09
@FlashHero You have to make sure you "copy" the array when you store it, or they will all reference the same array.
DistinctWolf
@DistinctWolf
Nov 19 2017 23:11
@sjames1958gm so when i swap the letter it automatically updates the array variable so i just pushed it to a new array when n==1 and it's still the same repeating cba
DistinctWolf
@DistinctWolf
Nov 19 2017 23:19
@sjames1958gm this is what i have
function permAlone(str) {
  var array = str.split('');
  function swap(a, b) {
    var tmp = array[a]
    array[a] = array[b];
    array[b] =tmp;
    return array;
  }
  var a=[];
  function generate(s, n) {
    if (n == 1) {
     a.push(array);
    } else {
      for (var i = 0; i < n-1; i++) {
        generate(s, n - 1);
        if (n % 2 === 0) {
           swap(i, n - 1);
        } else {
          swap(0, n - 1);
        }
      }
        generate(s, n - 1);
    }
  }
   generate(array, array.length);
return a;
}
permAlone('abc');
Stephen James
@sjames1958gm
Nov 19 2017 23:24
@FlashHero try a.push(s.slice());
DistinctWolf
@DistinctWolf
Nov 19 2017 23:25
@sjames1958gm dang finally it worked can you tell me what was wrong
Stephen James
@sjames1958gm
Nov 19 2017 23:27
@FlashHero Without the slice() you were pushing a reference to the same array, so each entry got the final result. slice() makes a copy
DistinctWolf
@DistinctWolf
Nov 19 2017 23:28
@sjames1958gm doesn't the swap update it automatically though
DistinctWolf
@DistinctWolf
Nov 19 2017 23:35
@sjames1958gm anyway thank you so much for help man, I appreciate it
CamperBot
@camperbot
Nov 19 2017 23:35
flashhero sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8699 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Stephen James
@sjames1958gm
Nov 19 2017 23:38
swap is always updating the same array -
your're welcome