Posts

Showing posts with the label function

For loops in JavaScript functions

For loops in JavaScript functions const animals = ["dog", "cat", "tree frog"]; function pluralize(array) { for (var i = 0; i <= array.length; i++) { array[i] += "s" return (array) } } console.log(pluralize(animals)); My goal is to create a function that adds the s to the end of words. However when I ran my code s was only added to the first word in the array. Why was the s not added to the other words in the array? Because you immediately return in your loop. Move the return out of the loop to the end of the function. – ASDFGerte Jul 1 at 9:13 return return Also you should iterate to i < array.length . Otherwise you will operate on array[array.length] , which is after the last element. – ASDFGerte Jul 1 at 9:20 ...

How to delay a function returning a value before response from database? [duplicate]

How to delay a function returning a value before response from database? [duplicate] This question already has an answer here: I have a function which modifies a variable as per response from database. function f () { let x = { }; redis.get ("key1").then ( value => x.prop1 = value); redis.get ("key2").then ( value => x.prop2 = value); return x; } This is my code roughly. It is actually in a for loop, using node-redis. I have multiple calls to Redis. I add the data from Redis to a new object and return the object. PROBLEM: function f () is returning an empty object every time I call it. f () How do I wait for all Redis promises to resolve before returning the object? This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 1 Answer 1 In your code, you are not waiting f...

Assign a dynamic function to Global Environment in R

Image
Assign a dynamic function to Global Environment in R This question will be a simple one for some members of this forum, but I am expending a lot of time searching for an answer and I did not find any working one. Let me clarify: I have a simple function that conducts a linear regression, and the results become a variable and this variable is assigned to the global environment. Straightforward method. However, every time I run this function, the variable is replaced, what I don't want. I just want to define a specific name instead of replacing this variable. Please check out this code to understand my question: set.seed(123) ds <- data.frame(income=rnorm(1000,700,20), sex=c(0,1), age=rnorm(1000,30,10)) regress <- function(iv_string) { regression_formula <- as.formula(paste("income ~", iv_string)) results <<- lm(regression_formula, ds) plot(results) print(results) } regress("age") regress("sex") Something like that regress <-...

In PHP, how can a function body reference variables in its dynamic parent?

In PHP, how can a function body reference variables in its dynamic parent? In PHP, one can write $a="$b$c"; This concatenates the assumed strings in $b and $c into $a elegantly (that is, using minimal yet clear syntax). Now I want to move this statement into a function. The problem is that $b and $c are naturally interpreted as being local to this function, not its caller (which can be the global code or a function). Even if the caller defines $b and $c, this new function cannot see those definitions. I want to do something clever that requires the statement to work the same (except for the location of $a) even though it has been moved into a function. Furthermore, I want this to be efficient. No use of extract() or debug_backtrace(). Just want to use the caller's local scope. I don't mind if I use Zend to get the caller's symbol table, or any other hack, so long as it's efficient. Don't worry, I'm not going to use this as general programming practice ...

R: how to use symbols as a FUN for apply?

R: how to use symbols as a FUN for apply? In R's documentation for apply, it says: FUN: the function to be applied: see ‘Details’. In the case of functions like +, %*%, etc., the function name must be backquoted or quoted. I don't understand the latter half the sentence. When I do matrix1 = matrix(rnorm(3*4), 3, 4) apply(matrix1, 1, "+") I get the transpose of the matrix And when I do apply(matrix, 1, "%*%") I get an error. I'm trying to get the row-wise sum and product of this matrix. Also, if that's not what the documentation is talking about, what do does + and %*% supposed to do when supplied as the FUN argument of apply? Note that the first apply returns a vector and in R that means a column vector. As for the multiply, you need two operands, like in apply(matrix1, 1, "%*%", t(matrix1)) . – Rui Barradas Jun 30 at 16:50 ...