Identifier has already been declared error [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm getting this error when using sample code from the up and going book in the ydkjs series. Can anyone help me out? The error is
Uncaught SyntaxError: Identifier 'ACCESSORY_PRICE' has already been declared
at :1:1
const ACCESSORY_PRICE = 9.99;
var bank_balance = 302.13;
var amount = 99.99;
amount = amount * 2;
// can we afford the extra purchase?
if ( amount < bank_balance ) {
console.log( "I'll take the accessory!" );
amount = amount + ACCESSORY_PRICE;
}
// otherwise:
else {
console.log( "No, thanks." );
}

You are using an environment where executing the very same code twice (or more) doesn't allow you to redeclare already declared consts.
I can easily reproduce the code in Chrome's console, in the Sources/Snippets section. Executing the code for the first time works as expected. Executing it again raises the error as the const is already defined by the prior execution.
Assuming the Chrome's console (or similar) is your environment, one of the workarounds is to hit F5 to refresh the page under the console and rerun the script.
Another, disputably cleaner workaround is to switch to another environment where mutliple executions of the same code have no such unexpected effects.

You can't re-declare a const. This error says you already declared ACCESSORY_PRICE. Remove the second const ACCESSORY_PRICE or var ACCESSORY_PRICE or let ACCESSORY_PRICE.

Related

How do i get just 1 element JS [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
i am trying to add hcaptcha to my website but i am doing it in a strange way i want to get if the div with the class 'check' is hidden or not but i cannot do it.
here is my code
var x = document.getElementsByClassName("check")[0]
var y = window.getComputedStyle(x).display
alert(y)
it gives me the error 'cip.html:62 Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
at cip.html:62'
You need to verify that x is not undefined in case none of the elements have the class your looking for
var x = document.getElementsByClassName("check")[0]
if (x) {
var y = window.getComputedStyle(x).display
alert(y)
} else {
alert('not found')
}

Variables not global even though being globaly defined [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
A website I am making has a startup function that includes a call to another function to change add to or change a global variable and update an element in the HTML document to the value.
function moneyUpdate (x,z){
var k;
if (z == 1){
k = money + x;
money = k;
document.getElementById("moneyPrint").innerHTML ="$"+ k;
}
if (z == 0){
money = x;
document.getElementById("moneyPrint").innerHTML="$"+ money;
}
I call the function line this
//other code
moneyUpdate(1000,0);
and declare the variable like this
var money=0
I'm new to java script so bare withe my probable incompetence.
It prints'x' fine put it doesn't set the value of 'money' to 1000 and calling it up with y=1 doesn't add to 'money'.
I have no clue what to even try.
In your other code, try;
money = moneyUpdate(1000,0);

Trying to get an if else statement to work in Java script on Code academy [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
I am new to programming and I am learning Java script on Code academy my instructions are "Write your own if / else statement. The only instruction is that the result of evaluating the statement is a log to the console of "I finished my first course!". I get thrown with the error message Oops, try again. It looks like you didn't log anything!
This is my code
if (7 == 7)
console.log=("I finished my first course!");
else
console.log=("Meh!");
I am confused as it prints out what is supposed to and I even tried running it in a different browser and was given same error message.
You will later learn that the console is an Object, and objects have methods, like a car has a function to accelerate, so you access the accelerate function of the car using dot notation, like this: "car.accelerate()". In this case you are using the log method of the console, in this manner "console.log()", so you don't need the equal sign because you are not setting any variable.
console.log("message goes here");
Please refer to the javascript documentation by mozilla for more info.
https://developer.mozilla.org/en-US/docs/Web/JavaScript
Take out the = in your console.log method.
if (7 == 7)
console.log("I finished my first course!");
else
console.log("Meh!");
You can try the following code for your purpose.
if (7 == 7) {
console.log("I finished my first course!");
}
else {
console.log("Meh!");
}
if (7 == 7)
{ console.log("I finished my first course!");}
else {
console.log("Meh!");}
That seems to work thanks.

JavaScript check variable globally [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want the global variable exp, when it's 100, to increase the level. I'm using a button to take exp.
It works fine. It gives exp and one other variable. But the level doesn't increment by 1 when exp is 100.
exp = 0;
level = 1;
250 lines of code later, I have this:
if (exp == 100) {
level = level + 1;
exp = 0;
document.getElementById("level").innerHTML = level;
document.getElementById("exp").innerHTML = exp;
}
It doesn't work. After 100, it keeps counting.
What am I doing wrong?
It's difficult to say what's wrong because you haven't included enough code here.
However, it appears that there are a couple of things that could be wrong:
Where have you defined exp and level? You haven't used var to define them above, which means that you have likely defined them somewhere else and are re-defining here, which would reset the count each time it increments, defeating the purpose of the incrementation.
What causes the incrementation to occur? Have you wrapped your if statement in the function you're calling to increment exp? If not, you may only be checking it the first time, which would cause it to never trigger the statement.
I've created a demo here that fills in some of these holes and it works fine in isolation. The question, of course, is how that piece fits within the context of your code. The solution to that requires a bit more information about how you have your project set up.

Javascript, adding to a variable not setting [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm having trouble adding to a var at the moment I can only get it to set, I tried a number of operators like "+=" "++" but it doesn't work :(
I have a function that gets called a number of times inside I have this
score = Math.floor((Math.random()*15)*1);
But when it gets activated the function runs and sets the score then when its run again it overwrites the old score with the new random number, I want to add the old score plus whatever is generated together do I need a 2nd var?
When I tried to use score += Math.floor((Math.random()*15)*1); I got the following error:
Uncaught ReferenceError: score is not defined
Let me take a guess, score was not declared to be an Int but a string
try
var score = 0;
score += Math.floor((Math.random()*15)*1);
The issue is that score += Math.floor((Math.random()*15)*1); really means score = score + Math.floor((Math.random()*15)*1);, but, since you haven't defined score anywhere yet, when it tries to use it in the calculation, it is "undefined" (and tells you as much :D ).
Define score before attempting to use the += operator on it and you should be fine.

Categories