How to use lastIndexOf() function with symbols - javascript

Just started using indexOf() and lastIndexOf() functions and I know why they are used, however, the result doesn't make me feel happy :)
let str = $('#info').html();
// WORKS
//alert(str.lastIndexOf('√'));
// DOESN'T WORK
alert(str.lastIndexOf('√'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="info">√</div>
The problem is I get the alert result as "-1", which means the √ couldn't be found in the str variable. Using simple symbol √ it works, however, I'm not sure if it's a good practice using this symbol here.
In my opinion, another approach about this problem would be encoding √ symbol in the HTML to √, so using "Inspect element" feature you would see √.
What do you think?

There is no direct way to achieve this. But if you still want to do this way then you simply need to create a HEX value of the ASCII value:
let str = ascii_to_hexa($('#info').html());
str = '&#x0'+str.toUpperCase()+';';
alert(str.lastIndexOf('√'));
function ascii_to_hexa(str)
{
var arr1 = [];
for (var n = 0, l = str.length; n < l; n ++){
var hex = Number(str.charCodeAt(n)).toString(16);
arr1.push(hex);
}
return arr1.join('');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="info">√</div>

When the browser reads and parses your HTML, it builds up a DOM, without retaining the exact HTML you provided. Later, if you ask for HTML, it builds a new HTML string using its own rules for doing that.
That's why str.lastIndexOf('√') doesn't work: The browser isn't under any obligation to give you back the character in the same form you used when you supplied it. It could give it back as just a character (√) or a named character reference (√ in this case) or a decimal numeric character reference (√), rather than the hex numeric character reference you're looking for.
You'll have to test on your target browsers to see what they give you, and then look for that. I suspect most if not all will return the actual character, and so your str.lastIndexOf('√') (or str.lastIndexOf('\u221A')) will be the way to go.
<div>√</div>

Related

Calculate the number of statements in a javascript function

I want to work out the number of statements inside a block of javascript code. This is to evaluate how short the code is for a programming challenge (if there's a better/easier way to evaluate code, I'm interested in hearing that too).
For the purposes of this evaluation, I would like to assume a statement is anything that is capable of performing an operation within it, for example:
let values = ['test 1', 'test 2'];
for(let i in values) {
let object = {
a: i%3,
b: Math.floor(i/3),
c: i*2
};
let another = {test: 0 || 4};
let something = values[i];
let otherSomething = getSomeValues(object[a], object[b]);
setSomeValues(object[a], object[c]);
for(let j = 0; j < 5; i++) if(i < j) break;
}
There's quite a lot of syntax to cover so ideally I would like to do this with a library if one is available - my Googling was unable to find anything very suitable.
I tried writing a regex to match all possible breaks between statements, but this is getting messy quickly:
[\n;]|\)[ \w]|[{,][\s\w]+:\s*|[{}]
Here's a link to the regexr I've been using. Breaking this down:
[\n;] - matches a newline or semicolon, the normal ways to start a new statement
\)[ \w] - matches statements following a closing bracket, e.g. if (something) return;
[{,][\s\w]+:\s* - matches the key of a key-value pair in an object
[{}] - matches opening and closing brackets of blocks
I also remove any zero-length matches as statements cannot be empty.
As I've said, ideally I would prefer a library but I wanted to ask for some opinions on my approach and if I've got most of the edge cases.
Since you are trying to understand a particular chunk of code and not building a library, you should check out astexplorer.net
Here is a link that displays a nicely parsed tree and if desired, you can configure the example to use alternative parsers (babel, acorn, eslint, etc).

How to print on same line using console.log() in JavaScript?

I want to print a list (var lst=[1,2,3,4,5]) using a loop on the same line. Can I do that in JavaScript ?
Well you can achieve this by doing this. Make an empty string variable and then concatenate each array value to it, then print it out. This will work
var lst = [1,2,3,4,5];
var output = "";
for(var i =0; i <= lst.length; i++){
output += lst[i] + " ";
}
console.log(output);
In NodeJS and as long as you didn't change your standard output you can use process.stdout.write("Something here") as the standard output of your application is terminal (console).
process.stdout.write("Hello ")
process.stdout.write("World")
process.stdout.write("!")
Hello world!
Console.log doesn't allow you to print in the same line. However the task that you are trying to do can be done using the below:
console.log(lst.join(" "))
Output: 1 2 3 4 5
You could use Function#apply or spread syntax ....
var list = [1, 2, 3, 4, 5];
console.log.apply(null, list); // traditional
console.log(...list); // ES6
The exact behavior of console.log is not specified by ECMAScript, so it varies from implementation to implementation, but it is intended to be used for logging events. The fact that each event usually becomes a single line in a web browser's console window led it to be the Node.js equivalent of "println", but outside the context of a specific implementation its behavior is not predictable.
You can pass multiple parameters to log, in which case most (but not all) implementations will print them all out on the same line separated by whitespace. So if you're using one of those implementations, and you have a list and just want all the elements on one line separated by space, you can use apply to call the method as if each element of the list was a separate argument:
console.log.apply(console, lst);
Caveat: if the first argument is a string that contains what look like Formatter format control sequences (%s, etc.), it will be parsed as such and the remaining arguments used to fill in those slots.
But the most reliable way to achieve the desired result is to build a single string yourself representing what you want the final line to look like, and then call console.log exactly once with that string as the argument.
You could do it easily by using the join() function which can be used on any array in JavaScript!
For example:
var lst = ["check","word","3","could","hello"]
console.log(lst.join())
source-code:
https://jsfiddle.net/Lpdurxsb/

What unicode character I can use to "flag" a string?

I want to represent an object that has several text properties, every one representing the same text value but in different languages. In case the user modifies a single field, the other fields should be revised, and I'm thinking on adding a single Unicode character at the beginning of the string of the other fields, and then to check for fields that need attention, I just have to check the value at obj.text_prop[0].
Which Unicode character can I use for this purpose? Ideally, it would be non-printable, supported in JS and JSON.
Such flagging should be done some other way, at a protocol level other than character level. For example, consider as making each language version an object rather than just a string; the object could then have properties such as needsAttention in addition to the property that contains the string.
But in case you need to embed such information into a string, then you could use ZERO WIDTH SPACE U+200B. As such it means line break opportunity, but this should not disturb here. The main problem is probably that old versions of IE may display it as a small rectangle.
Alternatively, you could use a noncharacter code point such as U+FFFF, if you can make sure that the string is never sent anywhere from the program without removing this code point. As described in Ch. 16 of the Unicode Standard, Special Areas and Format Characters, noncharacter code points are reserved for internal use in an application and should never be used in text interchange.
I would suggest you not to use strange characters in the beginning of the line. You can implement something like this:
<script type="text/javascript">
function LocalizationSet(){};
LocalizationSet.prototype.localizationItems = [];
LocalizationSet.prototype.itemsNeedAttention = [];
LocalizationSet.prototype.setLocalization = function(langId, text)
{
this.localizationItems[langId] = text;
this.itemsNeedAttention[langId] = true;
}
LocalizationSet.prototype.getLocalization = function(langId)
{
return this.localizationItems[langId];
}
LocalizationSet.prototype.needsAttention = function(langId)
{
if(this.itemsNeedAttention[langId] == null)
{
return false;
}
return this.itemsNeedAttention[langId];
}
LocalizationSet.prototype.unsetAttentionFlags = function()
{
for(var it in this.itemsNeedAttention)
{
this.itemsNeedAttention[it] = false;
}
}
//Example
var set = new LocalizationSet();
set.setLocalization("en","Hello");
set.setLocalization("de","Willkommen");
alert(set.needsAttention("en"));
alert(set.needsAttention("de"));
set.unsetAttentionFlags();
alert(set.needsAttention("en"));
set.setLocalization("en","Hi");
alert(set.needsAttention("en"));
//Shows true,true,false,true
</script>

quantity of white spaces left of a string jQuery

I need to count the white spaces left of a string with jQuery.
For example:
String: " Hello how are you? " will have 4 white spaces at its left!
How can i get that with jQuery?
thanks.
Using regexp in plain old JavaScript:
var spacesOnLeft = myStr.match(/^ */)[0].length
No loops involved. :)
This is something that's doable with plain old javascript.
function countLeftBlanks(arg) {
var i = 0;
while (i < arg.length && arg[i] === ' ') {
i++;
}
return i;
}
If we're using RegExp, the below might be a better cross-environment solution:
var spacesOnLeft = ( myStr.match(/^ */) || [[]] )[0].length;
The above stops a TypeError from being thrown in certain environments when the result of match is null. In the official ECMAScript Language Specification, the match method states that:
If n = 0, then return null.
Despite this, most modern browsers seem to return an array with an empty string in it. In some environments, however, the ECMAScript definition is honoured, and a TypeError will be thrown if attempting to access matched[0]. The NodeJS environment is a good example of this.

IE Issue with Javascript Regex replacement

r = r.replace(/<TR><TD><\/TD><\/TR>/gi, rider_html);
...does not work in IE but works in all other browsers.
Any ideas or alternatives?
I've come to the conclusion that the variable r must not have the value in it you expect because the regex replacement should work fine if there is actually a match. You can see in this jsFiddle that the replace works fine if "r" actually has a match in it.
This is the code from fiddle and it shows the proper replacement in IE.
var r = "aa<TR><TD></TD></TR>bb";
var rider_html = " foo ";
r = r.replace(/<TR><TD><\/TD><\/TR>/gi, rider_html);
alert(r);
So, we can't really go further to diagnose without knowing what the value of "r" is and where it came from or knowing something more specific about the version of IE that you're running in (in which case you can just try the fiddle in that version yourself).
If r came from the HTML of the document, then string matching on it is a bad thing because IE does not keep the original HTML around. Instead it reconstitutes it when needed from the parsed page and it puts some things in different order (like attributes), different or no quotes around attributes, different capitalization, different spacing, etc...
You could do something like this:
var rows = document.getElementsByTagName('tr');
for (var i = 0; i < rows.length; i++) {
var children = rows[i].children;
if (children.length === 1 && children[0].nodeName.toLowerCase() === 'td') {
children[0].innerHTML = someHTMLdata
}
}
Note that this sets the value of the table cell, rather than replacing the whole row. If you want to do something other than this, you'll have to use DOM methods rather than innerHTML and specify exactly what you actually want.

Categories

Resources