-3

Can you implement a while loop inside a curried arrow function expression? I would like to be able to express a while loop as an expression.

var arr = [1,2,3,4,5,6];

var len = arr.length;

const iter = i => while(() => (console.log(i), ()=>i--);

 iter(len);
Rick
  • 1,035
  • 10
  • 18

3 Answers3

4

Nope. Arrow functions without a block must be expressions, while loops arent (theyre statements). You may do sth like

function whileExp(check,callback){
 while(typeof check=="function"?check():check){
   if(callback) callback();
 }
}

(counter=>whileExp(()=>--counter,()=>alert(counter)))(10);

However i dont know why this should be useful

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
2

Your are missing the name of the declaration of the array, and i don't know this syntax for the while loop, but this syntax below works fine

var arr = [1,2,3,4,5,6];

var len = arr.length;

const iter = i => {while(i > 0){console.log(i); i--;}};

 iter(len);
Patrick Sampaio
  • 390
  • 2
  • 13
1

You can use Array.prototype.reduceRight(), comma operator

const arr = [1,2,3,4,5,6];

const iter = (a, b) => (console.log(a), b);

arr.reduceRight(iter);
guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    What do you mean by "control over the number of iteration on the array."? The code at Question only calls `console.log()`. What is expected result? – guest271314 May 24 '17 at 18:09
  • @Arrow _"I only want to iterate only through half the array. I don't want to iterate beyond that."_ That requirement is not described at original Question . See https://stackoverflow.com/help/how-to-ask – guest271314 May 24 '17 at 18:14
  • @Arrow Suggest being specific and clear at text of Question itself what requirement is, instead of attempting to massage new requirements into Question at comments. Presently requirement is not clear. Use `break` to stop iteration of `for` or `while` loop. – guest271314 May 24 '17 at 18:17
  • @Arrow It is not a matter of "want" one way or the other. Rather, effort put forth to improve Question as to specifically what requirement is. The present Answer is interpretation of requirement at original Question. Why are you now considering deleting Question? – guest271314 May 24 '17 at 22:34
  • I changed the question. Let me know if the quality has been improved or if there are concerns you believe I have not addressed. I want to delete the question because the question has negative points. I have my answer, this question is not worth the loss of points. – Rick May 24 '17 at 22:55
  • @Arrow _"I have my answer, this question is not worth the loss of points."_ It can be, depending on perspective. That is, what if anything you have learned from the Question and answers. You can also accept your own Answer https://stackoverflow.com/help/self-answer – guest271314 May 24 '17 at 23:04
  • "It can be, depending on perspective. You can also accept your own Answer". I am not interested in StackOverflow martyrdom :-). Also if certain people like you who have a following, disapprove of the question and express it, others will follow and react irrationally. The system has its negatives complimented by its positives that are balanced from what I notice. I am here to learn not join a school of thought :) – Rick May 24 '17 at 23:17
  • @Arrow What is rational reason for "downvote"s at https://stackoverflow.com/q/43103717/? Some would obviously contend that there is rational reason for "downvote"s at https://stackoverflow.com/questions/44081901/combine-these-specific-javascript-functions/44081983#44081983 and https://stackoverflow.com/questions/43725419/converting-nucleotides-to-amino-acids-using-javascript/43725733#43725733. What are you going to do? Have cast no "downvote", here. Did cast vote for closure of current Question for lack of clarity at OP. Though you answered your own Question, resolving any shortcoming at OP – guest271314 May 24 '17 at 23:27
  • @Arrow You cannot undo what has already been done. You can accept your own Answer. If your reason for Question was to get the correct Answer, you have achieved the fulfillment of that reason yourself. If your reason for Question was "upvote" of Question, you cannot be certain what type of response your Question will receive, if any response at all. – guest271314 May 24 '17 at 23:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145072/discussion-between-arrow-and-guest271314). – Rick May 24 '17 at 23:34