These are chat archives for FreeCodeCamp/HelpJavaScript

22nd
Mar 2018
Ken Haduch
@khaduch
Mar 22 2018 01:15
@Lumexralph - hello :wave:
@JLuboff - :wave:
Markus Kiili
@Masd925
Mar 22 2018 06:13
@Lumexralph Hi.
bilaal-s
@bilaal-s
Mar 22 2018 07:32
lol
@khaduch @Masd925 @JLuboff @Lumexralph :wave:
Markus Kiili
@Masd925
Mar 22 2018 07:36
@bilaal-s Hi.
lishundi
@lishundi
Mar 22 2018 12:31
var phoneticLookup= {
alpha:"Adams",
bravo:"Boston",
charlie:"Chicago",
delta:"Denver",
echo:"Easy",
foxtrot:"Frank",
};
Francois Beyers
@Blusoup
Mar 22 2018 14:17
How can I make this better?
.replace(")", "").replace(/^\s/g,"").replace(/,/g,";").replace(/'/g, "").replace(/^\s+|\s+$/g,"").replace(/" "/,"")
alpox
@alpox
Mar 22 2018 14:32
@Blusoup What is the task this has to fulfill?
Removing special characters?
I see what it does, but i don't know if you just accounted for exact cases
Francois Beyers
@Blusoup
Mar 22 2018 14:33
it is for exact cases
i'm just trying to see if I can combine them better.
alpox
@alpox
Mar 22 2018 14:36
@Blusoup .replace(/[\s)']/g, "").replace(",", ";")
Francois Beyers
@Blusoup
Mar 22 2018 14:37
thank you @alpox 🙏🏼
CamperBot
@camperbot
Mar 22 2018 14:37
blusoup sends brownie points to @alpox :sparkles: :thumbsup: :sparkles:
:star2: 1590 | @alpox |http://www.freecodecamp.org/alpox
Zunaid Aslam
@ZunaidAslam
Mar 22 2018 14:56

Is there any developer who knows react native?
I have a couple of question to ask him/her.
I am learning front-end development. Still at basic stage. I can make decent website, weather app, wikipedia viewer. Small projects.
I wish to make apps using JS. I figured out I need to know React Native for that.

I know it's relative but how difficult is the learning curve from this stage.
How long it may take for someone from this stage?

I would really appreciate the feedback because I am working full-time in another job
So I will organize my time in a suitable way if I can get an estimate.
Thanks!

Jason Luboff
@JLuboff
Mar 22 2018 15:15
Good morning everyone who tagged me above :P
Markus Kiili
@Masd925
Mar 22 2018 15:53
@JLuboff Morning.
Jason Luboff
@JLuboff
Mar 22 2018 15:54
Whats good?
Markus Kiili
@Masd925
Mar 22 2018 15:56
@JLuboff I have two weeks of vacation before the new job starts. Very nice.
How about you?
Jason Luboff
@JLuboff
Mar 22 2018 15:57
That does sound very nice. Just at work... watching the rain..
We have a flash flood warning for the next few hours.. haven't seen anything close to that yet though
Markus Kiili
@Masd925
Mar 22 2018 16:02
@JLuboff I have to decide this week if I will apply for the national training ring of freedivers. If I do, I might have a chance to qualify into World championships that are held in June.
Jason Luboff
@JLuboff
Mar 22 2018 16:02
You should do it!
Markus Kiili
@Masd925
Mar 22 2018 16:03
@JLuboff Yeah. Being 45, I might regret if I don't.
Jason Luboff
@JLuboff
Mar 22 2018 16:08
Soon you'll be too old
Nate Mallison
@NJM8
Mar 22 2018 16:14
anyone here good with regex?
I want a regex to match underscore, spaces, and capital letters
driving me nuts
I hate regex
Jason Luboff
@JLuboff
Mar 22 2018 16:15
I'm terribly mediocre at it
Razvan Jackson
@RazvanJackson
Mar 22 2018 16:15
@NJM8 here
Jason Luboff
@JLuboff
Mar 22 2018 16:15
oh.. that shouldn't be too difficult
Razvan Jackson
@RazvanJackson
Mar 22 2018 16:15
[_\sA-Z]
try this
Nate Mallison
@NJM8
Mar 22 2018 16:16
I need positive look ahead on the caps to keep them, I tried /\s(?=[A-Z])/
but it isn't working correctly
Jason Luboff
@JLuboff
Mar 22 2018 16:16
What do you mean by positive look ahead?
Nate Mallison
@NJM8
Mar 22 2018 16:17
(?=)
Jason Luboff
@JLuboff
Mar 22 2018 16:18
Oh hm. Can you give more context of what you're trying to do/what you're working with
Ken Haduch
@khaduch
Mar 22 2018 16:19
@NJM8 - could you give more context?
Nate Mallison
@NJM8
Mar 22 2018 16:19
so 'HiHiHi'.split([A-Z]); will return ['iii']
Jason Luboff
@JLuboff
Mar 22 2018 16:19
@khaduch Morning Ken
Ken Haduch
@khaduch
Mar 22 2018 16:19
@JLuboff - good morning! (Well just after noon for me... :) )
Nate Mallison
@NJM8
Mar 22 2018 16:20
'HiHiHi'.split((?=[A-Z])); will return ['Hi', 'Hi', 'Hi' ]
Jason Luboff
@JLuboff
Mar 22 2018 16:20
@NJM8 Missing the / in your regex (unless this is just a quick example)
@khaduch Ah, yes, good afternoon!
Nate Mallison
@NJM8
Mar 22 2018 16:21
look ahead... looks ahead to the next element and flags a match just before the element, instead of on the element, so if you are splitting a string on the caps but want to keep them you can
I'm doing the spinal tap challenge
function spinalCase(str) {
  str = str.replace('_', '-');
  const seperator = new RegExp(/(?=[A-Z])/);
  return str.split(seperator).map(word => word.toLowerCase()).join('-').replace(' ', '');
}
should take a string and replace all spaces and underscores with dashes, make all lowercase, and add a dash in front of caps
this solution will probably pass but I can't get the regex to grab dashes, spaces, and just before caps
so I'm doing it separately, but ugly
Ken Haduch
@khaduch
Mar 22 2018 16:29
@NJM8 - my replacement was done in steps, too - for that part I did this: newstr = newstr.replace( /([A-Z])/g, ' $1' ).toLowerCase();
I first replaced any dashes or underscores with spaces. Then I did that line above, then I replaced any spaces \\s+ with a single -... I'm not familiar with the regexp lookahead - I should go and learn that one! I just massaged the string to fit a regexp construct that I was familiar with...
Nate Mallison
@NJM8
Mar 22 2018 16:40
well i got the whitespace into it. still can't get the underscore into it. regex makes no sense. I add the underscore using the same pattern as the space and it breaks the whole thing
Jason Luboff
@JLuboff
Mar 22 2018 16:42
You may need to use the OR | operator
Nate Mallison
@NJM8
Mar 22 2018 16:43
yeahhhhhh
Thanks @JLuboff and @khaduch
CamperBot
@camperbot
Mar 22 2018 16:43
njm8 sends brownie points to @jluboff and @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 2380 | @jluboff |http://www.freecodecamp.org/jluboff
:star2: 3811 | @khaduch |http://www.freecodecamp.org/khaduch
Nate Mallison
@NJM8
Mar 22 2018 16:43
got it in one line. :smile:
Jason Luboff
@JLuboff
Mar 22 2018 16:43
Nice job
Nate Mallison
@NJM8
Mar 22 2018 16:44
oops, fails test
Ken Haduch
@khaduch
Mar 22 2018 16:57
@NJM8 - I got it to one line... kind of a nasty one line, but it works... haven't tried it against the automatic test suite but I put all of the cases in and it looks like it works?
Mackenzie Miller
@mackmmiller
Mar 22 2018 17:05
I'm looping through some data and writing it to the DOM, but trying to put it into divs by year. I'm trying to check if the year's node exist,
if (document.querySelector(`.${year}`) != null) { container.innerHTML += `<div class='${year}'>${year}</div>` },
but my querySelector keeps erring and crashing the browser. Any ideas?
Ken Haduch
@khaduch
Mar 22 2018 17:06
@mackmmiller - I would not try to query the DOM for that. I would keep some JS variable or object to track the year and the DIV that contained the data for that year. Too much DOM querying is bad medicine...
Mackenzie Miller
@mackmmiller
Mar 22 2018 17:08
Does comparing this year to the next year and handling it appropriately sound like a better solution?
Ken Haduch
@khaduch
Mar 22 2018 17:12
@mackmmiller - I'm not exactly sure what your objective is or what the data is that you're working with, but I would keep as much of this in JS as possible. My rough idea: If you have one top-level <div> for each year, I would have an object, the keys would be years, the data would be the reference to the div (either a javascript or jQuery reference). If the year did not exist in the object, make a new div and store it there, Then if you get a new piece of data, check if you already have a div for that year, and if you do, append the data into that div, if not, create the new div and store the info in the object... If you have code that you would want to share, maybe a codepen pen or something? It would be possible to comment further.
Onome Sotu
@onomesotu
Mar 22 2018 17:57
does a nested function that has a return statement end the program?
for example:
function sym(args) {
  var argument = Array.from(arguments);

  var lengthOfArgs = argument.length - 1,
      currentArg = 1,
      symDiff = [];

  function checker(array1, array2){
    for(var i = 0; i < array1.length; ++i){
      if(array2.indexOf(array1[i]) == -1){
        symDiff.push(array1[i]);
      }
    }
  }

  function findSymDiff(arr1, arr2){//function that checks for the symmetric differences
    checker(arr1, arr2);
    checker(arr2, arr1);
   //return symDiff; this return doesn't work. 
  }

  findSymDiff(argument[0], argument[1]);
  return symDiff; //this returns something
}

sym([3, 3, 3, 2, 5], [2, 1, 5, 7]);
I want the inner function findSymDiff to return the array simDiff but for some reason, it doesn't
i thought a function can be returned anywhere in it's scope
Gulsvi
@gulsvi
Mar 22 2018 18:02
@onomesotu No, it does not end the program. The return bubbles up to the parent function.
Markus Kiili
@Masd925
Mar 22 2018 18:03
@onomesotu In other words, the function call to the inner function evaluates to the return value of the inner function.
Onome Sotu
@onomesotu
Mar 22 2018 18:04
wow! and i thought i knew how to program :(
Gulsvi
@gulsvi
Mar 22 2018 18:05
function sayHello() {

  function sayHelloInSpanish() {
    return "Hola!";
  }

  sayHelloInSpanish();
}

sayHello() // undefined
but you can return sayHelloInSpanish() to get that value
Onome Sotu
@onomesotu
Mar 22 2018 18:07
@gulsvi you are very correct, thank you, so in my case, I have to return findSymDiff(argument[0], argument[1]);
CamperBot
@camperbot
Mar 22 2018 18:07
onomesotu sends brownie points to @gulsvi :sparkles: :thumbsup: :sparkles:
:star2: 2681 | @gulsvi |http://www.freecodecamp.org/gulsvi
Onome Sotu
@onomesotu
Mar 22 2018 18:07
@Masd925 thanks..
CamperBot
@camperbot
Mar 22 2018 18:07
onomesotu sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 4738 | @masd925 |http://www.freecodecamp.org/masd925
Onome Sotu
@onomesotu
Mar 22 2018 18:08
When you think you're flying, humble is always just around the corner
What gave me sleepless night yesterday ..was simply a return statement smh
Onome Sotu
@onomesotu
Mar 22 2018 18:17
Another question, is there a way to find the symmetric difference of an array so that array elements are not duplicated?
for example: var a = [3, 3, 3, 2, 5], b = [2, 1, 5, 7]; the symmetric difference should be [3,1,7]
but since i used indexOf to check the differences, it repeats [3,3,3,1,7]
I can write a code to take out the multiple occurrences, I want to know if there are better methods to solve the symmetry instead of adding more code to remove multiple occuring numbers
Markus Kiili
@Masd925
Mar 22 2018 18:23
@onomesotu One way is to write a function that remove elements of one array from another array. Then solve it by using the function twice.
Another way is to first remove repeating elements from original arrays (leaving one of each number). Then concat the arrays and filter out those numbers that are not single.
Onome Sotu
@onomesotu
Mar 22 2018 18:26
@Masd925 but i will still have recurring elements that are in only one array.. if i understand you correctly
@Masd925 OK, I wil try the second one
@Masd925 thanks
CamperBot
@camperbot
Mar 22 2018 18:29
onomesotu sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
api offline
Nate Mallison
@NJM8
Mar 22 2018 18:43
@khaduch finally got it, ugly but it works. too bad regex is gibberish or it would be easy
Jason Luboff
@JLuboff
Mar 22 2018 18:47
Regex is very powerful if you understand it
Ken Haduch
@khaduch
Mar 22 2018 18:56
@NJM8 - this str.replace( /(\-|_|(([^\s])(?=[A-Z])))/g, '$3-') did most of the cases... :)
Nate Mallison
@NJM8
Mar 22 2018 18:57
captain clever on the forums just replaced all non letters with a dash then split it by the uppercase letters and joined again
found someone did the one line with regex too, nice solution
Ken Haduch
@khaduch
Mar 22 2018 18:59
@NJM8 - that's kind of what I did in my first solution - well, a similar idea. And the one I just posted above didn't really solve "most" of the tests... I might have been confused as I iterated toward my solution... Maybe my expression could be simpler?
maybe back to regexr.com... :)
Nate Mallison
@NJM8
Mar 22 2018 19:13
I have found all those regex sites to be a pain and more confusing then they are worth
Muhammad Hasham
@MohammadHasham
Mar 22 2018 19:13
how can i convert this array arr=[1,2,3] to 1,2,3
Nate Mallison
@NJM8
Mar 22 2018 19:13
I tried looking for a regex builder but couldn't find a good one
Muhammad Hasham
@MohammadHasham
Mar 22 2018 19:13
should i use spread operator
Nate Mallison
@NJM8
Mar 22 2018 19:14
you mean individual number?
Markus Kiili
@Masd925
Mar 22 2018 19:14
@MohammadHasham What you mean by 1,2,3 ?
Muhammad Hasham
@MohammadHasham
Mar 22 2018 19:14
like i want to remove the array brackets
Markus Kiili
@Masd925
Mar 22 2018 19:15
@MohammadHasham A string "1,2,3" ?
Muhammad Hasham
@MohammadHasham
Mar 22 2018 19:15
like this "1","2","3"
i am doing like this
let arr = result.data.map(item => (...item));
Markus Kiili
@Masd925
Mar 22 2018 19:16
@MohammadHasham ["1","2","3"] ?
Muhammad Hasham
@MohammadHasham
Mar 22 2018 19:16
actually this is the data
[
    {
        "courses": [
            "Computer Graphics",
            "Calculus"
        ],
        "_id": "5ab3eae2f36d282750925d34"
    },
    {
        "courses": [
            "Digital Image Processing",
            "Web Programming"
        ],
        "_id": "5ab3ee9af36d282750925eed"
    }
]
i want to concat the courses into one single array
@Masd925
i want to concat the courses
image.png
Markus Kiili
@Masd925
Mar 22 2018 19:19
@MohammadHasham
arr.reduce(function(acc,curr){
  return acc.concat(curr.courses);
},[]);
Muhammad Hasham
@MohammadHasham
Mar 22 2018 19:19
can i do it with map
@Masd925 thanks
CamperBot
@camperbot
Mar 22 2018 19:19
mohammadhasham sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 4739 | @masd925 |http://www.freecodecamp.org/masd925
Muhammad Hasham
@MohammadHasham
Mar 22 2018 19:20
@Masd925 thanks once again! i should have looked into reduce thanks
CamperBot
@camperbot
Mar 22 2018 19:20
mohammadhasham sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
api offline