Cross char string with char javascript

function SimpleSymbols(str) { var letter =['a','b','c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; var newstr = ""; for (var i = 0; i<str.length; i++){ if (str.charAt(i).toLowerCase() in letter){ newstr += "M"; } else{ newstr += "X"; } } return newstr; } 

If str "The argument goes here", it returns XXXXXXXXX. Why doesn't it return MMMMMMMMMM?

+6
source share
3 answers

You are not viewing the entry in the array with in . use indexOf() to find the position of the array entry. indexOf() will return a position or -1 if the record is not found.

 for (var i = 0; i<str.length; i++){ var strChar = str.charAt(i).toLowerCase(); if ( letter.indexOf(strChar) >= 0 ) { newstr += "M"; } … 
+6
source

in operator returns true if the object has a property with name , and not with this value.

An array is basically an object with numerical properties. That is, indexes are the names of the properties of an object. It basically looks like this:

 var letters = { 0: 'a', 1: 'b', ... length: ... }; 

So, in your case, the condition will only be true if str.charAt(i).toLowerCase() returns a number between 0 and letter.length (and since charAt returns only one character, it can only be 0-9 ).

Example:

 > var letters = ['a', 'b', 'c']; > 'a' in letters // array doesn't have a property 'a' false > 0 in letters // array has a property 0 (it the first element) true 

So, since "Argument goes here" does not contain any digits, the in condition will always be false , and therefore you will get the result XXXXXX...

See the question “ How to check if an array includes an object in JavaScript? ” To check for the existence of an element in the array.


FWIW, for the in operator to work, you would need to create a form object:

 var letters = { 'a': true, 'b': true, // ... }; 

but it's a little cumbersome to record.

+3
source

Let me suggest a side view, another way to handle what I think you intend to do using Regular expressions with something like:

"test2".replace(/[az]/gi,"M").replace(/[^M]/g,"X") //Outputs "MMMMX"

String.replace will replace a string containing the letters from [az] i at the end of the expression, which means case insensitive. g means searching for all possible matches, not just the first match. In the second expression [^M] this ^ means negation, so everything that is not M will be replaced by X

There is another way to implement a custom function inside String.replace using regular expressions, and it can be implemented as follows:

 "test2".replace(/([az])|([^az])/gi, function(m,g1, g2){ return g1 ? "M" : "X"; }); 

Groups and | are created in regular expression brackets. means either in this expression ([az])|([^az]) there are 2 groups with letters from az, and the other, which means everything that is not az , with the replacement function, which we requested only for the group g1 , if she is a group 1, equal to M otherwise, is X

Another interesting thing you could do is add this function to your entire line, prototyping it like this:

 String.prototype.traverse = function(){ return this.replace(/([az])|([^az])/gi,function(m,g1){ return g1 ? "M" : "X" });} 

Then it can be used as simply as: "test1".traverse();

0
source

Source: https://habr.com/ru/post/974316/


All Articles