These are chat archives for FreeCodeCamp/HelpJavaScript

2nd
Feb 2018
TonyRednil
@TonyRednil
Feb 02 03:04
Hey, I'm having some trouble with jquery in code pen. Can someone help me please?
DavinZhu
@DavinZhu
Feb 02 03:06
I want to Help You ,but I will Not.
Now I also encountered a problem.
MaciejReimann
@MaciejReimann
Feb 02 07:16
Hi there!
I am having a hard time understanding why this piece of code doesn't do what I expect from it.
If I open the console and assign to a variable new ArrayOfEquations i.e. :
let variable = new ArrayOfEquations() and put variable.getInOrder() I get what I want.
If I however, hit variable.getShuffled() first, the array get shuffled forever and then variable.getInOrder() won't give me back the original array.
I cant understand why if I shuffle the copy of the array stored in temp, the array got shuffled also. Please help!
const Equation = function(i, j) {
  this.x = i;
  this.y = j;
  this.z = i * j;
  this.inputPosition = "";
  this.index = (i-1).toString() + (j-1).toString();
};

const shuffle = function(array) {
   for (let i = array.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        let temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
  return array;
};

const ArrayOfEquations = function(givenArray) {
  let globalIndex = 0; 
  let tempIndex = 0;
  let array;
  if (givenArray) {
   array = givenArray
  } else {
    array = new Array;
    for (let i = 1; i < 10; i++ ) {       
      for (let j = 1; j < 10; j++ ) {
        let equation = new Equation(i, j);
        array.push(equation);
      };
    };
  };
 const temp = array ;
 const shuffled = function(n) { return shuffle(n) }
 this.getShuffled = function() {return shuffled(temp) }; 
 this.getInOrder = function() {return array  };
};
Markus Kiili
@Masd925
Feb 02 07:20
@MaciejReimann If you mean const temp=array;, that only copies the object reference, so both variables point to the same array.
MaciejReimann
@MaciejReimann
Feb 02 07:26
@Masd925 Thanks! Any tips on how to build this constructor function in a way that it can store the original array?
CamperBot
@camperbot
Feb 02 07:26
maciejreimann sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 4684 | @masd925 |http://www.freecodecamp.org/masd925
Markus Kiili
@Masd925
Feb 02 07:26
@MaciejReimann const temp = array.slice(); would make a separate copy.
If array elements happen to be objects, it only copies the references.
MaciejReimann
@MaciejReimann
Feb 02 07:28
@Masd925 A ha! So brutally simple... :)
Adam Faraj
@adamfaraj
Feb 02 09:56
any React users in here?
Tiago Correia
@tiagocorreiaalmeida
Feb 02 10:18
@adamfaraj I know a bit of it
if I can help
Stephen James
@sjames1958gm
Feb 02 10:49
@adamfaraj I have React experience
Abhi
@abhishek737
Feb 02 11:12
I am writing a regex the scenario is
6-16 chars length
Must be one number,
and must be one alphabet, so I have written this
`!!/^(.)(?=.[A-Za-z])(?=.\d)[A-Za-z\d](.){6,16}$/```
what i am doing wrong
?
Nate Mallison
@NJM8
Feb 02 12:46
@abhishek737 It always helps me to write each test individually then combine them with the look ahead
I think you'll want something like so? ^(?=.*\d)(?=.*[a-zA-Z]+)[a-z\d]{6,16}$/, 'g'
oops, first \d should have a + on it
^(?=.*\d+)(?=.*[a-z]+)[a-z\d]{6,10}$/, 'g'
Diego Mayer
@Chrono79
Feb 02 13:24
@NJM8 that 10 should be a 16 I think
Nate Mallison
@NJM8
Feb 02 13:25
@Chrono79 yes you are right, thanks
CamperBot
@camperbot
Feb 02 13:25
njm8 sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4609 | @chrono79 |http://www.freecodecamp.org/chrono79
Nate Mallison
@NJM8
Feb 02 13:26
I was just copying my code from a similar password verification challenge. :)
Diego Mayer
@Chrono79
Feb 02 13:27
:+1:
and maybe use i flag if it's case insensitive
Nate Mallison
@NJM8
Feb 02 13:28
and I forgot to add the A-Z again. terrible. :fire:
Diego Mayer
@Chrono79
Feb 02 13:28
That's the other option
Nate Mallison
@NJM8
Feb 02 13:28
/^(?=.*\d+)(?=.*[a-z]+)[a-z\d]{6,16}$/, 'gi'
I think i flag would be better so you don't have to put A-Z in both the letter check and the length check
Razvan Jackson
@RazvanJackson
Feb 02 14:31
Hello guys!
i have this
qtyUpdate(index, value){
        if(!this.state.items[index].qty - value < 0){
            let newItems = this.state.items;
            newItems[index].qty += value;

            this.setState({
                items : newItems
            })
        }
    }
why it's not substracting
but it's adding
<button className="button is-primary" onClick={()=>this.props.qtyUpdate(this.props.index, 1)}>Add Qty</button>
adding is working substracting isn't
<button className="button is-warning" onClick={()=>this.props.qtyUpdate(this.props.index, 1*-1)}>Substract Qty</button>
Diego Mayer
@Chrono79
Feb 02 14:37
@RazvanJackson if(!this.state.items[index].qty - value < 0){ should be if(!this.state.items[index].qty + value < 0){
you're passing a negative value
That doesn't seem the only problem, but that's one less
and the ! maybe is misplaced
you could test if(this.state.items[index].qty + value >= 0){
Razvan Jackson
@RazvanJackson
Feb 02 14:41
@Chrono79 thanks
CamperBot
@camperbot
Feb 02 14:41
razvanjackson sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 4610 | @chrono79 |http://www.freecodecamp.org/chrono79
Quantomistro3178
@Quantomistro3178
Feb 02 15:05
I need help, anyone here?
I wanna iterate through an array, comparing the current value to the next value. How do I do that?
Razvan Jackson
@RazvanJackson
Feb 02 15:06
@Quantomistro3178 Hey
@Quantomistro3178
for(let i=0;i<arr.length;i++){
 if(arr[i]>arr[i+1]) // Do smth
}
Ken Haduch
@khaduch
Feb 02 15:07
@Quantomistro3178 - you should just be able to do arr[i] and arr[i+1], at least at a simple level. You have to make sure that you do not access beyond the end of the array. So maybe a little more complete description of what you are trying to do would be helpful? Because accessing arr[i+1] when i is the index of the last element in the array would result in an error...
Quantomistro3178
@Quantomistro3178
Feb 02 15:11
I've got an array [1,1,2,2,3,3,4,5,5] and I wanna remove all elements appearing once....
Wait.... @khaduch I realize comparing the current with the next wont do that....
Nevermind, I'll try to think of something else...
Diego Mayer
@Chrono79
Feb 02 15:12
@Quantomistro3178 there are several ways, some less performant than others
Zunaid Aslam
@Zunaid-Aslam
Feb 02 15:12

For the challenge Confirm Ending,
I have used this code :

```function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var ln = target.length;

var test = str.substring(str.length - ln);

if (test===target) {
return "true";}
else {return "false";}

}

confirmEnding("Bastian", "n");
```

It is not working. I kind of tested it with the texts and the answer seems to match.

Diego Mayer
@Chrono79
Feb 02 15:13
@Zunaid-Aslam Leave the ``` in their own line
"true" and "false"are strings
Zunaid Aslam
@Zunaid-Aslam
Feb 02 15:13
Oh
tx @Chrono79
Diego Mayer
@Chrono79
Feb 02 15:14
np
Zunaid Aslam
@Zunaid-Aslam
Feb 02 15:14
I did leave the ``` in their own line @Chrono79
Diego Mayer
@Chrono79
Feb 02 15:14
not the fist ones
Zunaid Aslam
@Zunaid-Aslam
Feb 02 15:14
``` (shift+enter for line break)
var code = "formatted";
```
Testing
Now I get it
Quantomistro3178
@Quantomistro3178
Feb 02 15:14
@Zunaid-Aslam You don't need the last if statement, you can just write return test===target, since this will return either true or false
Diego Mayer
@Chrono79
Feb 02 15:15
There was a challenge about that
Zunaid Aslam
@Zunaid-Aslam
Feb 02 15:15
Yeah more simple
awesome solved :D ; Thanks guys
Zunaid Aslam
@Zunaid-Aslam
Feb 02 15:17
I tend to forget many of the previous tutorial
but thanks both of you
Jason Luboff
@JLuboff
Feb 02 16:21
:dart:
FlashHero
@FlashHero
Feb 02 16:42
In which file do you create routes for reactjs and if it's in a separate file other than entry file then do I need to export any thing
FlashHero
@FlashHero
Feb 02 17:04
Or do you make the routes file the entry file
Christopher McCormack
@cmccormack
Feb 02 17:32
@FlashHero you can make the routes wherever you want with React Router v4, but remember that your Links should be within your Router.
Jason Luboff
@JLuboff
Feb 02 17:32
@cmccormack :wave:
Christopher McCormack
@cmccormack
Feb 02 17:32
I have my routes in a separate file but only because I want to keep it clean.
@JLuboff yo!
Jason Luboff
@JLuboff
Feb 02 17:33
@cmccormack Whats good?
Christopher McCormack
@cmccormack
Feb 02 17:33
@JLuboff it's all good man, keeping busy
@JLuboff how are you?
Jason Luboff
@JLuboff
Feb 02 17:36
@cmccormack Doing alright. It's friday, can't complain about that. Completed my first C# application (rework of our companies Fleet management application now with better security and a lot of new features as requested by the end users). Reworking the meeting submission side of my conference room sign right now. Decided against putting node/mongo on the raspberry pi's directly and instead going to use a linux VM that I was going to need to build for another project anyhow
Christopher McCormack
@cmccormack
Feb 02 17:37
@JLuboff Linux VM sounds like a better solution, probably easier to replicate if there is a failure as well
@JLuboff awesome man sounds like you're super busy
Jason Luboff
@JLuboff
Feb 02 17:38
Ya man. While I'm not one for job titles...kind of hoping to get a new title lol
Christopher McCormack
@cmccormack
Feb 02 17:39
@JLuboff Head of Busywork?
Jason Luboff
@JLuboff
Feb 02 17:39
Lol
Christopher McCormack
@cmccormack
Feb 02 17:39
no wait Director of Busywork!
Gotta run for appt ttyl
Jason Luboff
@JLuboff
Feb 02 17:42
Nah... just get away from the IT HelpDesk/Network Technician title. While I'm still doing all the helpdesk stuff I'm doing more development/database stuff
See ya
Razvan Jackson
@RazvanJackson
Feb 02 17:46
Hello! Someone here who knows React?
Markus Kiili
@Masd925
Feb 02 18:15
@JLuboff Ask for data structure visionary title.
Jason Luboff
@JLuboff
Feb 02 18:15
@Masd925 Sounds fancy
I like it
Markus Kiili
@Masd925
Feb 02 18:20
@JLuboff Not the best day for me. Girlfriend got hit by a train. Most likely just loses few toes. I told her that I love her as much with ten or seven toes.
Jason Luboff
@JLuboff
Feb 02 18:21
@Masd925 Oh man, I'm sorry to hear that. a few lost toes is better than a lost life. How did she get hit?
Markus Kiili
@Masd925
Feb 02 18:22
@JLuboff Yeah, most people don't get the chance to tell they got hit by one. She slipped on a train station and a train was moving nearby.
Jason Luboff
@JLuboff
Feb 02 18:23
Thats scary
Markus Kiili
@Masd925
Feb 02 18:23
Bad weather here. Easy to slip.
Jason Luboff
@JLuboff
Feb 02 18:23
Glad to hear that it wasn't a lot worsethough
Markus Kiili
@Masd925
Feb 02 18:24
Yeah.
Kelechi Chinaka
@ke1echi
Feb 02 18:28
wow..that could be scary @Masd925 glad to hear she is safe
Markus Kiili
@Masd925
Feb 02 18:32
@kelechy Yeah. Scary it was.
Stephen James
@sjames1958gm
Feb 02 19:09
@Masd925 Yikes.
obscuredetour
@obscuredetour
Feb 02 19:10
Ah jeez, spent over an hour on Profile Lookup via beta.freecodecamp.org before realizing the given function was broken :worried:
Stephen James
@sjames1958gm
Feb 02 19:12
@obscuredetour :( ouch.
obscuredetour
@obscuredetour
Feb 02 19:12
The given function is function lookUpProfile(name, prop) and it should be function lookUpProfile(firstName, prop)
Thought I was going crazy
Diego Mayer
@Chrono79
Feb 02 19:30
why would the parameter name broke things?
Diego Mayer
@Chrono79
Feb 02 19:38

@obscuredetour function lookUpProfile(name, prop) works just fine, the instructions state:

The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.

Jason Luboff
@JLuboff
Feb 02 19:41
@Chrono79 Hola
Diego Mayer
@Chrono79
Feb 02 19:42
:wave:
@Masd925 Glad your GF is alive, could be worse
Jason Luboff
@JLuboff
Feb 02 19:43
Que paso
Diego Mayer
@Chrono79
Feb 02 19:44
@JLuboff wasn't he telling you she slipped in front of a train?
Jason Luboff
@JLuboff
Feb 02 19:44
@Chrono79 Ya, I meant whats up with you haha
Diego Mayer
@Chrono79
Feb 02 19:44
practicing spanish? Nice
Jason Luboff
@JLuboff
Feb 02 19:44
Yo quiero un pequito espanol
Diego Mayer
@Chrono79
Feb 02 19:45
But, it should be in present tense, Qué pasa sounds better
Lol, little by little
Jason Luboff
@JLuboff
Feb 02 19:45
I thought o is for male and a for female?
Diego Mayer
@Chrono79
Feb 02 19:46
@JLuboff not always, the verbs have a lot more declinations than in english
Markus Kiili
@Masd925
Feb 02 19:46
Spain. The country I choose never to visit.
Diego Mayer
@Chrono79
Feb 02 19:49
Why is that?
Jason Luboff
@JLuboff
Feb 02 19:49
@Chrono79 I find spanish very confusing lol
Diego Mayer
@Chrono79
Feb 02 19:49
It's harder than english for sure
Markus Kiili
@Masd925
Feb 02 19:49
@Chrono79 La Tomatina, of course.
I read somewhere that the male runners have a habit of squeezing each other testicles. I will not take the risk by crossing the border.
Diego Mayer
@Chrono79
Feb 02 19:50
LOL
I think you'll be safe if you don't go to Buñol the last wednesday of August
Markus Kiili
@Masd925
Feb 02 19:54
In this case being 99.999999% sure is not enough. Never heard of https://en.wikipedia.org/wiki/Precautionary_principle ?
Diego Mayer
@Chrono79
Feb 02 20:02
San Fermín running of bulls seems dangerous too
Kelechi Chinaka
@ke1echi
Feb 02 20:06
:wave: guys
Diego Mayer
@Chrono79
Feb 02 20:06
Hi Kelechi
Razvan Jackson
@RazvanJackson
Feb 02 20:07
Ce faceti oamenilor?
Just little bit of Romanian :laughing:
Kelechi Chinaka
@ke1echi
Feb 02 20:07
everyone now learning Spanish now? :smile:
:wave: @Chrono79 happy weekend
Diego Mayer
@Chrono79
Feb 02 20:10
Thanks
I'd be glad if I could sleep 6 hours in a row
Kelechi Chinaka
@ke1echi
Feb 02 20:20
:smile:
Jason Luboff
@JLuboff
Feb 02 20:24
@Chrono79 My wife nudged me at 2 am last night to change the baby. I think I almost fell twice
Diego Mayer
@Chrono79
Feb 02 20:27
I have a cough since almost 2 months now, seen different doctors, taken some meds, still there
I'll start the zombie apocalypse any day now
Jason Luboff
@JLuboff
Feb 02 20:27
That sucks
Brian
@BrianCodes33
Feb 02 20:38
function isValid(formula){
  for(var i=0; i<formula.length; i++) {
    if (formula[i] === 1 && formula[i] === 2) {
      return false;
    }
  }
}

material1 and material2 cannot be selected at the same time
material3 and material4 cannot be selected at the same time
material5 and material6 must be selected at the same time
material7 or material8 must be selected(at least one, or both)

i haven’t coded in a while i’m a bit rusty. life has been tough. any recommendations what i’m doing wrong?

Diego Mayer
@Chrono79
Feb 02 20:40
@BrianCodes33 how would formula[i] be 1 and 2 at the same time?
Brian
@BrianCodes33
Feb 02 20:40

For formula = [1,3,7], The output should be true.

For formula = [7,1,2,3], The output should be false.

Brad
@bradtaniguchi
Feb 02 20:40
@BrianCodes33 Im not sure about how the code you gave matches anything you said in the question, what are material1-8? An array or just two arguments/
Diego Mayer
@Chrono79
Feb 02 20:41
@BrianCodes33 Try using indexOf
Brad
@bradtaniguchi
Feb 02 20:42
If any of the cases you mentioned is false, do you care about the other cases? or can you just say its not valid and return false?
Diego Mayer
@Chrono79
Feb 02 20:42
@bradtaniguchi formula is an array of n materials going from 1 to 8
Brad
@bradtaniguchi
Feb 02 20:42
@Chrono79 Yes I see now
Task
You are given a integer array formula. Array contains only digits 1-8 that represents material 1-8. Your task is to determine if the formula is valid. Returns true if it's valid, false otherwise.
And the rules that @BrianCodes33 already posted above
Diego Mayer
@Chrono79
Feb 02 20:56
Note
All inputs are valid. Array contains at least 1 digit. Each digit appears at most once.