These are chat archives for FreeCodeCamp/Help

26th
Feb 2018
Darren
@DarrenfJ
Feb 26 2018 05:07
evening fCC
Claudio Restifo
@Marmiz
Feb 26 2018 05:30
:wave: @DarrenfJ
harishcalvin
@harishcalvin
Feb 26 2018 08:03
hey i need one help
Claudio Restifo
@Marmiz
Feb 26 2018 08:11
@ericmiller777 what?
@harishcalvin just post your question, someone will try to help for sure :)
Eric Miller
@ericmiller777
Feb 26 2018 08:14
@Marmiz figured it out
@Marmiz was 2 walls of code there a sec ago :P anyway thanks
CamperBot
@camperbot
Feb 26 2018 08:14
ericmiller777 sends brownie points to @marmiz :sparkles: :thumbsup: :sparkles:
:star2: 1174 | @marmiz |http://www.freecodecamp.org/marmiz
Claudio Restifo
@Marmiz
Feb 26 2018 08:15
that's great :)
Nazar
@IsaakNazar
Feb 26 2018 08:29
anyone help with regExp! if there's 2 spaces, how to delete only single space
Claudio Restifo
@Marmiz
Feb 26 2018 08:31
@IsaakNazar have you looked into replace method?
Johnny
@JohnnyBizzel
Feb 26 2018 10:36
@IsaakNazar
var string2spaces = "A  string  with  big  spaces.";
var spacesCropped = string2spaces.replace(/  /gi,' ');
console.log(spacesCropped);
Nazar
@IsaakNazar
Feb 26 2018 10:50
@JohnnyBizzel thank you :smile:
CamperBot
@camperbot
Feb 26 2018 10:50
isaaknazar sends brownie points to @johnnybizzel :sparkles: :thumbsup: :sparkles:
:star2: 1801 | @johnnybizzel |http://www.freecodecamp.org/johnnybizzel
Johnny
@JohnnyBizzel
Feb 26 2018 10:50
@IsaakNazar :+1:
Nazar
@IsaakNazar
Feb 26 2018 11:12
this function should return on format like commas between them, and and before the last one, and space after commas, like example below.
years, days, hours, minutes and seconds
days, minutes and seconds
years and seconds
my function doesnt work properly, maybe should I fix it with regex, but its huge work
function format(sec){
  var m = 60;
  var h = 3600;
  var d = 86400;
  var y = 31536000;
  var year = Math.floor(sec / y);
  var day = Math.floor((sec % y) / d);
  var hour = Math.floor((sec % d) / h);
  var min = Math.floor((sec % d % h) / m);
  var secs = Math.floor(sec % d % h % m);
  var str = '';

  const pluralize = (st, amount) => (amount === 1) ? (amount + " " + st) : (amount !== 0) ? (amount + ' ' + st + 's') : "";
  var arr = [];


  if (sec >= y) {
    arr.push(`${pluralize("year", year)}, ${pluralize("day", day)}, ${pluralize("hour", hour)}, ${pluralize("minute", min)}, ${pluralize("second", secs)}`);
  }
  if (sec >= d && sec < y) {
    arr.push(`${pluralize("day", day)}, ${pluralize("hour", hour)}, ${pluralize("minute", min)}, ${pluralize("second", secs)}`);
  }
  if (sec >= h && sec < d) {
    arr.push(`${pluralize("hour", hour)}, ${pluralize("minute", min)}, ${pluralize("second", secs)}`);
  }
  if (sec >= m && sec < h) {
    arr.push(`${pluralize("minute", min)}, ${pluralize("second", secs)}`);
  }
  if (sec < m) {
    arr.push(`${pluralize("second", secs)}`);
  }

  var result = arr.join("").split(", ");

  function convert(ar){

    if (ar.length >= 2) {
      ar[ar.length-1] = 'and ' + ar[ar.length-1];
    }else {
      return ar.join();
    }
    ar = ar.join(', ');
    ar = ar.slice(0, ar.lastIndexOf(',')) + ar.slice(ar.lastIndexOf(',')+1, ar.length);
    return ar;
  }

  return convert(result);

}
console.log(format(31536000+3600));
console.log(format(86403));
console.log(format(869300));
console.log(format(1186900));
console.log(format(869000));
console.log(format(31869000));
console.log(format(31536002));
Stephen James
@sjames1958gm
Feb 26 2018 13:01
@IsaakNazar "test0, test1, test2".replace(/, (\S*)$/, ", and $1")
You can use regex to match the last ", "
Nazar
@IsaakNazar
Feb 26 2018 14:28
@sjames1958gm i have one more question
Stephen James
@sjames1958gm
Feb 26 2018 14:28
@IsaakNazar Only one more :)
@IsaakNazar ask away
Nazar
@IsaakNazar
Feb 26 2018 14:28
:smile:
when i logging theres 9+9undefined, but I want only 9+9, can you give me a hint
function nine(op) {
  return ('9' + op) || '9';
}

function plus(num) {
  return '+' + num;
}
console.log( nine(plus(nine())));
Stephen James
@sjames1958gm
Feb 26 2018 14:31
@IsaakNazar ('9' + op) || '9'; this will always be the first choice even when op is undefined because '9' + undefined is still truthy
@IsaakNazar return op ? ('9' + op) : '9'; this would be how I would code that
Nazar
@IsaakNazar
Feb 26 2018 14:34
@sjames1958gm is it possible to find a solution with || ?
@sjames1958gm but yeah, your is the perfect solution , thx :+1:
CamperBot
@camperbot
Feb 26 2018 14:35
isaaknazar sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 9031 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Stephen James
@sjames1958gm
Feb 26 2018 14:36
@IsaakNazar a solution with || doesn't seem straight forward
Nazar
@IsaakNazar
Feb 26 2018 14:37
@sjames1958gm the smartest person in gitter IMHO :+1:
Stephen James
@sjames1958gm
Feb 26 2018 14:39
@IsaakNazar thx
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 19:29
Hey guys, I need some help with my Recipe Box. In the RecipeIngredients Component I modified the code such that if I click on the pencil icon, all other icon change form and functionality. But it doesn't work. Is my logic wrong here?
Kaz Baig
@kbaig
Feb 26 2018 20:40
@MukeshAngrish unless I'm misunderstanding what you're trying to implement, it seems to work fine for me https://i.gyazo.com/0f551c451838d3d0c84e3064cc8aec4a.png
I clicked on the pencil icon and the edit icons appeared
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 20:45
@kbaig Yeah it's working fine now. I got some help from HelpFrontEnd.
Kaz Baig
@kbaig
Feb 26 2018 20:46
@MukeshAngrish cool :)
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 21:06
@kbaig Though there is one thing I need help with. Those button are not working. Can you please check it.
Kaz Baig
@kbaig
Feb 26 2018 21:07
@MukeshAngrish you mean the x and trash icon buttons?
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 21:17
@kbaig Yeah those are the ones not working.
Christopher McCormack
@cmccormack
Feb 26 2018 21:52
@MukeshAngrish Your case statement isn't correct for deleteRecipe - this.setState({ myRecipeBox }); should be this.setState({ myRecipeBox: newRecipeBox });
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 21:56
@cmccormack thanks! I missed that typo
CamperBot
@camperbot
Feb 26 2018 21:56
mukeshangrish sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
:star2: 1340 | @cmccormack |http://www.freecodecamp.org/cmccormack
Christopher McCormack
@cmccormack
Feb 26 2018 21:57
@MukeshAngrish np
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 22:00
@cmccormack strangely though, I'm getting this.props.recipe.ingredients.map() is not a function after correcting that.
Christopher McCormack
@cmccormack
Feb 26 2018 22:01
@MukeshAngrish worked for me
although I was messing with a lot of stuff
@MukeshAngrish Just tested your pen and it works
for Edit > delete recipe
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 22:05
Screen Shot 2018-02-27 at 3.33.37 AM.png
Christopher McCormack
@cmccormack
Feb 26 2018 22:06
Did you save? I'm not getting any errors at all. Also what actions were you taking that surfaced that error?
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 22:17
Yeah I did save. Don't know why that error came(as you can see in the screenshot). Certainly something went wrong in the browser. It works fine on other browser. But the add ingredient button is not working
Christopher McCormack
@cmccormack
Feb 26 2018 22:20
@MukeshAngrish your addIngredient function doesn't actually do anything
you setState your recipe box but the state isn't changed
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 22:22
@cmccormack is there another way to push '' into an array inside an object inside another array?
Christopher McCormack
@cmccormack
Feb 26 2018 22:24
@MukeshAngrish let me restate - your function is doing something but not provoking a state update.
Christopher McCormack
@cmccormack
Feb 26 2018 22:30
@MukeshAngrish also understand if you were to use setState and your assigning an object to a variable, if that object had already been assigned to that variable, React will NOT update. The object is a reference, so even if the object changed, React still sees it as the same reference
Mukesh Kumar Angrish
@MukeshAngrish
Feb 26 2018 22:35
@cmccormack oh ... okay. Then how do I change state in such a case(or without assigning object to the variable)?
Kyle Holm
@thekholm80
Feb 26 2018 22:36
shouldComponentUpdate(nextProps, nextState)
it's brilliant
Christopher McCormack
@cmccormack
Feb 26 2018 22:43
@thekholm80 I'm not certain that would solve that problem - do you have some examples?
Kyle Holm
@thekholm80
Feb 26 2018 22:45
i've had trouble with changing complex objects not triggering updates, so doing something like
shouldComponentUpdate(nextProps, nextState) {
  if (this.state.myComplexObject.has.way.tooMany.levels !== nextState.myComplexObject.has.way.tooMany.levels) {
    return true; // update the DOM
  }
}
Christopher McCormack
@cmccormack
Feb 26 2018 22:47

Ah ok this is the part I missed:

shouldComponentUpdate() is invoked before rendering when new props or state are being received

I assumed an actual state change would have to occur but this is checked prior, very cool

@thekholm80 thanks
CamperBot
@camperbot
Feb 26 2018 22:47
cmccormack sends brownie points to @thekholm80 :sparkles: :thumbsup: :sparkles:
:star2: 1711 | @thekholm80 |http://www.freecodecamp.org/thekholm80
Kyle Holm
@thekholm80
Feb 26 2018 22:50
i know it's not good to micromanage the updates, but sometimes it's necessary
Christopher McCormack
@cmccormack
Feb 26 2018 22:52
@thekholm80 yeah - you could also break off pieces of your object into state as well then update your global or higher level object when you update child state
Kyle Holm
@thekholm80
Feb 26 2018 22:53
at any rate, it's always good to keep in mind the lifecycle hooks are available