The nesting problem with “+=” (javascript) [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 2 years ago.
Improve this question
Why is the result of JS -3?
a in JS does not seem to change with subsequent assignments
c:
int a=3;
a+=a-=a*a;
result:
a=-12;
js:
var a=3;
a+=a-=a*a;
result:
a=-3;

C and JavaScript have different rules to handle such expressions.
Clang (and probably also GCC and other C compilers) triggers a warning:
1.c:5:7: warning: unsequenced modification and access to 'a' [-Wunsequenced]
a+=a-=a*a;
~~ ^
1 warning generated.
In plain English this says that one of the read operations will not get the initial value of a but the current value of a at the moment when that operation is executed.
The statement above is executed the same way as:
a-=a*a;
a+=a;
This is why the result in C is -12 but it could be -3 as well.
Avoid writing such expressions. Even when there is no ambiguity about how they are evaluated, they are difficult to read and understand by other developers. One of the other developers is an older version of you. The code is written once but it is read many times. Let the code be easy to read an understand.

Related

Why Node.js cannot run my JavaScript code in the terminal? [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
Can anyone please tell me why my javascript code cannot be executed? What is Uncaught SyntaxError: Unexpected Identifier?
You have a > before your text cursor. This means that you're already in Node.
Right now, what's happening is you're telling Node to run the following JavaScript:
node index.js
which, of course, is not valid JavaScript.
You need to exit Node so that the standard terminal is displayed, such as cmd or powershell (press Control-C a couple times)
Then you can type in node index.js. If you're in the same directory as the index.js file, it'll execute.

innerHTML is changing the value entered [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
In an app I'm working on, I'd found that updating the DOM by rewriting a new value to innerHTML doesn't give the expected result.
I typed the following command in the DevTools console:
document.querySelector('main-app').shadowRoot.querySelector('.unitslist').innerHTML = '<unit-bar>1</unti-bar><unit-bar>2</unti-bar>'
With the following result:
<unit-bar>1<unit-bar>2</unit-bar></unit-bar>
You have a typo in your strings. You open with <unit-bar>, but close with </unti-bar>. The closing tags are therefore probably ignored by the browser and it guesses where they should be, resulting in the difference you see.
so , you can solve to avoid overwriting: -
1. get the content of the div and that add the current value to update and the existing values
DOMElem.innerHTML = DOMElem.value + (value to be updated)

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.

javascript/jquery syntax differences, declaring new a variable [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 5 years ago.
Improve this question
My code in javascript causes an error at the following line:
var tries:int=GetCallAttemptCount();
The error message says Error in Script Unexpected token : , Line 3
Line 3 is where the error appears. The script was originally in jquery, so I wonder is there a difference in how a variable is declared in Javascript and Jquery?
I'm computer literate, but I am a beginner with Javascript.
Thanks all, Adam
That's not javascript, this looks like typescript, in JS:
var tries = GetCallAttemptCount();

Javascript String.prototype unexpected token [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 6 years ago.
Improve this question
Apologies if this has been asked before but I couldn't find this specific issue.
I've been tasked with analysing a bit of highly obfuscated, and malicious, JS code. I'm trying to get it to run in a VM at the minute just to see exactly what it's up to but I'm getting a syntax error on the first line.
function String.prototype.x(){...
From Chrome's dev console:
Uncaught SyntaxError: Unexpected token .
The red squiggly line shows that the error is being thrown at the first dot (between String and prototype). To be honest, I don't know enough about JS to figure it out but I'm sure one of you lovely lot will know the answer. Why's it thrown and what can I do to fix it?
String is already a class (function with the inbuilt prototype methods) in JS , in order to create methods in its prototype chain you have use as below
String.prototype.reverse = function(){
return this.split('').reverse().join()
}
"hello".reverse() // olleh

Categories