javascript unexpected calculation result [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 8 years ago.
Improve this question
Codes:
g = function () {
H = 3
return H + H
}
f = function () {
Η = 2
return Η + H
}
// 3 + 3 = 6
alert(g())
// 2 + 2 = 5
alert(f())
Live demo:http://jsfiddle.net/qhRJY/light/
While the output is 6 and 5.
It is strange.
Then I try to change the value of the H, the result is still unexpected.
What's the magic here?

In f, the first and second Η are actually the Greek letter Eta, not the Roman H. They look similar, but they're not the same character. It has Unicode code point 0x51377, rather than ASCII code 0x48. So you're adding two different variables.
Would you find the answer unexpected if it were written like this? Because this is equivalent to what you wrote.
g = function () {
H = 3
return H + H
}
f = function () {
Eta = 2
return Eta + H
}
// 3 + 3 = 6
alert(g())
// 2 + 2 = 5
alert(f())

Its a tricky question .Modify the problem with alphabet H.It seems like Roman letters or something special characters.It works fine with 'H' for me.

f = function () {
Η = 3
return Η + H
}
for this code, change to this
f = function () {
I = 3;
return I + I;
}
The 'H' is not cleared from the first function, it's still using the previous value. 2 + 3 = 5

Related

Something is wrong with my if and else statement [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 1 year ago.
Improve this question
Here is my code:
var points = 10
if(points == 10) {
points + 200;
}
else if (points === 100) {
points + 10;
}
console.log(points)
What happens is that it logs 10, when what i would like to happen is that it logs 210. Any idea on what i have done wrong? I got some feedback on it on an earlier question, but it still does not seem to work.
When we use assignment operator we have to use like this:
points = points + 200
so in your code at line "points + 200" or "points + 10" have not changed the value of variable points at all. So, variable points is the same as the first line (var points = 10)
You might modify your program like this:
var points = 10
if(points == 10) {
//points = points +200
points + 200;
}
else if (points === 100) {
//points = points + 10
points + 10;
}
console.log(points)

Convert integers to string and then adding using recursion [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
Hi I am trying to solve a problem where the input for a function digital_root(n) will add the digits. I am not sure what I am doing wrong.
function digital_root(n) {
// ...
//1. separate n into array of digits
var nString = n.toString();
//[ '1', '2', '3', '4' ]
var numbersToAdd = [];
var total = 0;
for (var i = 0; i < nString.length; i++) {
numbersToAdd.push(+nString.charAt(i));
}
// result is [ 1, 2, 3, 4 ]
//2. add digits
for (var x = 0; x < numbersToAdd.length; x++) {
total += numbersToAdd[i];
//expected outputs
// total = 0 + numbersToAdd[0]--> 0+1--> total = 1
// total = 1 + numbersToAdd[1]-->1+2--> total = 3
// total = 3 + numbersToAdd[2]-->3+3--> total = 6
// total = 6 + numbersToAdd[3]-->6+3--> total = 9
}
return total;
}
console.log(digital_root(1234));
You need to use "x" instead of "i"
So changing
total += numbersToAdd[i];
to
total += numbersToAdd[x];
will fix an issue.
Also output should be 10 instead of 9, there is calculation mistake in your question

How to rate to difficulty of simple algebra operations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a simple app to help my daughter practice basic Algebra (first grade). I would like to assign a score to each response depending on the difficulty of the operation, where 2+2 is worth less than 12-7, for example.
Do you know of any existing algorithm I can look up and adapt to my needs?
EDIT
if you want to see the app https://algebro.herokuapp.com
if you want to see the code https://gitlab.com/etozzato/algebro
**EDIT ← **
trying to make the question more specific ;)
given two integers and a basic algebraic operation
algorithm input: int a,b and string operation
algorithm output: float difficulty
→ What are the factors that can help inferring a difficult coefficient?
I would surely look at the input numbers, where their distance can be
significant in determining the complexity of the operation. 10 + 1 is clearly easier than 7 + 5 because (when not memorized and instantly responded) it takes longer counting time;
As an amendment to the previous statement, common/simple arguments should decrease the complexity of the operation: 0 or 10 are a good example;
I don't know of any algorithm to find the "difficulty" of an equation but if I had more time, I might try to play around with something like this... Even though it is for reading, the concept might be adaptable to arithmetic.
Anyway here is a super silly post-midnight crack at something that might work with some tweaking for extremely basic arithmetic. You can tweak the factors/weights but this might get you started. Good luck!
function get_difficulty (eq) {
var difficulty = 0;
var settings = {
terms_factor : 3, //Multiply by the number of terms in an equation
digits_factor : 2, //Multiply by the number of digits in each term
negative_weight : 2, //Add this if subtracting two numbers in the equation yields a negative number
operations : {
"+" : 1,
"-" : 2,
"*" : 4,
"/" : 6,
"=" : 0
}
};
eq += "=";
var ptr = 0;
var terms = 0;
var prev_term = null;
var len = eq.length;
var stack = [ ];
var is_numeric = function (n) {
return /\d+/.test (n); //Not a brilliant way but works for basic arithmetic
};
while (ptr < len) {
var tok = eq [ptr];
if (tok !== " " && tok !== "(" && tok !== ")") {
if (is_numeric (tok)) {
stack.push (tok);
} else if (tok in settings.operations) {
var curr_term = parseInt (stack.join (""));
if (prev_term !== null && curr_term > prev_term && ["-", "="].indexOf (tok) !== -1) {
difficulty += settings.negative_weight;
}
difficulty += stack.length * settings.digits_factor;
prev_term = curr_term;
stack = [ ];
terms++;
difficulty += settings.operations [tok];
} else {
console.log ("Unknown token: " + tok);
}
}
ptr++;
}
difficulty += terms * settings.terms_factor;
return difficulty;
}
console.log (get_difficulty (" 2 + 2 ")); //11
console.log (get_difficulty (" 12 - 7 ")); //14
console.log (get_difficulty (" 7 - 12 ")); //16
console.log (get_difficulty (" 5 - 5 ")); //12
console.log (get_difficulty (" 5 - 1205 ")); //20
console.log (get_difficulty (" 5 - 1205 * 35 ")); //29
console.log (get_difficulty (" 5 * 40 ")); //18
console.log (get_difficulty (" 102 - 5 / 13 + 32 ")); //39
console.log (get_difficulty (" 100 - 100 ")); //20
console.log (get_difficulty (" 32 - 12 ")); //16
console.log (get_difficulty (" 12 - 32 ")); //18

How was this .js archive encrypted? [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 7 years ago.
Improve this question
How was this code encrypted? A former webmaster left me code encrypted this way. I do not know how to solve.
Code:
function setCookie(a, b, c) {
var d = new Date();
d[_0x6fff[1]](d[_0x6fff[0]]() + 24 * c * 60 * 60 * 1e3);
var e = _0x6fff[2] + d[_0x6fff[3]]();
document[_0x6fff[4]] = a + _0x6fff[5] + b + _0x6fff[6] + e;
}
function getCookie(a) {
var b = a + _0x6fff[5];
var c = document[_0x6fff[4]][_0x6fff[8]](_0x6fff[7]);
for (var d = 0; d < c[_0x6fff[9]]; d++) {
var e = c[d];
while (_0x6fff[11] == e[_0x6fff[12]](0)) e = e[_0x6fff[10]](1);
if (e[_0x6fff[13]](b) != -1) return e[_0x6fff[10]](b[_0x6fff[9]], e[_0x6fff[9]]);
}
return _0x6fff[14];
}
Looks like it's a combination of minification and Hex-encoded Chinese characters:
Minification is a way of reducing the size of a javascript file by replacing long variable names with single letters (a, b, c in your example above)
_0x6fff is the HEX representation of a HAN character: cross on stepping-stones
Once code has been minified, you can't really undo it. See here

Repeat function takin previous result of this function [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
var x = function (a) { return a + a/4 - 600}
I have a function that does something.
So now my goal is to repeat this function 12 times with an argument used from previous operation;
Let's say initial ammount is 5000;
So
`x(5000) = 5650;
x(5650) =6462.5;
x(6462.) =...;
and this should be repeated 12 times;
So how can this be done in code?
`
Certainly the easiest way would be to use a for loop:
var x = function(a) { return a + a/4 - 600 },
v = 5000;
for (var i = 0; i < 12; i++) {
v = x(v);
}
console.log(v);
I'd not recommend doing something like this, but instead of a loop you could use recursion:
var x = function(42.34, 12);
----
function(double a, int maxSize)
{
a = ((a + a) / (4 - 600));
if(maxSize>0){
maxSize--;
return function(a,maxSize);
}else{
return a;
}
}
(just pseudocode)

Categories

Resources