Variable's strings count | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Variable's strings count

How can i count how many times a letter repeats herself between many variables? var match3 = "V"; var match2 = "S"; var match3 = "v"; then how can I put an If statement that processes something if there are two "v" and one "s"?

1st Nov 2018, 5:36 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
14 Answers
+ 4
let limits scope of the variable and var does not so these days I use let for most declarations. https://stackoverflow.com/questions/762011
2nd Nov 2018, 7:13 PM
John Wells
John Wells - avatar
+ 3
Janningā­ I like your friends concept, but the JavaScript is the leader. It can modify both HTML & CSS as needed on the fly. HTML controls what can be displayed. While it can call JavaScript functions, those functions can add and remove those calls. CSS controls how things are displayed.
2nd Nov 2018, 3:51 PM
John Wells
John Wells - avatar
+ 3
Here is an example: https://code.sololearn.com/Wdf5uHvZ6C1o It supports single character strings of "a", "b", or "c".
2nd Nov 2018, 4:35 PM
John Wells
John Wells - avatar
+ 2
I'd write a function that is called with the variable and returns a list of 26 (a to z) counts. Call it with your various variables and sum up the returned lists to get your final numbers.
2nd Nov 2018, 3:33 AM
John Wells
John Wells - avatar
+ 2
Thanks very much!! John Wells
2nd Nov 2018, 6:07 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
+ 1
I'm not sure I fully understand what you're asking for. Can you please provide a link to your Code Playground code so we can help you troubleshoot the specific issues you are running into? If you have inline comments to help out with the thought processes, that would also be helpful. šŸ™‚ The Code Playground should be relatively easy to find in the website version of SoloLearn, but if you are in the app version, it can be accessed via the curly braces, green circle with plus sign to add a new project, then select the project type. Looking forward.
2nd Nov 2018, 12:04 AM
Janningā­
Janningā­ - avatar
+ 1
Janningā­ I don't have a code right now because I don't know how to make it. I try to explain it better: I would like to make a function that works in base of the the repetition of a certain value of a variable. For example with a number of matches played by a team: var milan = { match1 : "W" match2 : "L" match3 : "W" Where "W" means win and "L" lost. Then I want to make a function that can calculate how many matches have been won, so how many "W" are contained in the variable. This function should work like "if there are 2 "W" return a certain value" or "if there are 3 "W" return an other value. That's all, if you have any advice I would really apreciate it.
2nd Nov 2018, 11:43 AM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
+ 1
I think John has a good suggestion for you then. You should try that and see how far you get. Might be similar to this: https://www.sololearn.com/Discuss/1568681/?ref=app Except you would add whatever unspecified logic to the counts once you have them. Is this for setting up tournament brackets or something?
2nd Nov 2018, 11:53 AM
Janningā­
Janningā­ - avatar
+ 1
The part about calling a function? If JavaScript is your first programming (more specifically, scripting) language, then I can see how it can be confusing. Actually, I'm not all that excited that this became a standard in the Web world, but I digress. šŸ™‚ In a group of three friends, I think of HTML as the general leader and the CSS and JavaScript file(s) are the two followers. The followers are relatively passive and only do something when the HTML file says, "Hey! I think it would be great if you did this (function)!" Then the JavaScript file says, "Umm, okay, let me see if I can do that (function)" and looks through its code to see if it has a function like that. The HTML file "invokes" ("calls") the function. Otherwise, functions usually just sit around and hang out in the JavaScript file. Further reading: https://www.w3schools.com/js/js_functions.asp (To round out the story: CSS is the stylish friend -- they just get to look pretty. šŸ˜œ)
2nd Nov 2018, 12:21 PM
Janningā­
Janningā­ - avatar
+ 1
John Wells , this is true. šŸ™‚ My impression was that for a static site, you need the HTML file. For a dynamic site, you can generate the HTML file on the fly.
2nd Nov 2018, 3:56 PM
Janningā­
Janningā­ - avatar
+ 1
Alessandro Palazzolo if all you care about is won or lost games, the code would be totally different than keeping track of how many of each letter.
2nd Nov 2018, 3:56 PM
John Wells
John Wells - avatar
0
Janningā­ I think I didn't really understand the one from John Wells. I'm sorry I'm just starting to learn, would you mind to explain it more...in particular the second part.
2nd Nov 2018, 12:05 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
0
Could you give me any example or advice?John Wells
2nd Nov 2018, 4:13 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
0
John Wells I would like to ask you just one last question. Did you use let instead of var for any particular reason or not?
2nd Nov 2018, 7:05 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar