Removing all characters after specific character [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I know this is a simple task, but my Javascript knowledge is (very) limited and I can't seem to get this to work. I've researched it thoroughly and have read through several similar questions here before posting, but still no luck. I'm trying to remove all characters after the comma within a particular div. My poor understanding of Javascript (and programming in general) is certainly limiting me here.
var s = document.getelementsbyclassname('menu-staff');
s = s.substring(0, s.indexOf(','));
document.write(s);
<div class="menu-staff">Value One, Value Two, Value Three, Value Four</div>
I can get this to work for a static string stored in the variable, so I know the Javascript function works, it must simply be a problem with targeting the div by class, right? Thanks for bearing with a newbie. I fully expect this to be flagged as a duplicate and removed in a matter of hours, but hopefully someone will be kind enough to guide an ignorant designer towards the light. :)

Split your string into parts and then just output the first one.
var str = document.querySelector('.menu-staff').innerHTML;
console.log(str);
var parts = str.split(',');
document.write(parts[0]); //Pssst... You shouldn't use document.write. Manipulate the DOm instead.
<div class="menu-staff">Value One, Value Two, Value Three, Value Four</div>

You could split pieces of your string separated by a comma into an array and just keep the first element :
var s = "bonjour, et bienvenu";
console.log(s.split(",")[0]); // bonjour

Related

Trying to understand the jQuery('#none') selector [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I am working on a small project for work and I came across a piece of code that is iterating a counter, and if that counter is not 0 then it executes this small bit of code:
if (cnt) jQuery('#none').prop('checked', false);
I am trying to understand what the jQuery('#none') selector is. When I execute it alone as just a selector within the chrome console, it returns an empty set. I have been googling for about 30 minutes, searching jQuery/JavaScript/CSS docs and all I can find are a few references to things where people are using this selector, but the topic in question is not in regard to the selector itself. To me this line appears to be doing literally nothing as commenting it out does not seem to change the behavior.
I really prefer to not mess around with stuff I don't understand as that almost always results in bugs. Can anyone point me to some documentation or just explain here what the #none selector is?
EDIT:
It would appear there is no magic #none selector. My assumption is this was left over from a previous iteration, it threw me off since the person who originally wrote it does not write erroneous code like this and I was finding people using the same selector online with no explanation - but those instances were red herrings.
jQuery('#none') selects all elements where id=none. If there are no elements with the id of "none", then it will return an empty set.
<div id=none>This div has the id = "none"</div>
It sounds like you found a piece of 'cruft' (aka zombie code).

Same function different MD5 hash results [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to calculate the hash of a string. And I found that the result changes depending on the method I use.
In this webpage https://codebeautify.org/md5-hash-generator.
If I use the form writing the string in the text field:
16120&{"number":"4545","params":"{\"locale\":\"en_EN\"}"}
I get 26528d6e0e802d5569e2e03fde0a825c. However if I do
CryptoJS.MD5('16120&{"number":"4545","params":"{\"locale\":\"en_EN\"}"}').toString();
the result is fe31f378efcc4ae4de71e70278991741.
If I use a simple string like 1234 I get the same result but using the one above I don't so I guess the problem is escaped bars or something but I can't find a solution.
In your JS code, JS is escaping the quotes. The website is not escaping. So you are hashing different things, hence different hash results.
The Backslashes for escaping the string are only ok in-code because they will not be part of the resulting string. See w3schools -> chapter "Escape Character".
Try executing console.log('\"') to understand this, it results in logging only a single doublequote (").
So if you remove these escape characters and insert the string in the input (where escaping is neither needed nor supported), the hash will be equal.
The backslashes in the second case need to be doubled up. Once for JavaScript and once for JSON.
CryptoJS.MD5('16120&{"number":"4545","params":"{\\"locale\\":\\"en_EN\\"}"}').toString();

Expanding ${var} references in a JavaScript string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Learning something beyond vanilla JavaScript and the book I'm reading is telling me a statement like:
let someVar = 'Happy';
console.log('I hope you have a ${someVar} day.');
Should display 'Happy'? in the log or an alert or possibly anywhere.
It doesn't work. I'm using FireFox Dev Ed and I just get a line with the entire:
${someVar}
in it. Any guidance... Is this a weird transpiler issue or ES6+ issue?
You need backticks surrounding the string there in order for the interpreter to properly interpret it as a template literal.
let someVar = 'Happy';
console.log(`I hope you have a ${someVar} day.`);
When you have a normal string, you can use single quotes ' or double quotes ", but when you're using a template literal, you must always use backticks `.
You can also use backticks anyway even if you aren't interpolating any variables inside, just so you don't have to escape quote characters, for example.

Is it bad form to treat argument names the same as variable names in javascript? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Whenever I define a function in javascript, I frequently name the place holder what I would like them to be. This leads to the problem that, declaring those arguments as variables in the function makes them have the same name. Is this a problem (because I do not often see it done).
For example:
var words = 'Hello there';
var talk = function talk (words) {
// Realizing this does nothing, I am leaving it in because other answers refer to it.
var words = words;
//I suppose this could also be this.words = words?
console.log(words);
}
talk(words); //To log "hello there"
This is an extreme example, but in many instances it seems like this would make more sense than using similar and not quite correct words (like var letters = words)
As a principle for readability, it is best to avoid unnecessary confusion. In your example, if your var-words was a deep copy of words that was then modified, then what would get printed? Even if you know the answer, will your fellow developers? Will they actively go and find out the correct answer before modifying the code? Especially in the case of javascript where it tries to make all code as runnable as possible, it could lead to undefined behaviour and bugs.
In most scenarios this wouldn't need to happen because you should be naming your copies appropriately, or not having them at all if they don't serve a purpose that deserves a new name (such as sortedWords, or duplicateWords, fiveLetterWords, etc)
If you HAVE to do as your extreme example states, the best I can come up with is changing the argument variable name to "words_argument" or something similar to avoid confusion.
Edit: seems like OP edited the question to remove the 'var words = words' line within the function, which is what most of my answer was referring to
You should avoid variable hoisting in JS because this could lead to a bigger problems
refer to these links:
http://thecomputersarewinning.com/post/a-dangerous-example-of-javascript-hoisting/
http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

Find your pokemon - how does it work? JS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I found this site http://pokemon.step.lv/
If you type in your name and surname, it gives you your pokemon. I was just wondering, if somebody tells me which pokemon they are, is it possible for me to figure out their name?
I don't want to use it in any bad way, I'm just really curious, I tried to find something in this code http://www.google-analytics.com/urchin.js but I'm just a beginner at programming in general so it's pretty difficult for me, but it also makes me more interested in it.
If anyone is bored enough to try to help me, please just take a look
I'm not sure how many pokemon there are. But I know there are fewer pokemon (men)? than there are names.
There's something called the pigeonhole principle that states, essentially, if you have n pigeons, and n-1 pigeonholes, each pigeon can't have its own hole. It's the same thing with names. Since there are more names than there are pokemon, there must be people who share the same pokemon. Thus, the answer is no, it's impossible.
There is an array of pokemons. Server side script calculates hash-code from your name and gets an element from this array according to this hashcode. An arbitrary logic can be applied here, to generate array index from this hashcode.

Categories