javascript how to "fix" a variable inside a function, through eval? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following function
var label = function() {
return 'File: '+texts[t];
};
which is attached to highcharts, specified here
http://api.highcharts.com/highcharts#plotOptions.pie.dataLabels.formatter
where t has values let's say from 1 to 10 and text[t] corresponds to a different text. I attach this function to 10 highchart tooltips so that it executes the function with a mouseOver event.
The intended logic is that chart 1 has the text[1] label appearing, chart 6 has text[6], etc.
The problem is that all charts have the text[10] appearing, since t has that value when the function is executed.
How can I solve this? Is it a place for eval() like
var label = function() {
return 'File: '+eval(texts[t]);
};
UPDATE: based on comments, trying
var label = function(t) {
return 'File: '+t+' '+texts[t];
};
doesn't work as expected, it prints "File: [object Object] undefined"

This is a very common closure problem:
You probably have t in a for loop, just wrap the code which attaches the handler in another function:
// This will not work the way you might expect
// The value of i is left at 10 because that is the last
// time it is changed in the attacheHandlers1 scope held
// but the closure in the anonymous function used as a callback
// in setTimeout
//
function attachHandlers1(){
for(var i = 0; i < 10 ; i++){
setTimeout(function(){
console.log("Version 1", i);
}, 100)
}
}
// This works because the value is closured in
// attachHandlerImpl as 'x' with different values for
// each invocation
//
function attachHandlers2(){
for(var i = 0; i < 10 ; i++){
attachHandlerImpl(i);
}
}
function attachHandlerImpl(x){
setTimeout(function(){
console.log("Version 2", x);
}, 100);
}
attachHandlers1();
attachHandlers2();
Will output:
Version 1 10
Version 1 10
Version 1 10
Version 1 10
Version 1 10
Version 1 10
Version 1 10
Version 1 10
Version 1 10
Version 1 10
Version 2 0
Version 2 1
Version 2 2
Version 2 3
Version 2 4
Version 2 5
Version 2 6
Version 2 7
Version 2 8
Version 2 9

Without knowing the rest of your implementation details, something like this could work:
var texts = ['Text 1', 'Text 2', 'Text 3']
var label = function(idx) {
return "File: " + texts[idx];
};
label(2) returns "File: Text 3"

Related

Could someone poin-out why this works (JS , .bind() ) [duplicate]

This question already has answers here:
addEventListener calls the function without me even asking it to
(5 answers)
Accessing an object's property from an event listener call in JavaScript
(6 answers)
Closed 1 year ago.
can someone point out why the next code as soon as I open the live server webpage prompts me to input the number ( the one without .bind()) while the one with bind works flawlessly?
let chooseNumber;
const poll = {
question: ' What is your favorite programming language?',
options: ['0 : JavaScript', '1:Python', '2: Rust', '3:C++'],
//This generates [0,0,0,0]. More In the next section :D
answers: new Array(4).fill(0),
registerNewAnswer: function () {
chooseNumber = prompt(`${this.question} \n ${this.options}`);
if (chooseNumber >= 0 && chooseNumber < 4) {
this.answers[chooseNumber] = this.answers[chooseNumber] + 1;
}
console.log(this.answers);
console.log('Munem');
},
};
document
.querySelector('.poll')
.addEventListener('click', poll.registerNewAnswer.bind(poll)); //works
//
// document
// .querySelector('.poll')
// .addEventListener('click', poll.registerNewAnswer());
//doesn't work

How to push a boolean into an array? "undefined" error

Why does the console print "undefined" when I print index 3? I'm trying to set index 3 to false and after 7 secs set it to true.
I've tried to change "nr", index 2, with success trying to narrow down the problem.
if (Math.random() < 0.005) {
nx = Math.random()*1000; //index0
ny = 200; //index1
nr = 5;//index2
var nfall = false; //index3
Apples.push([nx,ny,nr,nfall]); //simply inserting 'false' into index 3 doesn't work either.
setTimeout(function(){ Apples[2][2] = 500; Apples[2][3] = true;}, 7000); //3rd apple
console.log("New circle, x:"+Apples[2][0]+" y:"+Apples[2][1]+" nr:"+Apples[2][2] +" nfall:"+Apples[2[3]]);
//What works - in 7 seconds "nr" is updated from 5 to 500, but nfall is still undefined and NOT = true
Initially I expect the output of false from index 3 "nfall" then I expect the output of true from index 3 "nfall" after 7 secs.
Thank you
In your console.log call, you're accessing the array like this:
Apples[2[3]]
But it should be
Apples[2][3]
In the first version, you're accessing index 3 of the number 2 which actually is not an error, just undefined.
Your issue is the last part - you're accessing Apples[2[3]] which doesn't exist, and it's invalid syntax. You want to get the third item in Apples (which is an array), then access the fourth item of that array - do it simply with `Apples[2][3]:
const Apples = [1, 2, [3, 4, 5, 6]];
console.log(Apples[2][3]);

Bigquery javascript UDF with array

I'm trying to run below query in bigquery using standard SQL and javascript UDF. The query takes forever to run, thus I'm not even able to verify if the function is working or not. Can you please let me know if there is anything wrong with the query that makes it run forever? I tried to change the function call
from IRRCalc(Array<FLOAT64> [cash_flow], ARRAY<INT64> [date_delta]) as IRR to IRRCalc(array(select cash_flow from input),array(select date_delta from input)) as IRR and it resolved the issue. Though I don't understand what's wrong with IRRCalc(Array<FLOAT64> [cash_flow], ARRAY<INT64> [date_delta]) as IRR. Can someone please have a look and shed some light? Many thanks.
Here's the query:
CREATE TEMPORARY FUNCTION IRRCalc(cash_flow ARRAY<FLOAT64>, date_delta ARRAY<INT64>)
RETURNS FLOAT64
LANGUAGE js AS """
min = 0.0;
max = 1.0;
do {
guess = (min + max) / 2;
NPV = 0.0;
for (var j=0; j<cash_flow.length; j++){
NPV += cash_flow[j]/Math.pow((1+guess),date_delta[j]/365);
}
if (NPV > 0){
min = guess;
}
else {
max = guess;
}
} while (Math.abs(NPV) > 0.00000001);
return guess * 100;
""";
WITH Input AS
(
select
cash_flow_date,
date_diff(cash_flow_date, min(cash_flow_date) over (),day) as date_delta,
cash_flow as cash_flow
from cash_flow_table
)
SELECT
cash_flow,
date_delta,
IRRCalc(Array<FLOAT64> [cash_flow], ARRAY<INT64> [date_delta]) as IRR
FROM Input;
And here's the table containing the raw data:
Row cash_flow_date date_delta cash_flow
1 2017-09-08 0 -159951.78265102694
2 2017-09-08 0 -9.272567110204461
3 2017-09-08 0 -1000.0
4 2017-09-08 0 -159951.78265102694
5 2017-09-27 19 3552.8711640094157
6 2017-09-27 19 -544.122218768042
7 2018-03-28 201 -576.4290755116443
8 2018-03-28 201 3763.8202775817454
9 2018-04-02 206 437225.5536144294
Can someone please have a look and shed some light?
to see the difference - just run your SELECT w/o UDF
SELECT
cash_flow,
date_delta,
ARRAY<FLOAT64> [cash_flow],
ARRAY<INT64> [date_delta]
FROM Input
As you can see here - for each row you create array with just one element in it - so actually two arrays with one element in each - that element that respectively belong to same row
when you do ARRAY(SELECT cash_flow FROM input), ARRAY(SELECT date_delta FROM input) you actually create arrays which with respective elements from all rows
finally - when you pass ARRAY with just one element in it - it looks like your while (Math.abs(NPV) > 0.00000001) always true thus loop runs forever
Something along these lines I think
Note: above answers your exact question - but you still most likely have issue with logic - if so - ask new specific question

Serialized array save to mysql with php and orderby [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
This is javascript serialized array:
[{"id":1},{"id":2},{"id":3,"children":[{"id":4,"children":[{"id":5},{"id":6},{"id":7}]},{"id":8}]}]
How to save this (dynamic) to mysql with php like this:
***********************************
| id | subsite_id | orderby |
1 0 0
2 0 1
3 0 2
4 3 0
5 4 0
6 4 1
7 4 2
8 3 1
***********************************
Thanks for answer.
This might not be the best solution, but it's certainly a solution. Recently, I learned about RecursiveIterators and their cousin, RecursiveIteratorIterator. So, I took it upon myself to use them in everything I code (relevant XKCD: https://xkcd.com/208/).
I hacked this up quickly:
class ChildIDIterator implements RecursiveIterator{
private $_array;
private $_position = 0;
private $_parent;
public function __construct(array $array, $parent=0) {
$this->_array = $array;
$this->_parent = $parent;
}
function valid(){
return isset($this->_array[$this->_position]);
}
function current() {
return $this->_array[$this->_position]['id'];
}
function next() {
$this->_position++;
}
function rewind() {
$this->_position = 0;
}
function key() {
return $this->_position;
}
function hasChildren(){
return isset($this->_array[$this->_position]['children']);
}
function getChildren(){
return new self(
$this->_array[$this->_position]['children'],
$this->_array[$this->_position]['id']
);
}
function getParent(){
return $this->_parent;
}
}
This recursively iterates over your (decoded) array and returns the id values. To use it, you can do this:
$json = '[{"id":1},{"id":2},{"id":3,"children":[{"id":4,"children":[{"id":5},{"id":6},{"id":7}]},{"id":8}]}]';
$array = json_decode($json, TRUE);
$iterate = new RecursiveIteratorIterator(new ChildIDIterator($array), RecursiveIteratorIterator::SELF_FIRST);
foreach($iterate as $order=>$id){
echo "UPDATE sites SET subsite_id={$iterate->getParent()}, orderby={$order} WHERE id={$id};\n";
}
DEMO: https://eval.in/57189

How can I mirror a Multidimmensional array in javascript?

I need to be able to mirror a multidimensional array that changes in size. I'm currently hard coding for each particular array size and it is horribly inefficient.
Example:
Arr1 { 1 2 3 Arr2 { 1 2
4 5 6 3 4
7 8 9 } 5 6 }
Mirrored:
{ 3 2 1 { 2 1
6 5 4 4 3
9 8 7 } 6 5 }
Arrays range in size from 2x5 to 4x10.
Ok, so all you need is a horizontal mirror.
I suppose that your array contains an array for every line,
so that means that you just need to reverse every row.
for(var i=0;i<multiarr.length;i++){
multiarr[i].reverse();
}
or even better
multiarr.map(function(arr){return arr.reverse();});
For each of the lines:
For i = 0 to width/2:
arr[line][i] <-> arr[line][width - i]
Shouldn't that work?

Categories

Resources