Javascript eval in function [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I want to make a function that has to generate a random number between 2 numbers and you can save it in a variable. I'm using this code:
function rand(ran1, ran2, randVar) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
eval("var " +randVar+ " = "+randomNumb+";");
}
rand(12, 49, rand1)
alert("Your number is: "+rand1)
The error I get is: Can't find variable: rand1
Can anyone help me?

Using eval for this is entirely unnecessary. I'd recommend something like this instead:
function rand(ran1, ran2) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
return randomNumb;
}
var rand1 = rand(12, 49);
alert("Your number is: " + rand1);
Notice that in calling the function, the only real difference is in where you place the identifier, rand1.
Further Reading
Functions
return

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

Why url query string doesn't pass a variable value? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I am trying to pass a parameter in query string i.e._type=1 but it doesn't pass. It doesn't appear in URL, other values does but not this one. Why ?
SitePaymentReportByBranch = function () {
$('#btnprintSitePaymentByBranch').on('click', function (e) {
e.preventDefault();
if ($("#form1").validationEngine('validate')) {
var _employerID = "";
if ($('#cmbEmployerSitPaymentByParameter :selected').text() == "-Select-") {
alert('Plz Select Employer');
}
var url = '/Reports/frmSitePayment.aspx?_EmployerID=' + $('#cmbEmployerSitPaymentByParameter :selected').val() + '&_Formdate=' + $("#formdate").val() + '&_Todate=' + $("#todate").val() +'_type=1';
commonStartup.openReportWindow(url);
}
});
},
You missed & before _type fix will be '&_type=1'
var url = '/Reports/frmSitePayment.aspx?_EmployerID=' + $('#cmbEmployerSitPaymentByParameter :selected').val() + '&_Formdate=' + $("#formdate").val() + '&_Todate=' + $("#todate").val() +'&_type=1';
Query parameters must be separated with &. You have omitted this for your _type parameter:
'_type=1'
should be;
'&_type=1'

Why does JavaScript get this comparison wrong? [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
This is happening on an angular application I'm building. If a user enters 80 into an HTML input, it always seems to get this comparison wrong.
var x = '80';
var y = 150.9800;
/* Returns incorrect answer */
if (parceFloat(x) < y) {
return true;
} else {
return false;
}
You need to use ParseFloat() not parceFloat() ...
parceFloat is not an existing function.
parceFloat() is not a function, the function is parseFloat()
A simple typo is all the error there is.

JavaScript "this" pointer in nested-functions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
Here is a bit of non-working JavaScript code:
function clientEventsManager(io) {
this.connectedClients = 0;
this.createEventReceivers = function(io) {
io.sockets.on('connection', function(socket) {
this.connectedClients++;
//does not increase "connectedClients" of "clientEventsManager" function
}
}
createEventReceivers(io); //it is the only call to createEventReceivers()
}
var Manager = new clientEventsManager(io); //it is the only instanciation of clientEventsManager
My question is: Is there a way to change clientEventsManager.connectedClients in clientEventsManager.createEventReceivers()?
EDIT: this post is a duplicate of this one, thank's for answering
Quick and dirty solution
function base(param) {
this.attr1 = "azertyuiop";
this.attr2 = 123;
this.attr3 = param;
var self=this;
this.fct1 = function() {
console.log("azerty keyboard first row: "+self.attr1);
//Doesn't work
}
}

What is wrong with this jquery code, its driving my crazy? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I have a div with an id textarea_feedback and a textarea with an id msg.
This is my jQuery code:
$(document).ready(function(){
var text_max = 160;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#msg').keyup(function(){
var text_length = $('#msg').val().length;
var text_remaining = text_max - text_length;
$('#textarea_feeeback').html(text_remaining + ' characters remaining');
});
});
$('#textarea_feeeback') is a typo.
This is actually a very concrete example of the power of Vanilla JS over jQuery, and of jQuery being too clever for its own good. The Vanilla JS code:
document.getElementById('textarea_feeeback').innerHTML = text_remaining+" characters remaining";
... would have thrown an error saying that it can't set innerHTML of null, which is absolutely correct because textarea_feeeback does not exist. jQuery, however, simply has an object with no elements, and setting the html() of those zero elements is really easy: do nothing.
Unfortunately, "do nothing" is the reason why you can't debug it!
$('#textarea_feeeback').html(text_remaining + ' characters remaining');
^--- typo

Categories

Resources