Function can't find variable [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 6 years ago.
Improve this question
I was messing around with my game's CSS, and now I get a variable error for every button I click that calls a function, saying it can't find the variable.
I deleted the CSS and everything has changed, but the game is still broken.
I don't know whats wrong, so I have provided a link to download the code.
The error: reference error: can't find variable: duckClick
Related JavaScript:
var ducks = 0;
function duckClick(number){
ducks = ducks + number;
document.getElementById("ducks").innerHTML = ducks;
This calls duckClick but I'm getting a error saying it can't find it.
<button onclick="duckClick(1)">Find Duck</button>

Ah... a little does go a long way, especially in programming.
You forgot the curly closing duckClick, leaving it undefined.
function duckClick(number) {
ducks = ducks + number;
document.getElementById("ducks").innerHTML = ducks;
} //This curly brace

It seems like you have just forgot to close the duckClick-function with a } in your main.js file.

Related

does anyone know how to correct this code [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 last year.
Improve this question
function tipCalculator(nonTipTotal)
{
var totalWithTip = nonTipTotal * 15;
console.log(`You should pay {totalWithTip} including tip`)
}
function tipCalculator();
It's supposed to print out the statement of the total with the tip included
Maybe this?
function tipCalculator(nonTipTotal) {
const totalWithTip = nonTipTotal * 1.15;
console.log(`You should pay ${totalWithTip} including tip`)
}
const billTotal = 25 // get your bill input somehow - depends how this runs
tipCalculator(billTotal);
You are missing the $ symbol before the opening curly bracket. It should be ${totalWithTip} instead of {totalWithTip}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Problem adding Js variables in HTML with getElementById [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
<body>
<h1>Operadores aritméticos</h1>
<h3>+ Adición (102+103)=</h3><p id="suma1"></p>
<h3>- Substracción</h3><p id="resta1"></p>
<h3>* Multiplicación</h3><p id="multi1"></p>
<h3>/ División</h3><p id="div1"></p>
<h3>% Módulo</h3><p id="mod1"></p>
<script>
var suma = (102+103);
var resta = (36-20);
var multiplicación = (27*30);
var división = (900/30);
var módulo = (106%3);
document.getElementById("suma1").innerHTML = suma;
document.getElementById("resta1")innerHTML = resta;
document.getElementById("multi1")innerHTML = multiplicación;
document.getElementById("div1")innerHTML = división;
document.getElementById("mod1")innerHTML = módulo;
</script>
Hello guys, I have a problem, im pretty new at this (programming with HTML, Js, etc.).The issue is that when I try to make my Js variables appear on HTML (with document.getElementById), they do not appear. Nevertheless, if erase every document.getElementById except the one containing "suma1", the browser displays me the result of the sum (205), but if I add even one of them, the browser doesn´t display anything.
I hope I was clear with my problem, it seems very simple but hard to explain.
Any suggestions?
Thanks in advance
The reason you're not seeing anything when you add the statements to populate resta1, multi1, div1, and mod1 is probably because they all have a syntax error. This is likely causing even the first statement (suma1, which is syntactically valid) not to work.
Valid Statement
The 1 statement that is working is document.getElementById("suma1").innerHTML = suma;
Invalid statements
All the other statements follow this pattern:
document.getElementById("id")innerHtml = variable;
Note that you're missing the . between getElementById("id") and innerHtml. If you add the missing . then it should all work as expected.

Why am I getting "Uncaught SyntaxError: Unexpected token *" here? [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
I'm getting it for the * in the chunk of code
// click function for the "See More Scores" and "See Fewer Scores" buttons
$('.show-fewer-or-more-scores').click(function ( ) {
var rowsNotFirst = $(this).closest('tbody').children('tr:gt(0)');
// unravel the scores in an animated fashion
rowsNotFirst.filter(function (idx, el) {
setTimeout(function ( ) { $(el).toggleClass("hidden"); },
50 * idx);
});
});
and I don't see why. I put it in JSHint and was not alerted of any problems that would be causing this. Full code can be seen as commit bd4629b of https://github.com/jamkin/Snake/blob/master/SnakeGame/Scripts/game.js. You can also see the live bug here although it may be fixed if you're accessing this thread a day or more later than it was posted.
Any ideas what I've done wrong?
You have an artifact hanging around at the end of the file
var idx50 = 50*idx;
rowsNotFirst.filter(function (idx, el) {
setTimeout(function (){ $(el).toggleClass("hidden");}, idx50);

Why is my delete where statement not working? [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 8 years ago.
Improve this question
I am trying to delete a user from table USERS. Unfortunately, I keep getting sent to onSqlError. I can't figure out why. When I alert the request it gives me the correct name for the getTheId variable. Not too familiar with sql so maybe I have written something incorrectly. Any pointers appreciated.
// DELETE RECORD
function deleteRecord(getTheId){
deleteUser.onclick = (function () {//deleteUser is an element generated for each user when a button is clicked
var sqlStr2 = 'DELETE FROM USERS WHERE username = '+getTheId+'';
alert("SQL: " + sqlStr2); //This gives me the statement above with the correct name of the user clicked.
if (db){
console.log("db is there");//this logs
db.transaction(function(tx) {
tx.executeSql(sqlStr2);
}, onSqlError, onSqlSuccess); //THEN I GET SENT TO ERROR
}
});
}
Try this:
var sqlStr2 = "DELETE FROM USERS WHERE username = '"+getTheId+"'";
Try adding a ';' to the end of your statement, although without an error message it's hard to tell.

How to add property to a global Javascript object inside a function [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 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources