These are chat archives for FreeCodeCamp/HelpJavaScript

4th
Aug 2018
贾开元
@777585
Aug 04 2018 05:41
@cmccormack My webpage is crushed, unfortunately I write a recursive infinite loop. Every time I open this page and it stopped.
Bryan Xu
@bryanpalau
Aug 04 2018 11:15

Sorry I have a rookies question about JS:

var ourArray = [1,2,3];
ourArray[1] = 3;
var ourArray = [1,3,3];

WHY?@@

Simon Cordova
@gbsimon87
Aug 04 2018 11:17
Hi @bryanpalau
Don't worry about the quality of questions that's why the community is here
Elements inside arrays you have to count them starting from 0
So when you first declared the array..
The element's value at the [1] position is 2. You then replaced that with a 3, so the array changes to [1, 3 ,3]
Kelechi Chinaka
@ke1echi
Aug 04 2018 11:31
https://www.codewars.com/kata/scramblies/train/javascript @Chrono79 my solution is O(n^2)... i cant think of a O(n) solution... a O(n) hint from you would help me
Bryan Xu
@bryanpalau
Aug 04 2018 11:33
@gbsimon87 thank you so much! It is an aha moment for me :)
Simon Cordova
@gbsimon87
Aug 04 2018 11:50
@ke1echi I'd assume you'd need to be looping over an array no?
Kelechi Chinaka
@ke1echi
Aug 04 2018 11:52
i dont understand @gbsimon87
Simon Cordova
@gbsimon87
Aug 04 2018 11:52
The link you posted, you need help with that assignment?
Kelechi Chinaka
@ke1echi
Aug 04 2018 11:52
yes
Simon Cordova
@gbsimon87
Aug 04 2018 11:56
I think you need to loop through either str1 or str2 and check the characters one at a time
Kelechi Chinaka
@ke1echi
Aug 04 2018 11:57
i know..i did it in O(n^2)... i need todo it in O(n) @gbsimon87
Aditya
@ezioda004
Aug 04 2018 12:33
@ke1echi I did it with O(n+m), made a character dictionary for str2 and made it run against str1.
Kelechi Chinaka
@ke1echi
Aug 04 2018 12:39
how do you mean made it run against str1 @ezioda004
Aditya
@ezioda004
Aug 04 2018 12:43
@ke1echi Basically loop through the str1 once and check the dictionary for each character occurrence of str1 in that dictionary, modify the dict and finally return result based on that modified dictionary.
Aditya
@ezioda004
Aug 04 2018 13:41

@ke1echi

if (hash[str1[i]] < 0) {
        return false;
      }

This shouldnt matter

scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2

So even if hash[str[i]] goes negative, its not a big deal. You just need to make sure that after the 2nd loop every character of the hash has < 0or in other words, no character in the hash should have a positive value.

Kelechi Chinaka
@ke1echi
Aug 04 2018 13:54
you mean i need 1 more loop to confirm all character in hash < 0 @ezioda004
Aditya
@ezioda004
Aug 04 2018 13:57
@ke1echi Object.values() will help you out :smile:
Kelechi Chinaka
@ke1echi
Aug 04 2018 13:58
thats also a loop under the hood :smile: @ezioda004
function scramble(str1, str2) {
  var hash = {};

  for (var i = 0, len = str2.length; i < len; i++) {
    (hash[str2[i]]) ? hash[str2[i]]++ : hash[str2[i]] = 1;
  }

  for (let i = 0, len = str1.length; i < len; i++) {
    if (hash[str1[i]]) {
      hash[str1[i]]--;
    }
  }

  for (var key in hash) {
    if (hash[key] > 0) return false;  
  }

  return true;
}
@ezioda004
Aditya
@ezioda004
Aug 04 2018 14:03
@ke1echi Is it working?
Kelechi Chinaka
@ke1echi
Aug 04 2018 14:03
yes @ezioda004
Aditya
@ezioda004
Aug 04 2018 14:03
Nice :sparkles:
tundeiness
@tundeiness
Aug 04 2018 20:41
guys I need help with some of the user stories in the "Build a Technical Documentation Page " project. I'm not clear with the following statements:
User Story #8: I can see a nav element with a corresponding id="navbar".
User Story #9: The navbar element should contain one header element which contains text that describes the topic of the technical documentation.
User Story #10: Additionally, the navbar should contain link (a) elements with the class of nav-link. There should be one for every element with the class main-section.
User Story #11: The header element in the navbar must come before any link (a) elements in the navbar.
Now my understanding is that the nav element should be within a header element and not vice versa as the above statement is making me to understand. Naturally I should have a header element which has a nav element in it. Can anyone help with making it clearer? Thanks.
Christopher McCormack
@cmccormack
Aug 04 2018 22:13
@tundeiness header is not isolated to the top of the page, but is instead used anywhere you would need header information.
You can have several <header> elements in one document.
Nerando Johnson
@Nerajno
Aug 04 2018 23:39
is there a way to see if a variable is not in an array
?