These are chat archives for FreeCodeCamp/HelpJavaScript

5th
Feb 2018
Stephen James
@sjames1958gm
Feb 05 03:47
The component with the id of "app" is just a hook for React. You should do your styling and such on the react components
Ayush Bahuguna
@relentless-coder
Feb 05 05:13
hey, I need some help with react
Claudio Restifo
@Marmiz
Feb 05 05:19
what about it?
Thompson jackson
@micheljorden94
Feb 05 07:46

function golfScore(par, strokes) {
// 请只修改这条注释以下的代码
if(strokes == 1) return "Hole-in-one!";
else if(strokes <= par -2) return "Eagle";
else if(strokes === par -1) return "Birdie";
else if(strokes === par) return "Par";
else if(strokes === par + 1) return "Bogey";
else if(strokes === par + 2) return "Double Bogey";
else if(strokes >= par + 3) return "Go Home";
return "Change me";
// 请只修改这条注释以上的代码
}

// 你可以修改这一行来测试你的代码
golfScore(5, 4);

heelp
help?tahnks
thanks
Aditya
@ezioda004
Feb 05 08:41
@micheljorden94 You have a typo in return "Go Home";, it should be "Go Home!". Also, return "Change me"; is not required.
Tiago Correia
@tiagocorreiaalmeida
Feb 05 08:51
https://tiagocorreia-gameswishlist.herokuapp.com hey what do you guys think? Still think there's alot to improve for a next project
Jefferson
@jeffersonnnn
Feb 05 09:15
hi guys. A big thanks for help rendered on previous occasions, this community really is my home. Okay, so i am trying to achieve something in jquery and i can't seem to get it to worl . When my mouse hovers over a set of divs, i have set them (the divs now) to change colour,. However, the lastdiv refuses to change colour. Can anyone please point to me what i am doing wrong?
$(document).ready(function() {
        $('.rating-circle').hover(
        function() {
            $(this).prev().css('background-color', 'yellow')
        },    function() {
            $(this).css('background-color', '')
        });
    });
Aditya
@ezioda004
Feb 05 09:35
@jeffersonnnn Because of prev()? Since its the last div $(this).prev().css('background-color', 'yellow') fires on the div before so your last div doesnt have the hover event.
Jefferson
@jeffersonnnn
Feb 05 11:29
@ezioda004 if prev() solves my problem albeit with a slight malfunction, which method would you advice i use .
Aditya
@ezioda004
Feb 05 11:42
@jeffersonnnn If you want to hightlight the last one without changing prev() then on top of my head you can give the last div an id say id = "last" and modify your code like this
$(document).ready(function() {
        $('.rating-circle').hover(
        function() {
            if (this.id === "last"){
            $(this).css('background-color', 'yellow');
           }
            $(this).prev().css('background-color', 'yellow')
        },    function() {
            $(this).css('background-color', '')
        });
    });
The problem with the above code would be tho that when hovering over the last both of them changes color
Sweet Coding :)
@SweetCodingInc
Feb 05 14:48
@jeffersonnnn you're better off delegating this to css
.rating-circle: hover{
  background-color: yellow;
}
Blauelf
@Blauelf
Feb 05 15:15
I still don't get what the prev is meant to do. I know what it does, but I don't understand the kind of effect intended.
Stephen James
@sjames1958gm
Feb 05 15:16
@Blauelf Yeah, it wasn't clear what he was intending to do from a problem definition perspective
Razvan Jackson
@RazvanJackson
Feb 05 15:17
@sjames1958gm Can you help me with React Routing?
I don't know why is not working the NotFound page
Jen
@jenniredfield
Feb 05 15:24

Hi guys can I ask about regexes. Currently

 var reg = /^1?\s?(\()?\d{3}((\)\s?)?|((-)?|(\s)?))?\d{3}(-|\s)?\d{4}$/g;

is validating all US numbers apart from cases:

telephoneCheck("(555-555-5555") and telephoneCheck("1 555)555-5555") because is not detecting that it has unmatching parenthesis. I am unsure though, how do you make a regex that checks if there's an opening parenthesis, there should be aclosing one? Or will I have to check this separately?

Jen
@jenniredfield
Feb 05 15:31
Ah nevermind, just checked separately :P
Ken Haduch
@khaduch
Feb 05 15:36
@jenniredfield - you can combine that and simplify it, at least that's what i recall.
Norman Dela Cruz
@dcnr
Feb 05 15:39
Oh we can't view our old solutions anymore?
Ken Haduch
@khaduch
Feb 05 15:39
@dcnr - they had a note on one of the pages that said that they temporarily have hidden the solutions but that they are stored safely on the site.
@dcnr - Note: We have temporarily disabled viewing and sharing solutions, however they are safely stored in your profile. You can learn more about this here.
Norman Dela Cruz
@dcnr
Feb 05 15:40
@khaduch Ah I see! Thank you!
CamperBot
@camperbot
Feb 05 15:40
dcnr sends brownie points to @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 3712 | @khaduch |http://www.freecodecamp.org/khaduch
Norman Dela Cruz
@dcnr
Feb 05 15:41
Just remembered I had a fcc repo for my solutions, I remember that validate phone number exercise. Such a pain, but so much fun
Ken Haduch
@khaduch
Feb 05 15:43
@dcnr - what I tried to do in some cases with more complicated or longer to solve problems is to keep a text file that I copy and paste my work into. I didn't do it for everything, but I found that it's good in the event that you switch computers or have some other issue that you need to recover it.
Stephen James
@sjames1958gm
Feb 05 15:45
@dcnr I did the same as @khaduch but saved mine into github.
Ken Haduch
@khaduch
Feb 05 15:46
@sjames1958gm - that's a good choice, I should do that with more of my solutions... I have my text file on my dropbox account so it's available amongst my computers that I normally access. Which, of course, github would be, too... :)
Norman Dela Cruz
@dcnr
Feb 05 15:46
@khaduch I'm now keeping notes myself too! Looking at other people's solutions was a great way to learn things! Like just recently I learned about num & 1 instead of num % 2 for checking parity :D
Ken Haduch
@khaduch
Feb 05 15:47
so much to learn (and remember!) that's another reason I keep a running note text file.
Norman Dela Cruz
@dcnr
Feb 05 15:47
@khaduch Yeah! :D
Ken Haduch
@khaduch
Feb 05 15:55
@jenniredfield - I found it easier to just make two separate cases where the parentheses were optional, one that has them and one that doesn't, like (\(\d{3}\)|\d{3}) so that the entire group will have either a parenthesized group of three digits, or a non-parenthesized group of three digits. I am willing to bet that someone could come up with better ideas, but this seemed to be the simplest.
Jen
@jenniredfield
Feb 05 16:09
hey thanks @khaduch
CamperBot
@camperbot
Feb 05 16:09
jenniredfield sends brownie points to @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 3713 | @khaduch |http://www.freecodecamp.org/khaduch
Jen
@jenniredfield
Feb 05 16:09
sounds like a cool idea
Norman Dela Cruz
@dcnr
Feb 05 16:31
@jenniredfield Do you use something like regexr.com? It helps quite a lot when you're building patterns.
Maks Ushakov
@maks-ushakov
Feb 05 17:56

@janepohorecka

@maks-ushakov I would like it to be dynamic.. I am not sure how to implement relative coordinates in a SVG

use lowercase letter insted
C 50 50 150 150 250 50 - absolute
c 0 0 -100 100 200 0 - relative

gusd773
@gusd773
Feb 05 19:30

anybody wanna help? I have to find return the value which has the earliest duplicate value in array:

Note: Write a solution with O(n) time complexity and O(1) additional space complexity, since this is what you would be asked to do during a real interview.

Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does. If there are no such elements, return -1.

Example

For a = [2, 3, 3, 1, 5, 2], the output should be
firstDuplicate(a) = 3.

There are 2 duplicates: numbers 2 and 3. The second occurrence of 3 has a smaller index than than second occurrence of 2 does, so the answer is 3.

For a = [2, 4, 3, 5, 1], the output should be
firstDuplicate(a) = -1.

CODE (thus far) :

function firstDuplicate(a) {
var vals= [];
for (var i=0; i<a.length;i++){
for (var j=i+1; j<a.lenghth; j++){
if (a[i]==a[j] && i!==j){
vals.push(a.indexOf(a[i],j));
}
else{
return;
}
}
}

}

I am thinking of building an array with the indexes of duplicates and then returning the lowest one, thoughts?
Jim Homme
@jhomme
Feb 05 19:31
Hi,
Because I can't see the colors on the screen, I'm having a hard time telling where it is telling me I'm missing a semicolon. Can someone please point me in the right direction?

// Setup
function phoneticLookup(val) {
var result = "";

// Only change code below this line
 var lookup {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
};
result = lookup.val;
// Only change code above this line
return result;

}

// Change this value to test
phoneticLookup("charlie");

Brad
@bradtaniguchi
Feb 05 19:33
@jhomme Your declaration of var lookup is wrong
@jhomme It should be var lookup = {....}
just missing the equals it seems
Jim Homme
@jhomme
Feb 05 19:34
Thank you very much. I knew it had to be something little.
Brad
@bradtaniguchi
Feb 05 19:34
Also the way your using the lookup is wrong (that's part of the lesson so I wont give you the answer there haha)
lookup.val will give undefined, you need to do something else
Jim Homme
@jhomme
Feb 05 19:35
Very good. I don't want to get help unless I really need it.
Brad
@bradtaniguchi
Feb 05 19:40
@jhomme Your close, just gotta figure out how to get a property dynamically :)
Jim Homme
@jhomme
Feb 05 19:42
Hi. I used square brackets and the site says it is right. Now I'm a little confused, because in the previous few lessons, it has said that I could either use dot notation or square brackets. How can I tell when one or the other is wrong?
JLuboff
@JLuboff
Feb 05 19:46
@jhomme When using a variable or parameter name, you must use brackets. With dot notation, it will see it as a string literal
Pieter Stokkink
@forkerino
Feb 05 19:47
@jhomme also when some special characters are in the property name (for example -), you will need backets.
@JLuboff yo
JLuboff
@JLuboff
Feb 05 19:47
@forkerino Yo, whats up?
gusd773
@gusd773
Feb 05 19:49

if somebody can help my code is supposed to return the value which has the earliest duplicate index occurence

function firstDuplicate(a) {
var vals= [];
for (var i=0; i<a.length;i++){
for (var j=i+1; j<a.lenghth; j++){
if (a[i]==a[j] && i!==j){
vals.push(a.indexOf(a[i],j));
}
else{
return;
}
}
}

}

I figured at the end I'd sift through my "vals" array to weed out the smallest index value, help if possible I just want to make sure this is the right setup as I know how to get the smallest value in the array already

Pieter Stokkink
@forkerino
Feb 05 19:52
@JLuboff things are good here, you?
@gusd773 you need to return something
ah, misread your question a bit
Dovydas Stirpeika
@Giveback007
Feb 05 19:54
hey there
Pieter Stokkink
@forkerino
Feb 05 19:54
@gusd773 what can be helpful is that .indexOf returns the first occurrence of a value inside an array (or string)
@Giveback007 :wave:
JLuboff
@JLuboff
Feb 05 19:54
@forkerino Same. Just got a new project for work to start on. Going to use node and SQL for it. Also been redoing the backend for my conference room signs so that they have better control. Currently the inputters can only put in the current days events and to modify after the fact, they have to redo it entirely. So adding the ability to do any time (i.e date selector) and then I'll allow them to edit. Its also allowing me to refactor my code for the sign itself
Pieter Stokkink
@forkerino
Feb 05 19:55
@JLuboff cool, I guess looking at the old code makes you feel both ashamed and happy that you've progressed so much since then? That is my experience with looking at old stuff...
JLuboff
@JLuboff
Feb 05 19:56
@forkerino Oh ya. It also took me a little while to figure out what I had done lol. Had to make notes as I went through my code.
Dovydas Stirpeika
@Giveback007
Feb 05 19:56
@forkerino so awesome you're still helping people out
Pieter Stokkink
@forkerino
Feb 05 19:57
@Giveback007 occasionally
When I have nothing else to do
which is rare
:smile:
Dovydas Stirpeika
@Giveback007
Feb 05 19:58
I'm going to start comeing here more often. Take some of the burden off of you.
I finally feel confident enough to help some JS stuff
Pieter Stokkink
@forkerino
Feb 05 20:01
:+1:
@JLuboff I can imagine
I often can't remember what I was thinking.
Nate Mallison
@NJM8
Feb 05 20:06
Hey you smart guys. :smile: :smile:
How can I have form with two submit buttons with different paths?
I guess so you can
JLuboff
@JLuboff
Feb 05 20:09
@forkerino Luckily I had comments (something I don't really do anymore.. oops) but got it figured out so it's all good :+1: And performance is better thus far too
Dovydas Stirpeika
@Giveback007
Feb 05 20:10
even if you can't you can have a single function call 2 other functions
Diego Mayer
@Chrono79
Feb 05 20:10
:wave:
Dovydas Stirpeika
@Giveback007
Feb 05 20:11
:wave:
Jim Homme
@jhomme
Feb 05 20:11
It said to use brackets when the property name contains spaces. So I tried dot notation because the property name did not have spaces. I think you are saying that dot notation only works if I am directly getting a property value. Or am I still misunderstanding?
Diego Mayer
@Chrono79
Feb 05 20:12
I've created another CW kata, for beginners mostly (although I've seen some more experienced coders having troubles with something like that): https://www.codewars.com/kata/working-with-arrays-i-and-why-your-code-fails-in-some-katas/javascript
JLuboff
@JLuboff
Feb 05 20:13
@Chrono79 I'll try it after lunch :+1:
Diego Mayer
@Chrono79
Feb 05 20:13
I'll take you 5 seconds :P
No math involved (this time), just bug fixing
JLuboff
@JLuboff
Feb 05 20:14
well i am notoriousily bad at debugging others codes :P
Diego Mayer
@Chrono79
Feb 05 20:15
I find easier to debug others code than my own :P
Maybe related to "another pair of eyes..."
JLuboff
@JLuboff
Feb 05 20:18
Well sometimes I can get it easy (debug others code)...but when it is long or semi-complex I get lost in it lol
Diego Mayer
@Chrono79
Feb 05 20:19
That's true
Markus Kiili
@Masd925
Feb 05 20:19
Technically, people with glasses have four eyes.
Diego Mayer
@Chrono79
Feb 05 20:19
Long code (more when it's not well written) becomes a pit of snakes, and like Indy, I hate snakes
Dovydas Stirpeika
@Giveback007
Feb 05 20:22
@Masd925 that sounds technically incorrect
Brad
@bradtaniguchi
Feb 05 20:33
it seems like the only code I maintain is uncommented, untyped complex broken code :/
JLuboff
@JLuboff
Feb 05 20:44
@bradtaniguchi Sounds like my type of code!
Markus Kiili
@Masd925
Feb 05 20:58
@JLuboff Yeah. The era of clean code is over. It is enough that the code seems to work at least sometimes. Testing is also futile, because no testing can ever be proven to find all the bugs. Messy code is also more secure because nobody can read it.
Tiago Correia
@tiagocorreiaalmeida
Feb 05 21:00
hey
Markus Kiili
@Masd925
Feb 05 21:06
@tiagocorreiaalmeida Hello.
Tiago Correia
@tiagocorreiaalmeida
Feb 05 21:11
all good?
JLuboff
@JLuboff
Feb 05 21:11
@Masd925 I think the problem is partially the number of individuals with no formal coding learning. That is, I learned all my coding skills through FCC, videos, and books. But I don't know the Best Practice for many things, I just do them the way I know how to
Tiago Correia
@tiagocorreiaalmeida
Feb 05 21:11
would like to understand how this works, Imagine I created an api that will be called from the same server, how can I make sure that the api only awnser's to server call's?
JLuboff
@JLuboff
Feb 05 21:12
@tiagocorreiaalmeida What do you mean?
You want the api to only answer to your request and no one elses?
Tiago Correia
@tiagocorreiaalmeida
Feb 05 21:13
yes correct :D
JLuboff
@JLuboff
Feb 05 21:13
require a token or something along those lines
an API ID, something like that
Tiago Correia
@tiagocorreiaalmeida
Feb 05 21:16
yeah that makes sense and I didnt tought of that after so many times using third party apis
JLuboff
@JLuboff
Feb 05 21:17
;)
JLuboff
@JLuboff
Feb 05 22:16
@Chrono79 I've spent 5 minutes and haven't figured this out yet :P
Diego Mayer
@Chrono79
Feb 05 22:23
@JLuboff I warned it's mostly for beginners, but some more experience coders could trip with it too ;P
JLuboff
@JLuboff
Feb 05 22:32
@Chrono79 It doesn't make sense. It wants two elements removed not one..?!
Diego Mayer
@Chrono79
Feb 05 22:35
No, only one
Stephen James
@sjames1958gm
Feb 05 23:02
@Chrono79 Professor X wouldn't want to get involved with this.
Diego Mayer
@Chrono79
Feb 05 23:03
:P
Stephen James
@sjames1958gm
Feb 05 23:03
@Chrono79 Interesting that attempts with arr.sort() are in the solution set?
Diego Mayer
@Chrono79
Feb 05 23:04
Yes, I didn't want it to leave beta until some more tests, I'm thinking about removing the sort part, but I don't know if that's a good thing to do now
I'm thinking on doing one with only sort
One thing at a time
JLuboff
@JLuboff
Feb 05 23:06
@Chrono79 Hmmm... I got 40 tests passed lol
Diego Mayer
@Chrono79
Feb 05 23:07
I didn't mean it to be so hard
JLuboff
@JLuboff
Feb 05 23:07
Passed
gusd773
@gusd773
Feb 05 23:20

anybody know how I can check my code or where? I need to check the contents of my "vals" array which is supposed to house the index of duplicate occurences:

function firstDuplicate(a) {
var vals= [];
for (var i=0; i<a.length;i++){
for (var j=i+1; j<a.lenghth; j++){
if (a[i]==a[j] && i!==j){
vals.push(a.indexOf(a[i],j));
}
else{
return;
}
}
}

}

JLuboff
@JLuboff
Feb 05 23:20
@gusd773 use console.log
or pythontutor
gusd773
@gusd773
Feb 05 23:22
tried this on python tutor but that didn't help console.log(vals);
}
JLuboff
@JLuboff
Feb 05 23:22
Keep in mind return stops all code execution.
So if you're first loop the if statement is false, nothing will populate vals
gusd773
@gusd773
Feb 05 23:23
true thanks @JLuboff
CamperBot
@camperbot
Feb 05 23:23
gusd773 sends brownie points to @jluboff :sparkles: :thumbsup: :sparkles:
:star2: 2318 | @jluboff |http://www.freecodecamp.org/jluboff
Diego Mayer
@Chrono79
Feb 05 23:27
@sjames1958gm Purifiers upvoted my kata :P
Stephen James
@sjames1958gm
Feb 05 23:27
@Chrono79 :+1:
Lance
@texas2010
Feb 05 23:29
that is so confused. did i see someone use js in python?.... :(
i dont know why he said python...
JLuboff
@JLuboff
Feb 05 23:29
@texas2010 pythontutor..
Lance
@texas2010
Feb 05 23:29
i never heard about that.
Lance
@texas2010
Feb 05 23:30
ohhh . opps
thanks @JLuboff
CamperBot
@camperbot
Feb 05 23:30
texas2010 sends brownie points to @jluboff :sparkles: :thumbsup: :sparkles:
:star2: 2319 | @jluboff |http://www.freecodecamp.org/jluboff
gusd773
@gusd773
Feb 05 23:32
This message was deleted
Brad
@bradtaniguchi
Feb 05 23:33
@gusd773 Whats up with the formatting there
gusd773
@gusd773
Feb 05 23:33
@bradtaniguchi I'm self taught & have never actually written code outside of freecodecamp or codepen tbh
Brad
@bradtaniguchi
Feb 05 23:34
Before pasting here you can use a formatter, and the template strings (3 ` characters, above and below your code) so we can read it properly
Im more or less self taught as well, but that's no excuse to not format your code. I mean your the one reading it right. Itdoesn'thelpwhenthingsarenotformattedwell,evenifitsstraightforward
I usually format unformatted pased code using this site: http://jsbeautifier.org/
gusd773
@gusd773
Feb 05 23:44
function firstDuplicate(a) {
    var vals = [];
    for (var i = 0; i < a.length; i++) {
        for (var j = i + 1; j < a.lenghth; j++) {
            if (a[i] == a[j] && i !== j) {
                vals.push(a.indexOf(a[i], j));
            }

        }
    }
    console.log(vals);
}
var random = [1, 2, 8, 3, 7, 3, 2];

random = firstDuplicate(random);
idk man I tried formatting it n it keeps coming out the same
@bradtaniguchi
Brad
@bradtaniguchi
Feb 05 23:47
You need to use ``` <- three of those characters before and after the code
when posting here
function firstDuplicate(a) {
    var vals = [];
    for (var i = 0; i < a.length; i++) {
        for (var j = i + 1; j < a.lenghth; j++) {
            if (a[i] == a[j] && i !== j) {
                vals.push(a.indexOf(a[i], j));
            }
        }
    }
    console.log(vals);
}
var random = [1, 2, 8, 3, 7, 3, 2];
random = firstDuplicate(random);
gusd773
@gusd773
Feb 05 23:48
gotcha
Gulsvi
@gulsvi
Feb 05 23:48
```js
your js code goes here
```
Brad
@bradtaniguchi
Feb 05 23:48
^
gusd773
@gusd773
Feb 05 23:54
sorry I never knew those things existed i just thought I'd naturally write code that neatly over time, now I still don't know why nothing is being added to my "vals" array
JLuboff
@JLuboff
Feb 05 23:56
@gusd773 Spelling.
Your second loop declaration has a spelling error.