These are chat archives for FreeCodeCamp/Help

4th
Mar 2018
Fabien SHAN
@X140hu4
Mar 04 2018 02:46
@ezioda004 Oh are you doing CS50? :)
Ginger
@GingerChew
Mar 04 2018 03:09
hey guys, I'm running into an issue, I'm trying to recreate this dribbble 'https://dribbble.com/shots/4295147-Stepper' but I'm forgetting how to add classes based on the value of an innerhtml, here's my codepen 'https://codepen.io/WatchConnorCode/pen/MQRLYo?editors=1010'
Fabien SHAN
@X140hu4
Mar 04 2018 03:22
@GingerChew First your count const has an innerHTML at the end.
Second, are you using jQuery or pure JS?
@GingerChew with pure JS you need to use the addEventListener method
const count = document.querySelector('.count')
[...]
minus.addEventListener('click', function() {
  count.innerHTML = (count.innerHTML - 1);
});
Ginger
@GingerChew
Mar 04 2018 03:25
okay, I knew I was missing something, if I added jquery( which I'm in no way against) how does it change?
Fabien SHAN
@X140hu4
Mar 04 2018 03:49
@GingerChew I am not sure, I haven't used jQuery much
Ginger
@GingerChew
Mar 04 2018 03:49
okay, I'll mess with it a little
thanks @X140hu4 for the help!
CamperBot
@camperbot
Mar 04 2018 03:50
gingerchew sends brownie points to @x140hu4 :sparkles: :thumbsup: :sparkles:
:cookie: 342 | @x140hu4 |http://www.freecodecamp.org/x140hu4
Fabien SHAN
@X140hu4
Mar 04 2018 03:50
Maybe something like this:
$(".minus").on("click", function(){});
Darren
@DarrenfJ
Mar 04 2018 07:31
@lydatech cool, thanks for the info
CamperBot
@camperbot
Mar 04 2018 07:31
darrenfj sends brownie points to @lydatech :sparkles: :thumbsup: :sparkles:
:star2: 2677 | @lydatech |http://www.freecodecamp.org/lydatech
Aditya
@ezioda004
Mar 04 2018 09:10
@X140hu4 Yeah, finally, been procrastinating for a long time :D
Kelechi Chinaka
@ke1echi
Mar 04 2018 09:14
latest on your mario? @ezioda004
Aditya
@ezioda004
Mar 04 2018 09:15
@kelechy Havent worked on it after yesterday, plan to complete pset1 today
Kelechi Chinaka
@ke1echi
Mar 04 2018 09:17
ok... just take a step at a time @ezioda004
Aditya
@ezioda004
Mar 04 2018 09:19
That's the plan
Ishtmeet Singh
@Ishtmeet-Singh
Mar 04 2018 14:08
@DarrenfJ Yo dude!
Adegoke Fred
@Frediflexta
Mar 04 2018 20:19
Hi guys, i need some help, i am writing a function, and i want it to return an object
like so .. {uniques: 'aehlstx', duplicates: 2}
where 'aehlstx' and 2 are dynamically gotten
so my question is can i declare a placeholder like this?
const removeDuplicates = word => {
  // declare unique string and no of duplicates
  let unique = '';
  let noDupl = 0;

  // create an object literal
  const removeDuplicate = {
    uniques : ${unique},
    duplicates : ${noDupl}
  };
gishy1
@gishy1
Mar 04 2018 20:24
@sjames1958gm belated thanks city for the league confirmed
CamperBot
@camperbot
Mar 04 2018 20:24
gishy1 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 9057 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
gishy1
@gishy1
Mar 04 2018 20:24
hello World!!
starting the random qoute project
how can i intergrate the js code into the qoute machine ?
Aditya
@ezioda004
Mar 04 2018 20:27
@Frediflexta Yeah but you gotta put `${unique}` b/w backticks
Adegoke Fred
@Frediflexta
Mar 04 2018 20:28
oh... so just backticks?
Aditya
@ezioda004
Mar 04 2018 20:29
const removeDuplicates = word => {
  // declare unique string and no of duplicates
  let unique = '';
  let noDupl = 0;

  // create an object literal
  const removeDuplicate = {
    uniques : `${unique}`,
    duplicates : `${noDupl}`
  };
console.log(removeDuplicate);
}
removeDuplicates(); // {uniques: "", duplicates: "0"}
Adegoke Fred
@Frediflexta
Mar 04 2018 20:29
right! thank you! @ezioda004
CamperBot
@camperbot
Mar 04 2018 20:29
:cookie: 494 | @ezioda004 |http://www.freecodecamp.org/ezioda004
frediflexta sends brownie points to @ezioda004 :sparkles: :thumbsup: :sparkles:
Stephen James
@sjames1958gm
Mar 04 2018 20:29
@gishy1 You can run js at startup (code pen does that for you). Or you can attach js to controls like buttons and such
Ken Haduch
@khaduch
Mar 04 2018 20:38
@gishy1 - you would have a click handler for a button that would be activated by a click (when properly implemented) that would fetch a new quote. You can also invoke that same function when the page is loaded to get and update the quote for the first time. But as @sjames1958gm points out, on codepen, the JavaScript code is run at startup, you implement the actions that you want.
Adegoke Fred
@Frediflexta
Mar 04 2018 20:41
@ezioda004 please, how do i turn ${noDupl} to an int
Adel
@AdelMahjoub
Mar 04 2018 20:44
@Frediflexta why returning noDupl as a string if it is already a number and you want a number
Aditya
@ezioda004
Mar 04 2018 20:44
Number(`${noDupl}`)
Adegoke Fred
@Frediflexta
Mar 04 2018 20:47
@ezioda004 Yea, Thanks, that worked, but now i got a new bug
CamperBot
@camperbot
Mar 04 2018 20:47
frediflexta sends brownie points to @ezioda004 :sparkles: :thumbsup: :sparkles:
api offline
Adegoke Fred
@Frediflexta
Mar 04 2018 20:47
the values i get don't get plugged in
Adel
@AdelMahjoub
Mar 04 2018 20:47
if you are sure it is the string is numeric , +string return a number
you can turn any number to string number.toString()
Adegoke Fred
@Frediflexta
Mar 04 2018 20:48
Alright @AdelMahjoub, but Number(`${noDupl}`) worked fine, thank you
CamperBot
@camperbot
Mar 04 2018 20:48
frediflexta sends brownie points to @adelmahjoub :sparkles: :thumbsup: :sparkles:
:cookie: 664 | @adelmahjoub |http://www.freecodecamp.org/adelmahjoub
Adel
@AdelMahjoub
Mar 04 2018 20:49
despite that noDupl is initially a number, you are returning it as a string, to convert it back to a number. I don't see why
Aditya
@ezioda004
Mar 04 2018 20:50
@Frediflexta The values dont get plugged in because template literal is used when you run the function
Adegoke Fred
@Frediflexta
Mar 04 2018 20:51
So, how do i sort that out?
Aditya
@ezioda004
Mar 04 2018 20:51
What exactly are you trying to do?
Adegoke Fred
@Frediflexta
Mar 04 2018 20:51
cause that's the whole idea of using placeholders there.. i need to get the values dynamically
Aditya
@ezioda004
Mar 04 2018 20:52
Then dont use let unique = ''; let noDupl = 0;
Adegoke Fred
@Frediflexta
Mar 04 2018 20:52
Okay, then what?
Aditya
@ezioda004
Mar 04 2018 20:55
const removeDuplicates = (word, unique, noDupl)=> {
  // declare unique string and no of duplicates

  // create an object literal
  const removeDuplicate = {
    uniques : `${unique}`,
    duplicates : Number(`${noDupl}`)
  };
console.log(removeDuplicate);
}
removeDuplicates("Word", "somethingUnique", 2);
Example you can push it in via function dynamically
Adel
@AdelMahjoub
Mar 04 2018 21:00
convert it to JSON object and parse it again, meanwhile, because why not, lets convert everything and convert it back
Adegoke Fred
@Frediflexta
Mar 04 2018 21:00
ummm, so the specification is, the function takes just one parameter (word)
Aditya
@ezioda004
Mar 04 2018 21:02
@Frediflexta So once you use the template literal, you cant use it again. I guess you can just update the properties and values?
Adegoke Fred
@Frediflexta
Mar 04 2018 21:05
@AdelMahjoub what exactly am i converting to a JSON object
Adegoke Fred
@Frediflexta
Mar 04 2018 21:19
@AdelMahjoub so how excatly do i go about this please?
What exactly am i converting?
Manish Giri
@Manish-Giri
Mar 04 2018 22:22
@Frediflexta what is your function supposed to do
Darren
@DarrenfJ
Mar 04 2018 22:31
@Manish-Giri manish! :wave:
miss yah buddy :D
@Ishtmeet-Singh brutha!
:D
Manish Giri
@Manish-Giri
Mar 04 2018 22:32
@DarrenfJ :wave: same here same here :smile:
I think Ishtmeet got banned again today, lol
poor guy just can't catch a break
Darren
@DarrenfJ
Mar 04 2018 22:32
life has a way of getting in the middle of everything :/
Manish Giri
@Manish-Giri
Mar 04 2018 22:33
true
Adegoke Fred
@Frediflexta
Mar 04 2018 22:34
@Manish-Giri The function should return an object literal containing a 'uniques' property, which should be the sorted input string but without any duplicates or special characters.
The returned object should also have a 'duplicates' propoerty which should represent the total number of duplicate characters dropped
Manish Giri
@Manish-Giri
Mar 04 2018 22:36
okay
what's your code so far?
Adegoke Fred
@Frediflexta
Mar 04 2018 22:37
const removeDuplicates = word => {
  // declare unique string and no of duplicates
  let unique = '';
  let noDupl = 0;

  // create an object literal
  let removeDuplicate = {
    uniques : `${unique}`,
    duplicates : Number(`${noDupl}`)
  };

  // split and sort the word
  word.split('').sort().join('').replace(/[^A-Za-z0-9]/g, '');

  // loop through the word, deleting duplicates
  for (let i = 0; i < word.length; i++)
  {
    // note the 1st length of the word
    let startlen = word.length;

    if (word.lastIndexOf(word[i]) == word.indexOf(word[i]))
    {
        unique += word[i];
    }
    // note the new length of the word
    let currentlen = unique.length;
    noDupl += (startlen) - (currentlen);
  }
  console.log(removeDuplicate);
  return removeDuplicate;
}
Though i know it will still be buggy, just want to fix this 1st
Manish Giri
@Manish-Giri
Mar 04 2018 22:39
okay, which part isn't working?
Adegoke Fred
@Frediflexta
Mar 04 2018 22:41
when i call the function, it prints this { uniques: '', duplicates: 0 } which means the my values are not updated
Manish Giri
@Manish-Giri
Mar 04 2018 22:41
do you have an example test case?
Adegoke Fred
@Frediflexta
Mar 04 2018 22:43
Yea, if i call removeDuplicates('th#elex_ash?') its suppose to return {uniques: 'aehlstx', duplicates: 2}
Manish Giri
@Manish-Giri
Mar 04 2018 22:45
first thing you should note is that your .replace() isn't working

I put in a console.log after

word.split('').sort().join('').replace(/[^A-Za-z0-9]/g, '');

and I still get th#elex_ash?

Adegoke Fred
@Frediflexta
Mar 04 2018 22:48
okay.. i'll fix that
i think i'll use it when i am returning the object
Manish Giri
@Manish-Giri
Mar 04 2018 22:53

@Frediflexta
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement

You need to save the return value of the replacement -
word.split('').sort().join('').replace(/[^A-Za-z0-9]/g, '');
Adegoke Fred
@Frediflexta
Mar 04 2018 22:55
something like newword = word.split('').sort().join('').replace(/[^A-Za-z0-9]/g, ''); ?
Manish Giri
@Manish-Giri
Mar 04 2018 22:55
yes
also, you can use the shortcut -
let replaced = word.replace(/[\W_]/g, '');
[\W_] is equivalent to your current regex
Adegoke Fred
@Frediflexta
Mar 04 2018 22:56
Okay, noted!
i'm not sure it will account for _ in a string
Manish Giri
@Manish-Giri
Mar 04 2018 22:58
_ is removed, I just ran my code against your example
Screenshot 2018-03-04 17.58.53.png
Adegoke Fred
@Frediflexta
Mar 04 2018 22:59
yea.. i just did too, but its not sorted in alphabetical order
Manish Giri
@Manish-Giri
Mar 04 2018 22:59
just add a .sort()
Adegoke Fred
@Frediflexta
Mar 04 2018 23:00
okay...
your output seems right
what was i missing apart from replace()
Manish Giri
@Manish-Giri
Mar 04 2018 23:02
I didn't run your for loop, there's another way to remove duplicates that I used
Adegoke Fred
@Frediflexta
Mar 04 2018 23:03
Okay
Manish Giri
@Manish-Giri
Mar 04 2018 23:04

This is the sorting and replacement -

let replaced = [...word.replace(/[\W_]/g, '')].sort().join('');

and this is the removing duplicates -

let uniques = new Set(replaced);
Adegoke Fred
@Frediflexta
Mar 04 2018 23:05
Okay... Wow
Manish Giri
@Manish-Giri
Mar 04 2018 23:05
once you have these 2, you can just subtract the length of the 2nd from the 1st, that will give you the value for the duplicates property
Adegoke Fred
@Frediflexta
Mar 04 2018 23:05
yes.. like i did right?
Manish Giri
@Manish-Giri
Mar 04 2018 23:05
 return {
    uniques: [...uniques].join(''),
    duplicates: replaced.length - uniques.size
  }
yes
Adegoke Fred
@Frediflexta
Mar 04 2018 23:06
So this is how i return it?
Manish Giri
@Manish-Giri
Mar 04 2018 23:06
yes, this returns an object, like your question asks
Adegoke Fred
@Frediflexta
Mar 04 2018 23:07
How about let unique = ''; let noDupl = 0; do i undeclare it? or is it good?
Manish Giri
@Manish-Giri
Mar 04 2018 23:07
you can use it if you plan to run your original code to see if it works after fixing the .replace (which you should)
but you won't need those in my version
Adegoke Fred
@Frediflexta
Mar 04 2018 23:08
I see
Okay, let me put all these together here, would get back to you on how it went
Manish Giri
@Manish-Giri
Mar 04 2018 23:09
cool
:+1:
Adegoke Fred
@Frediflexta
Mar 04 2018 23:24
@Manish-Giri 1st of Thanks alot for your input, really appreciated
CamperBot
@camperbot
Mar 04 2018 23:24
frediflexta sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6813 | @manish-giri |http://www.freecodecamp.org/manish-giri
Adegoke Fred
@Frediflexta
Mar 04 2018 23:24
So, i am only getting one error now
My duplicate isn't parsing
  // split and sort the word
  let replacedword = [...word.replace(/[\W_]/g,'')].sort().join('');

  // note the initial length of the word
  let startlen = replacedword.length;

  // iterate and remove duplicates
  unique = new Set(replacedword);

  // note the new length of the word
  let currentlen = unique.length;

  return {
    uniques: [...unique].join(''),
    duplicates: startlen - currentlen
  };
}
Alright, so i figured what i was doing wrong
All good now
Manish Giri
@Manish-Giri
Mar 04 2018 23:28
@Frediflexta glad you got it working, good job!
Adegoke Fred
@Frediflexta
Mar 04 2018 23:28
Thank you...
Manish Giri
@Manish-Giri
Mar 04 2018 23:30
:+1: