I was wondering how to pass a number to a function I created for a loop. For example, I have a loop that simply adds 1 to a value when it runs. How would I go about passing how many times I want the loop to run in a function? Like so:
var i = 0;
function blahBlah (i ?){
for (i=0,i>10(this is what I want to pass to the function),i++){
i++;
}
Then call the function:
blahBlah(number of times I want it to run);
I'm not sure i understand the question, but how about
function blahBlah(n) {
for(var i=0; i < n; i++) {
//do something
}
}
function blahBlah (noOfTimes){
for (var i=0 ;i < noOfTimes ;i++){
//i++; you already incremented i in for loop
console.log(i);//alert(i);
}
}
blahBlah(10);// call function with a loop that will iterate 10 times
You mean calling the function each iteration?
function blahBlah( i ) {
// do something with i
}
for ( var i = 0; i < 10; i++ ) {
blahBlah( i );
}
Maybe like this:
function runLoop(length) {
for (var i=0; i < length; i++) {
{loop actions}
}
}
First, you used , instead of ; in for loop.
Second, you need two variables here: the first one is how many times to repeat (i, the argument), the second is a counter (a, which iteration is it now)
function blah(i) {
for (var a=0; a<i; a++) {
doStuff();
}
}
Use a loop inside your function:
function BlahBlah(n) {
for (i=0; i < n; ++i) {
// do something...
}
}
or simply invoke the function in a for loop:
function Blahblah() { /* do something */ }
// elsewhere:
n = 42;
for (i=0; i < n; ++i) BlahBlah();
Related
I have my array defined but at the moment i go into implement my bubble sorting function it doesn't enter the first for loop. It does run the function however
function bubbleSort (){
for (var j=0; j++; j < valores.length){
for (var i=j+1; i++; i < valores.length){
if (valores[j]>valores[i]){
var temp=0
temp=valores[j]
valores[i]=valores[j]
valores[j]=temp
}
}
}
console.log(valores)
}
so if valores input [2,1] I expect the output in console log to be [1,2].
I obtain my array by this function if that is of any help:
let valores =[];
let papelero=10;
function agregarValor (){
if (valores.length < papelero){
let val = Number(valor.value)
valores.push(val)
console.log(valores)
}
}
like #ug_ said my swap was incorrect, and he previously said j and i were incremented in the wrong places.
function bubbleSort() {
for (var j=0; j<valores.length; j++) {
for (var i=j+1; i<valores.length; i++) {
if (valores[j]>valores[i]) {
var temp=0
temp=valores[i]
valores[i]=valores[j]
valores[j]=temp
}
}
}
console.log(valores)
}
Wondering if there is by any chance to programmatically setting third statement of forloop
var conditionProgrammatically = 'i++';//or 'x--'
for (var i = 0; i < 10; conditionProgrammatically) {
console.log(i)
}
You can use any expression you want there including calling a function. You just need to be careful of scope. So, for example, this works:
var conditionProgramatically = () => i++ ;
for (var i = 0; i < 10; conditionProgramatically()) {
console.log(i)
}
But it depends on the fact that var i is in a scope shared by the function. This, however, doesn't work:
var conditionProgramatically = () => i++ ;
for (let i = 0; i < 10; conditionProgramatically()) {
console.log(i)
}
Because let is scoped to the block and not available.
Of course you can share an object which is mutable by passing it as an argument like:
fn = (o) => o.i += 1
for (let o = {i:0}; o.i < 10; fn(o)) {
console.log(o.i)
}
This allows you to use let, but is a little hard on the eyes.
All said, it's probably going to be easier to make your logic fit in a simple expression rather than calling a function. You can still perform some logic, though:
for (let i = 0; Math.abs(i) < 10; i = Math.random() > .65 ? i -1: i + 1) {
console.log(i)
}
You can set a variable and then operate with this variable according to your needs.
(remember that i-- is equivalent to i -= 1).
BTW, be careful because you would also have to change the condition, if not you will end up in an infinite loop. In your case, I would use abs()
var step = 1; // or var step = -1;
for (var i = 0; abs(i) < 10; i += step) {
console.log(i)
}
Usually, in functional programmings (like python and javascript), we can use dictionary (or objects) to store functions.
var myFunctions = {
"a": function (i) { return i + 1 },
"b": function (i) { return i - 3 }
};
Then, we can set the condition as the key to the dictionary:
myCondition = "a"; // this will set condition to increment by 1
Here is your for loop:
for (i = 0; i < n; i = myFunctions[myCondition](i)) {
// whatever
}
Here is the code
function fn(){
for (var i = 0; i < 4; i++) {
var tc=setTimeout(function(i){
console.log(i)
clearTimeout(tc)
},10,i);
}
}
fn()
//0, 1, 2 - output
I can't understand why the output is not '0, 1, 2, 3',only output three times.
Even more stranger is below
When I change the loop times, now, I call loop times as T.
T: 1 ---> 0
T: 2 ---> 0
T: 3 ---> 0, 1
above, the right is the output.
Here you go. Print i out at interval T.
function fn(){
var T = 1000;
var func = function(i,len) {
if ( i < len ) {
console.log(i);
setTimeout(func,T,i+1,len); // Calling itself.
}
};
setTimeout(func,T,0,4);
}
fn();
At last got it...The problem is with closure..You just need to pass the variable tc to setTimeout function.
function fn(){
for (var i = 0; i < 4; i++) {
var tc=setTimeout(function(i,tc){
console.log(i)
clearTimeout(tc);
},10,i,tc);
}
}
fn()
What you are doing is that you are overwriting the value of tc in the for loop.
By overwriting, you lose handle to corresponding return value from each call to setTimeout().
You need to clear individual tc as you go in the for loop.
So, something like this would work.
Note that I have hard-coded, tc array but you can make it generic, so this example is just for a reference as to how you could get 0,1,2,3.
var tc = [0,0,0,0];
function fn(){
for (var i = 0; i < 4; i++) {
tc[i]=setTimeout(function(i){
console.log(i);
clearTimeout(tc[i]);
},10,i);
}
}
fn();
If I have a funtion like this:
function xyz(b)
{
for(var i = 0; i < b.length; i++)
{
// do something with b items...
}
}
... wouldn't it be more memory-friendly if I were to assign b to a local variable inside of that function before working with its items?
function xyz(b)
{
var c = b;
for(var i = 0; i < c.length; i++)
{
// do something with c items...
}
}
In your example both b and c are local variables since they only exist in the function. So your code will actually be a bit less performant.
Side note - if you want your code to be more performant you should calculate c.length only once for the whole for loop. In your example you're calculating it for every iteration of the loop. Instead you can do as follows:
for (var i = 0, cLen = c.length; i < cLen; i++)
This way it calculates it only once before starting the loop.
How can I isolate my variable from variable in this function, if his creator forgot for var keyword?
for (var i = 0; i < 4; i++)
{
test();
}
function test()
{
i = 0;
}
Same idea than previous answer using scoping but a better way would be to use IIFE:
(function () {
for (var i = 0; i < 4; i++) {
test();
}
})();
http://jsfiddle.net/8vBc5/
put your for loop in a separated scope:
in a function.
function test(){
i = 0;
}
function trial(){
for (var i = 0; i < 4; i++){
test();
}
}
trial();
That way only the code and functions inside the trial function can access variables declared at that level.