How to perform math on an array's values in javascript? - javascript

I have an array with the following value:
2, *, 5
How can I execute this string like so:
2 * 5
so the result returned is 10?

The most horribly insecure way possible would be to do something like this:
eval([2,"*",5].join(''))
But I could never recommend doing that, like ever. The "right" way to do it would be to write some kind of parser.
var ops = [2,"*",5]
var val = ops.shift();
while(ops.length > 0) {
var item = ops.shift();
switch(item) {
case "*": val *= ops.shift();
case "+": val += ops.shift();
case "-": val -= ops.shift();
case "/": val /= ops.shift();
}
}
This would essentially work like a very simple calculator... but I still couldn't really recommend this approach.
What are you trying to do exactly? Maybe there is a better way to model what you are trying to do other than an array?

Assuming you can handle your own precedence using order and parentheses, and you are okay with using eval, then use eval:
var str = "2,*,5";
var exp = str.split(",").join("");
var n = eval(exp);
alert(n);
Also, that assumes that you have no spaces in your initial string.

You could just do:
var array = [2, '*', 5];
eval(array.join(''));
This is one of the few things eval is useful for, however you likely don't want to give it just anything.

Related

Recursive function to find binary not returning elements in single array. (Not pushing to the same )

Problem statement: I'm trying to get string > binary without using the inbuilt method in javascript.
This is a piece of program where a string input (like "ABC") is accepted, then it is translated to an array of equivalent code value ([65,66,67]).
Function binary() will change a number to binary. But I'm unable to join them together to loop through all the contents. Please help. (I'm a noob, please forgive my bad code and bad explanation)
var temp3 = [65,66,67];
var temp2 = [];
var r;
for(i=0;i<temp3.length;i++) {
var r = temp3[i];
temp2.push(binary(r));
}
function binary(r) {
if (r === 0) return;
temp2.unshift(r % 2);
binary(Math.floor(r / 2));
return temp2;
}
console.log(temp2);
I think this is a cleaner version of this function. It should work for any non-negative integers, and would be easy enough to extend to the negatives. If we have a single binary digit (0 or 1) and hence are less than 2, we just return the number converted to a string. Otherwise we call recursively on the floor of half the number (as yours does) and append the final digit.
const binary = (n) =>
n < 2
? String (n)
: binary (Math.floor (n / 2)) + (n % 2)
console.log (binary(22)) //=> '10110'
console.log ([65, 66, 67] .map (binary)) //=> ['1000001', '1000010', '1000011']
In your function you have this code
var r = temp3[i];
I don't see any temp3 variable anywhere in your code above so I'd imagine that could be causing some issues.

Hacker Rank test errors on .count

I'm taking a hackerrank test that is a simple puzzle that states that 8=2 and 0,4,6,9 = 1 and all other numbers are equal to 0. So I wrote this function in javascript, and it works in ie out of notepad, but when I put it into the hackerrank console I get an error that points to the .count. Does anyone know why this would happen? I'm new to javascript so it maybe be a simple I just can't figure it out. Any help would be great. Thanks
var str = prompt("Number: ");
String.prototype.count = function(match) {
var res = this.match(new RegExp(match,"g"));
if (res==null) { return 0; }
return res.length;}
document.write((str.count(8)*2)+ str.count(4) + str.count(6) + str.count(9) + str.count(0));
};
Not sure what you mean. Maybe a switch is in order:
function pickANumber(num){
switch(num){
case 8:
return 2;
case 0: case 4: case 6: case 9:
return 1;
default:
return 0;
}
}
console.log(pickANumber(8));
console.log(pickANumber(5));
console.log(pickANumber(9));
It's generally frowned upon to prototype built-in objects like String or Array. It would be much easier to just define a standalone function that did the exact same thing, and you shouldn't have to worry about count not being defined. The problem might be that Hacker Rank isn't letting you modify the String object? Not sure about that though. Try using something like this:
function count(str, letter) {
// your code
}

Convert string to number

I have some simple variables whose values are numeric strings:
var s = '10*10*10',
v = '60*10';
I want to turn s into 1000 and v to 600.
Use eval() function:
var result = eval('10*10*10');
alert(result); // alerts 1000
If the strings are from a truly trusted source, you can use eval to do that:
var s = '10*10*10';
var result = eval(s);
But note that eval fires up a JavaScript parser, parses the given string, and executes it. If there's any chance that the given string may not be from a trusted source, you don't want to use it, as you're giving the source the ability to arbitrarily execute code.
If you can't trust the source, then you'll have to parse the string yourself. Your specific examples are easy, but I'm sure your actual need is more complex.
The dead easy bit:
var s, operands, result, index;
s = '10*10*10';
operands = s.split('*');
result = parseInt(operands[0], 10);
for (index = 1; index < operands.length; ++index) {
result *= parseInt(operands[index], 10);
}
...but again, I'm sure your actual requirement is more complex — other operators, spaces around the values, parentheses, etc.
Picking up on Andy E's comment below, whitelisting might well be a way to go:
function doTheMath(s) {
if (!/^[0-9.+\-*\/%() ]+$/.test(s)) {
throw "Invalid input";
}
return eval('(' + s + ')');
}
var result = doTheMath('10*10*10'); // 1000
var result2 = doTheMath('myEvilFunctionCall();'); // Throws exception
Live example
That regexp may not be perfect, I'd stare at it long and hard before I'd let any unwashed input head its way...
this could be achieved quite simply without resorting to eval
function calc(s) {
s = s.replace(/(\d+)([*/])(\d+)/g, function() {
switch(arguments[2]) {
case '*': return arguments[1] * arguments[3];
case '/': return arguments[1] / arguments[3];
}
})
s = s.replace(/(\d+)([+-])(\d+)/g, function() {
switch(arguments[2]) {
case '+': return parseInt(arguments[1]) + parseInt(arguments[3]);
case '-': return arguments[1] - arguments[3];
}
})
return parseInt(s);
}
alert(calc("10+5*4"))
You can use the eval function to evaluate an expression in a string:
var evaluated = eval(s);
alert(evaluated) will then alert 1000.
If you "just" want to have these numbers out of the string you can do
eval(s)
to get "10*10*10" as a Number

What's the best way to convert a number to a string in JavaScript?

What's the "best" way to convert a number to a string (in terms of speed advantage, clarity advantage, memory advantage, etc) ?
Some examples:
String(n)
n.toString()
""+n
n+""
like this:
var foo = 45;
var bar = '' + foo;
Actually, even though I typically do it like this for simple convenience, over 1,000s of iterations it appears for raw speed there is an advantage for .toString()
See Performance tests here (not by me, but found when I went to write my own):
http://jsben.ch/#/ghQYR
Fastest based on the JSPerf test above: str = num.toString();
It should be noted that the difference in speed is not overly significant when you consider that it can do the conversion any way 1 Million times in 0.1 seconds.
Update: The speed seems to differ greatly by browser. In Chrome num + '' seems to be fastest based on this test http://jsben.ch/#/ghQYR
Update 2: Again based on my test above it should be noted that Firefox 20.0.1 executes the .toString() about 100 times slower than the '' + num sample.
In my opinion n.toString() takes the prize for its clarity, and I don't think it carries any extra overhead.
Explicit conversions are very clear to someone that's new to the language. Using type coercion, as others have suggested, leads to ambiguity if a developer is not aware of the coercion rules. Ultimately developer time is more costly than CPU time, so I'd optimize for the former at the cost of the latter. That being said, in this case the difference is likely negligible, but if not I'm sure there are some decent JavaScript compressors that will optimize this sort of thing.
So, for the above reasons I'd go with: n.toString() or String(n). String(n) is probably a better choice because it won't fail if n is null or undefined.
The below are the methods to convert an Integer to String in JS.
The methods are arranged in the decreasing order of performance.
var num = 1
Method 1:
num = `${num}`
Method 2:
num = num + ''
Method 3:
num = String(num)
Method 4:
num = num.toString()
Note: You can't directly call toString() on a number. 2.toString() will throw Uncaught SyntaxError: Invalid or unexpected token.
(The performance test results are given by #DarckBlezzer in his answer)
Other answers already covered other options, but I prefer this one:
s = `${n}`
Short, succinct, already used in many other places (if you're using a modern framework / ES version) so it's a safe bet any programmer will understand it.
Not that it (usually) matters much, but it also seems to be among the fastest compared to other methods.
...JavaScript's parser tries to parse
the dot notation on a number as a floating point literal.
2..toString(); // the second point is correctly recognized
2 .toString(); // note the space left to the dot
(2).toString(); // 2 is evaluated first
Source
Tongue-in-cheek obviously:
var harshNum = 108;
"".split.call(harshNum,"").join("");
Or in ES6 you could simply use template strings:
var harshNum = 108;
`${harshNum}`;
The simplest way to convert any variable to a string is to add an empty string to that variable.
5.41 + '' // Result: the string '5.41'
Math.PI + '' // Result: the string '3.141592653589793'
I used https://jsperf.com to create a test case for the following cases:
number + ''
`${number}`
String(number)
number.toString()
https://jsperf.com/number-string-conversion-speed-comparison
As of 24th of July, 2018 the results say that number + '' is the fastest in Chrome, in Firefox that ties with template string literals.
Both String(number), and number.toString() are around 95% slower than the fastest option.
I recommended `${expression}` because you don't need to worry about errors.
[undefined,null,NaN,true,false,"2","",3].forEach(elem=>{
console.log(`${elem}`, typeof(`${elem}`))
})
/* output
undefined string
null string
NaN string
true string
false string
2 string
string
3 string
*/
Below you can test the speed. but the order will affect the result. (in StackOverflow) you can test it on your platform.
const testCases = [
["${n}", (n) => `${n}`], // 👈
['----', undefined],
[`"" + n`, (n) => "" + n],
[`'' + n`, (n) => '' + n],
[`\`\` + n`, (n) => `` + n],
[`n + ''`, (n) => n + ''],
['----', undefined],
[`String(n)`, (n) => String(n)],
["${n}", (n) => `${n}`], // 👈
['----', undefined],
[`(n).toString()`, (n) => (n).toString()],
[`n.toString()`, (n) => n.toString()],
]
for (const [name, testFunc] of testCases) {
if (testFunc === undefined) {
console.log(name)
continue
}
console.time(name)
for (const n of [...Array(1000000).keys()]) {
testFunc(n)
}
console.timeEnd(name)
}
I'm going to re-edit this with more data when I have time to, for right now this is fine...
Test in nodejs v8.11.2: 2018/06/06
let i=0;
console.time("test1")
for(;i<10000000;i=i+1){
const string = "" + 1234;
}
console.timeEnd("test1")
i=0;
console.time("test1.1")
for(;i<10000000;i=i+1){
const string = '' + 1234;
}
console.timeEnd("test1.1")
i=0;
console.time("test1.2")
for(;i<10000000;i=i+1){
const string = `` + 1234;
}
console.timeEnd("test1.2")
i=0;
console.time("test1.3")
for(;i<10000000;i=i+1){
const string = 1234 + '';
}
console.timeEnd("test1.3")
i=0;
console.time("test2")
for(;i<10000000;i=i+1){
const string = (1234).toString();
}
console.timeEnd("test2")
i=0;
console.time("test3")
for(;i<10000000;i=i+1){
const string = String(1234);
}
console.timeEnd("test3")
i=0;
console.time("test4")
for(;i<10000000;i=i+1){
const string = `${1234}`;
}
console.timeEnd("test4")
i=0;
console.time("test5")
for(;i<10000000;i=i+1){
const string = 1234..toString();
}
console.timeEnd("test5")
i=0;
console.time("test6")
for(;i<10000000;i=i+1){
const string = 1234 .toString();
}
console.timeEnd("test6")
output
test1: 72.268ms
test1.1: 61.086ms
test1.2: 66.854ms
test1.3: 63.698ms
test2: 207.912ms
test3: 81.987ms
test4: 59.752ms
test5: 213.136ms
test6: 204.869ms
If you need to format the result to a specific number of decimal places, for example to represent currency, you need something like the toFixed() method.
number.toFixed( [digits] )
digits is the number of digits to display after the decimal place.
The only valid solution for almost all possible existing and future cases (input is number, null, undefined, Symbol, anything else) is String(x). Do not use 3 ways for simple operation, basing on value type assumptions, like "here I convert definitely number to string and here definitely boolean to string".
Explanation:
String(x) handles nulls, undefined, Symbols, [anything] and calls .toString() for objects.
'' + x calls .valueOf() on x (casting to number), throws on Symbols, can provide implementation dependent results.
x.toString() throws on nulls and undefined.
Note: String(x) will still fail on prototype-less objects like Object.create(null).
If you don't like strings like 'Hello, undefined' or want to support prototype-less objects, use the following type conversion function:
/**
* Safely casts any value to string. Null and undefined are converted to ''.
* #param {*} value
* #return {string}
*/
function string (str) {
return value == null ? '' : (typeof value === 'object' && !value.toString ? '[object]' : String(value));
}
With number literals, the dot for accessing a property must be distinguished from the decimal dot. This leaves you with the following options if you want to invoke to String() on the number literal 123:
123..toString()
123 .toString() // space before the dot 123.0.toString()
(123).toString()
I like the first two since they're easier to read. I tend to use String(n) but it is just a matter of style than anything else.
That is unless you have a line as
var n = 5;
console.log ("the number is: " + n);
which is very self explanatory
I think it depends on the situation but anyway you can use the .toString() method as it is very clear to understand.
.toString() is the built-in typecasting function, I'm no expert to that details but whenever we compare built-in type casting verse explicit methodologies, built-in workarounds always preferred.
If I had to take everything into consideration, I will suggest following
var myint = 1;
var mystring = myint + '';
/*or int to string*/
myint = myint + ''
IMHO, its the fastest way to convert to string. Correct me if I am wrong.
If you are curious as to which is the most performant check this out where I compare all the different Number -> String conversions.
Looks like 2+'' or 2+"" are the fastest.
https://jsperf.com/int-2-string
We can also use the String constructor. According to this benchmark it's the fastest way to convert a Number to String in Firefox 58 even though it's slower than
" + num in the popular browser Google Chrome.
Method toFixed() will also solves the purpose.
var n = 8.434332;
n.toFixed(2) // 8.43
You can call Number object and then call toString().
Number.call(null, n).toString()
You may use this trick for another javascript native objects.
Just come across this recently, method 3 and 4 are not appropriate because how the strings are copied and then put together. For a small program this problem is insignificant, but for any real web application this action where we have to deal with frequency string manipulations can affects the performance and readability.
Here is the link the read.
It seems similar results when using node.js. I ran this script:
let bar;
let foo = ["45","foo"];
console.time('string concat testing');
for (let i = 0; i < 10000000; i++) {
bar = "" + foo;
}
console.timeEnd('string concat testing');
console.time("string obj testing");
for (let i = 0; i < 10000000; i++) {
bar = String(foo);
}
console.timeEnd("string obj testing");
console.time("string both");
for (let i = 0; i < 10000000; i++) {
bar = "" + foo + "";
}
console.timeEnd("string both");
and got the following results:
❯ node testing.js
string concat testing: 2802.542ms
string obj testing: 3374.530ms
string both: 2660.023ms
Similar times each time I ran it.
Just use template literal syntax:
`${this.num}`

Javascript string replace with calculations

Is there a way to resolve mathematical expressions in strings in javascript? For example, suppose I want to produce the string "Tom has 2 apples, Lucy has 3 apples. Together they have 5 apples" but I want to be able to substitute in the variables. I can do this with a string replacement:
string = "Tom has X apples, Lucy has Y apples. Together they have Z apples";
string2 = string.replace(/X/, '2').replace(/Y/, '3').replace(/Z/, '5');
However, it would be better if, instead of having a variable Z, I could use X+Y. Now, I could also do a string replace for X+Y and replace it with the correct value, but that would become messy when trying to deal with all the possible in-string calculations I might want to do. I suppose I'm looking for a way to achieve this:
string = "Something [X], something [Y]. Something [(X+Y^2)/(5*X)]";
And for the [___] parts to be understood as expressions to be resolved before substituting back into the string.
Thanks for your help.
There's no direct, built-in way (well, okay, perhaps there is — see below), but if you use the callback feature of the replace function, where the replacement can be a function rather than a string (the return value is what's substituted in), you can implement this fairly easily.
For instance, suppose you use the Ruby notation #{xyz} for your placeholders. This code loops through those:
var mappings, str;
str = "One #{X} three #{Y} five";
mappings = {
"X": 2,
"Y": 4
};
str = str.replace(/\#\{([^#]+)\}/g, function(match, key) {
var result;
result = mappings[key];
/* ...processing here */
return result;
});
The resulting string is One 2 three 4 five, because #{X} and #{Y} have been replaced via lookup. You can look at the key and see whether it's an expression and needs to be evaluated rather than simply looked up. That evaluation is where your real work comes in.
Now, you could use with and eval to achieve expression support; change the result = mapping[key]; line above to this:
with (mappings) {
result = eval(key);
}
If you feed the string "One #{X} three #{Y} five #{X + Y * 2}" into that, the result is One 2 three 4 five 10 — because 2 + 4 * 2 = 10.
That works because with sticks the given object on top of the scope chain, so it's the first thing checked when resolving an unqualified reference (like X), and eval executes Javascript code — and so can evaluate expressions — and magically does so within the scope in which it's called. But beware; as Eric pointed out, not all operators are the same in various forms of expression, and in particular Javascript interprets ^ to mean "bitwise XOR", not "to the power of". (It doesn't have an exponent operator; you have to use Math.pow.)
But you need to be very careful about that sort of thing, both with and eval (each in their own way) can be problematic. But the main issues with with are that it's hard to tell where something comes from or where it will go if you do an assignment, which you're not; and the main issues with eval come from using it to interpret strings you don't control. As long as you keep safeguards in place and are aware of the issues...
Boiling that down into a function:
function evaluate(str, mappings) {
return str.replace(/\#\{([^#]+)\}/g, function(match, key) {
var result;
with (mappings) {
result = eval(key);
}
return result;
});
}
alert(evaluate(
"The expression '(#{X} + #{Y}) * 2' equals '#{(X + Y) * 2}'",
{"X": 2, "Y": 4}
)); // alerts "The expression '(2 + 4) * 2' equals '12'"
alert(evaluate(
"The expression '(#{X} + #{Y}) * 2' equals '#{(X + Y) * 2}'",
{"X": 6, "Y": 3}
)); // alerts "The expression '(6 + 3) * 2' equals '18'"
The only way I can think of to achieve this would be a templating engine such as jTemplates. Also see the answers to this SO question.
Nice question:
function substitutestring(str,vals)
{
var regex = /\[[^\]]*\]/gi;
var matches = str.match(regex);
var processed = [];
for(var i = 0; i<matches.length; i++)
{
var match = matches[i];
processed[match] = match.slice(1,-1);
for(j in vals)
{
processed[match] = processed[match].replace(j,vals[j]);
}
processed[match] = eval("("+processed[match]+")");
}
for(var original in processed)
{
str = str.replace(original,processed[original]);
}
return str;
}
document.write(
substitutestring(
"[x] + [y] = [x+y]",
{"x": 1, "y": 2}
)
);
In ES6 you can now use template strings:
var X = 2, Y = 3;
string = Tom has ${X} apples, Lucy has ${Y} apples. Together they have ${X+Y} apples;

Categories