These are chat archives for FreeCodeCamp/HelpJavaScript

24th
Jun 2018
Long Nguyen
@longnt80
Jun 24 2018 07:30
@dindrala maybe you should put the function inside the script tag
Ogundele Olumide
@Lumexralph
Jun 24 2018 10:36
@dindrala just like @longnt80 said, any javascript you're trying to run should live in the <script> tag
@cmccormack what about npm or git you want to ask if I can be of help
Nate Mallison
@NJM8
Jun 24 2018 11:25
@Pjdaze the param names to the recursive function call are the same but the actual params are not. Notice in my number example when you call the function inside itself you call num - 1
an iterative function performs some work step by step inside itself. All the steps are performed inside one function call. A recursive function only performs one step at a time, then calls itself with the params changed to perform the next step, until some exit case is hit, example in mine: if (num === 0) { return then the function exits and is done
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:37
Guys help with this challenge
Intermediate Algorithm Scripting: DNA Pairing
I didn't understand what I should to do. I mean what is base pair?
Nate Mallison
@NJM8
Jun 24 2018 11:41
Base pair are the letters "AT" or "CG"
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:41
But how I match them?
How I know that C comes with G and that A comes with T
I really don't understand what are these.
Nate Mallison
@NJM8
Jun 24 2018 11:42
so if I gave you a list of letters and said they are missing their pairs
"A", "T", "G"
you would answer
"AT", "TA", "GC"
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:43
How you know that it is TA and not AT
How you know how to pair them?
Nate Mallison
@NJM8
Jun 24 2018 11:43
just add the missing letter of the pair after the letter you have
look at the order
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:44
So let me get this straight
Nate Mallison
@NJM8
Jun 24 2018 11:44
from the challenge: For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]]
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:44
for "C", "G", "T"
Nate Mallison
@NJM8
Jun 24 2018 11:44
note that the first letter in each set is GCG, your input
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:44
I will make CG
then GC
then TG, right?
"CG","GC","TG"
Nate Mallison
@NJM8
Jun 24 2018 11:45
almost, what does T pair with
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:45
Yeah but the pair you made eariler is ATG
With A?
Nate Mallison
@NJM8
Jun 24 2018 11:46
yes, when I wrote "A", "T", "G", they are not pairs, they are missing their pairs
"A", "T", "G" -> "AT", "TA", "GC"
see?
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:47
But I don't understand, why you don't pair G with T
As I understand, you pair each letter with the previous letter
like A comes with T, because T is next
then T pairs with A because A is previous
Right?
Nate Mallison
@NJM8
Jun 24 2018 11:47
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
you don't pair G with T because they define AT as a pair
and CG
no next or previous
just split your input
and add the missing letter from the pair
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:49
Oooooh, I understand now.
If A then T comes because AT
If T comes then A pairs with it because the reverse of AT, TA
If C comes then G pairs because CG
Nate Mallison
@NJM8
Jun 24 2018 11:49
AGG -> [[A],[G],[G]] -> [[A,T],[G,C],[G,C]]
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:49
If G comes then C pairs because the reverse of CG, GC
Nate Mallison
@NJM8
Jun 24 2018 11:50
you got it!
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:50
Thank you a lot for the help!
Wait, wait min.
Nate Mallison
@NJM8
Jun 24 2018 11:50
:smile:
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:50
Isn't supposed to be CG?
Why you paired G with A
Nate Mallison
@NJM8
Jun 24 2018 11:50
oh
opps
there
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:51
Thanks!
I now understood it.
Nate Mallison
@NJM8
Jun 24 2018 11:51
just, ahhhhh, testing you. Hahah
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:51
Hahaha, it seems that I passed the test.
Nate Mallison
@NJM8
Jun 24 2018 11:51
:fire:
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 11:57
I'm an idiot.
I made an infinite loop.
koder3
@koder3
Jun 24 2018 11:59
Hi how do you check if two objects are equal:
var obj = {fruits: 1}
var obj2 = {fruits: 1}
//how to check if var obj is equal to var obj2
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 12:08
I guess return obj === obj2
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 12:16
How I could make sub-arrays in an array?
koder3
@koder3
Jun 24 2018 12:20
@NJM8 Thanks!
@OGTechnoBoy you mean [ [], [], [] ] ?
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 12:20
Yeah
koder3
@koder3
Jun 24 2018 12:21
Can you paste your code here
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 12:23
Nevermind. I just made it by using a loop
It's a long code but it works so I think it's good
koder3
@koder3
Jun 24 2018 12:24
Okay!
Simon Cordova
@gbsimon87
Jun 24 2018 12:38
Good afternoon
koder3
@koder3
Jun 24 2018 15:56
Just finished the Cash register algorithm challenge!!!!
Thanks to all who helped
FeelsGoodMan
but my code is pretty ugly hehe
image.png
but you can get the correct change for any value as long as there is enough cash in the drawer
Simon Cordova
@gbsimon87
Jun 24 2018 16:00
@koder3 good stuff!
koder3
@koder3
Jun 24 2018 16:01
Thanks!
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 16:18
Guys why this code doesn't work

function convertHTML(str) {
// :)
str = str.split("");
for (var i = 0; i < str.length;i++) {
switch(str[i]) {
case "&":
str.splice(str[i],1,"&");
continue;
case "<":
str.splice(str[i],1,"<");
continue;
case ">":
str.splice(str[i],1,">");
continue;
case '"':
str.splice(str[i],1,""");
continue;
case "'":
str.splice(str[i],1,"'");
continue;
}
}
return str.join("");
}

convertHTML("Dolce & Gabbana");

Help
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 16:26
Nevermind. I fixed it myself.
Ozarion
@Ozarion
Jun 24 2018 17:57
Man regexp is whole another language... I can't wrap my head around group captures. Can anyone show me the path to enlightenment here?
BTW What's up? XD
Tiago Correia
@tiagocorreiaalmeida
Jun 24 2018 17:59
@Ozarion try out regex101.com
Ozarion
@Ozarion
Jun 24 2018 18:00
@tiagocorreiaalmeida Okay. Let me check that out...
It's nice. However I'm still having problem solving the third last challenge in Regex section
Tiago Correia
@tiagocorreiaalmeida
Jun 24 2018 18:09
@Ozarion keep giving it a few tries its worth it you will start to get used to some of the things regex can do and later on might be usefull
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:10
Is there a way to reference the current function(argument)?
Ozarion
@Ozarion
Jun 24 2018 18:11
@OGTechnoBoy yes. The 'this' keyword
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:11
I tried it but it doesn't work.
Ozarion
@Ozarion
Jun 24 2018 18:11
Are you using arrow functions?
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:11
Yep.
Ozarion
@Ozarion
Jun 24 2018 18:12
Use the other function. Arrow functions don't support this keyword on themselves.
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:12
Are you sure?
Ozarion
@Ozarion
Jun 24 2018 18:13
Yep. Arrow functions carry the 'this' of their containing block.
Give it a try. ;)
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:14
Then how I will be able to do it in ES6?
Aditya
@ezioda004
Jun 24 2018 18:15
@OGTechnoBoy What do you want to reference again? this changes value depending on context and yes arrow functions dont bind this in them, the this is carried as the previous binding.
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:15
I have an object constructor
with the argument(FirstandLast)
I'm making a method in it where it has the same argument(FirstandLast)
Ozarion
@Ozarion
Jun 24 2018 18:16
ES6 doesn't say 'use only arrow functions' XD
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:17
Its job is to set the current FirstandLast(in the constructor) to the inside FirstandLast(inside the method) that has been choosen by the user.
The problem is that when I write FirstandLast = FirstandLast, the method doesn't know which argument to follow so it goes by the containing method
Aditya
@ezioda004
Jun 24 2018 18:18
@OGTechnoBoy I'm still a bit confused haha, is this a fcc challenge? If so can you provide a link?
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:19
The link

var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
var firstAndLast = firstAndLast.split(" ");
this.getFullName = function() {
return firstAndLast.join(" ");
};
this.getFirstName = function() {
return firstAndLast[0];
};
this.getLastName = function() {
return firstAndLast[1];
};
this.setFirstName = function(first) {
firstAndLast[0] = first;
}
this.setLastName = function(last) {
firstAndLast[1] = last;
}
this.setFullName = function(firstAndLast) {
firstAndLast = firstAndLast;
}
};

var bob = new Person('Bob Ross');
bob.setFullName("Haskell Curry");
bob.getFullName();

As you see, the method setFullName doesn't work correctly
Because it doesn't reference the original argument, only the argument that is inside the method.
Ozarion
@Ozarion
Jun 24 2018 18:26
Notice that your object's firstAndLast is an "Array" and you are assigning a string to it directly. That is what is causing the problem here.
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:26
What I should to do now.
Ozarion
@Ozarion
Jun 24 2018 18:28
Not very beautiful but
this.setFullName = function(firstAndLast) {
let temp = firstAndLast.split(" ");
firstAndLast[0] = temp[0];
firstAndLast[1] = temp[1];
}
Try that out and let me know. :)
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:41
Why don't you put firstAndLast = temp.join(" ");
Ozarion
@Ozarion
Jun 24 2018 18:42
Yes that can work But I though it would be clearer explicitly.
Did that work btw?
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 18:43
It didn't work
I tried again, it still doesn't work
Also I don't understand what you did there.
The method's job was to set the whole name to the name provided by argument
From what I understand, by setting temp to the split firstAndLast then you split the argument, not the first argument
Sorry but I just don't understand what you did
Aditya
@ezioda004
Jun 24 2018 18:47

@OGTechnoBoy You can perhaps do something like this

 this.setFullName = function (fullName) {
    firstAndLast = fullName.split(" ");
  }

The parameter can be anything, doesnt really matter.

dindrala
@dindrala
Jun 24 2018 19:01
@longnt80 @Lumexralph The function lives in a js file. I just have them grouped together here to make it easy to read. Ive tried pulling the function out of the JS file and putting it in script tags and still get the same error.
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 19:07
I can't do that.
@ezioda004 I can't do that, the challenge says that it has to be firstAndLast
dindrala
@dindrala
Jun 24 2018 19:09
Hi I have a JS that returns input in capital letters. The following works in Firefox but in Chrome it throws this error: LetterCapitilize is not defined at HTMLButtonElement.onclick. Although it works in Chrome in the www.w3schools.com online text editor. Any help would be appreciated. I am stumped.
<!DOCTYPE html>
<html>
<body>

<input id="letters">

<button type="button" onclick="LetterCapitilize()">Submit</button>

<p id="demo"></p>

<script>
function LetterCapitilize (str) {
  str = document.getElementById("letters").value;
  str = str.match(/\w\S*/g).map(function (x) {return x.toUpperCase();});
  document.getElementById("demo").innerHTML = str;
}


</script>

</body>
</html>
Ogundele Olumide
@Lumexralph
Jun 24 2018 19:09
@dindrala what error are you getting?
dindrala
@dindrala
Jun 24 2018 19:10
@Lumexralph LetterCapitilize is not defined at HTMLButtonElement.onclick
Aditya
@ezioda004
Jun 24 2018 19:10
@OGTechnoBoy Weird, this seems to be working for me

var Person = function (firstAndLast) {
  // Complete the method below and implement the others similarly
  var firstAndLast = firstAndLast.split(" ");
  this.getFullName = function () {
    return firstAndLast.join(" ");
  };
  this.getFirstName = function () {
    return firstAndLast[0];
  };
  this.getLastName = function () {
    return firstAndLast[1];
  };
  this.setFirstName = function (first) {
    firstAndLast[0] = first;
  }
  this.setLastName = function (last) {
    firstAndLast[1] = last;
  }
  this.setFullName = function (fullName) {
    firstAndLast = fullName.split(" ");
  }
};
dindrala
@dindrala
Jun 24 2018 19:11
@Lumexralph Ive reposted the question. It is right above your question. Thank you for the help:)
Ogundele Olumide
@Lumexralph
Jun 24 2018 19:12
@dindrala seen it
Aditya
@ezioda004
Jun 24 2018 19:13
@dindrala Your code is working in my chrome.
Ogundele Olumide
@Lumexralph
Jun 24 2018 19:16
@dindrala just like @ezioda004 said, your code is working in my chrome
@dindrala i hope you're aware the str parameter is not doing anything in the function, maybe you remove it
dindrala
@dindrala
Jun 24 2018 19:17
@ezioda004 @Lumexralph How did you guys test it? I am using the about:blank page and just copying / pasting my code into the browser console.
@Lumexralph Thanks! Yup, youre right. Realized that yesterday and forgot to remove it.
Ogundele Olumide
@Lumexralph
Jun 24 2018 19:18
why don't you use an editor?
and run it from the editor
Aditya
@ezioda004
Jun 24 2018 19:19
Just copied pasted in a new file and ran that file.
Or that too.
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 19:19
@ezioda004 It doesn't work in freecodecamp sadly
dindrala
@dindrala
Jun 24 2018 19:20
Usually use editor for more serious stuff but will start to use consistently since there are discrepancies console vs editor. Thank you so much for the help you two! @Lumexralph @ezioda004
Aditya
@ezioda004
Jun 24 2018 19:21
@OGTechnoBoy I tested and its passing all the test, thats why its weird why its not working for you.
Ogundele Olumide
@Lumexralph
Jun 24 2018 19:21
@dindrala :+1:
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 19:21
@ezioda004 Oh yeah, it worked, thanks!
It seems that I was making calling the methods which caused the problem
Aditya
@ezioda004
Jun 24 2018 19:22
Glad it works! :thumbsup:
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 19:23
Just a question, why I had to make write FullName.split(" ")?
Aditya
@ezioda004
Jun 24 2018 19:27
@OGTechnoBoy You had the identifier issue so any different parameter name except firstAndLast would do the job. Specifically why we did .split(" ") is because thats how you've initialized firstAndLast at the beginning var firstAndLast = firstAndLast.split(" "); so we have to split it and make it an array.
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 19:28
But I don't understand
when the user selects the name
the name enters in FullName then firstAndLast takes the value of FullName
So firstAndLast will be split as well because of the first var firstAndLast = firstAndLast.split(" ");
Aditya
@ezioda004
Jun 24 2018 19:32
No..you're reassigning to firstAndLast
OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 19:32
Yeah, that's what I said.
Aditya
@ezioda004
Jun 24 2018 19:33
var firstAndLast = firstAndLast.split(" "); this executes only once when you create a new instance of Person.
So the above line would only run once when you do var bob = new Person("Bob Ross");

Like in this method

this.setFirstName = function (first) {
    firstAndLast[0] = first;
  }

You're assigning a different value to the first index of the array firstAndLast

And in this example

  this.setFullName = function (fullName) {
    firstAndLast = fullName.split(" ");
  }

We're completely replacing that array with a new one.

OGTechnoBoy
@OGTechnoBoy
Jun 24 2018 19:40
Oooh
I thought that it applies after the object gets created as well lol
It was a pretty dumb thought since objects can't have variables like these
Thank you for the explaination!
koder3
@koder3
Jun 24 2018 20:57
any leetcode user here
Krishaan Khubchand
@KrishaanKhubchand
Jun 24 2018 21:20
hey guys, tryna include a hyperlink within an array of javascript strings, but neither .innerHTML nor .link seem to be working for me -- any advice?
Aditya
@ezioda004
Jun 24 2018 21:48
@KrishaanKhubchand Use Element.getAttribute("href"); and append it to some array?
anthonygallina1
@anthonygallina1
Jun 24 2018 21:52
OR make a str let and var result = str.link("https://YOUR LINK HERE");
Aditya
@ezioda004
Jun 24 2018 21:53
Didnt know there was a method called .link(), seems like its deprecated.
anthonygallina1
@anthonygallina1
Jun 24 2018 21:55
@ezioda004 Might be. But works Works is allways agood starting point for me
ArchivalBoat50
@ArchivalBoat50
Jun 24 2018 23:44
Hey guys, I'm trying to remove all non integers but am having trouble. Does anyone mind helping?
const array = [1, 1.5, 2, 2.5];
const integer = array.map(function(num) {if (num % 1 == 0) {
return num; }});
console.log(integer);
Aditya
@ezioda004
Jun 24 2018 23:46
@ArchivalBoat50 Logic seems fine, using .filter() instead of .map() should do the trick.
ArchivalBoat50
@ArchivalBoat50
Jun 24 2018 23:48
@ezioda004 Why does .filter() work? Whats making the difference?
Aditya
@ezioda004
Jun 24 2018 23:52
@ArchivalBoat50 .filter() returns a new array based on if the callback function returns true for that value and other values are filtered. Whereas .map()returns a new array with all elements only modifying the values.
ArchivalBoat50
@ArchivalBoat50
Jun 24 2018 23:53
@ezioda004 Oh I understand now, thanks
Aditya
@ezioda004
Jun 24 2018 23:54
:thumbsup:
ArchivalBoat50
@ArchivalBoat50
Jun 24 2018 23:58
@ezioda004 Do you know how I could do the same using an arrow function?