How to rewrite this function as a pure function?
I have this function
function sumUpToNum(num) {
let sum = 0;
for(let z=0;z<num;z++) {
sum+=z;
}
return sum;
}
How to rewrite as a pure function? What i have tried:
function sumUpToNum(num) {
let sum = 0;
while(num) {
sum+=num;
}
return sum;
}
See also questions close to this topic

Change datasource in javascript based on dropdown click?
I have an HTML page that looks like this. It includes a dropdown and a
div
used to display a map:<head> <meta name="viewport" content="width=devicewidth, initialscale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="dropdown"> <button class="btn btnprimary dropdowntoggle" type="button" datatoggle="dropdown">Month<span class="caret"></span></button> <ul class="dropdownmenu"> <li><a href="#">11012017</a></li> <li><a href="#">12012017</a></li> </ul> </div> <div> <div id="MapDiv" ></div> </div> </div>
MapDiv
is filled using the following javascript function. It's a WCF that returns ajson
stream. It's hardcoaded to 11012017:var portsMap = AmCharts.makeChart( "MapDiv", { "type": "map", "theme": "light", "colorSteps": 5, "dataLoader": { "url": "http://OurServer/Service1.svc/GetInfo/11012017" ...(OTHER CODE)... } );
My question is: how can I change
dataloader
so that it loads based on my dropdown selection? So if I select 12012017, then the data displayed will be from date.Thanks.

Remove jquery dependency on simple text rotation
At the moment I am loading a whole jquery library for something that is really rather simple. It relates through a list of span's which each contain different text, so that the message is different each time.
Here is the code that I have:
(function($){ $.fn.extend({ rotaterator: function(options) { var defaults = { fadeSpeed: 500, pauseSpeed: 500, child:null }; var options = $.extend(defaults, options); return this.each(function() { var o =options; var obj = $(this); var items = $(obj.children(), obj); items.each(function() {$(this).hide();}) if(!o.child){var next = $(obj).children(':first'); }else{var next = o.child; } $(next).fadeIn(o.fadeSpeed, function() { $(next).delay(o.pauseSpeed).fadeOut(o.fadeSpeed, function() { var next = $(this).next(); if (next.length == 0){ next = $(obj).children(':first'); } $(obj).rotaterator({child : next, fadeSpeed : o.fadeSpeed, pauseSpeed : o.pauseSpeed}); }) }); }); } }); })(jQuery); $(document).ready(function() { $('.rotate').rotaterator({fadeSpeed:500, pauseSpeed:6000}); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h2 class="light contentmedium center textcenter softtopquad"> Join us for a <div class="rotate"> <span>relaxed</span> <span>wonderful</span> <span>crazy</span> </div> weekend of <div class="rotate"> <span>fun</span> <span>games</span> <span>laughter</span> <span>dancing</span> <span>love</span> </div> resulting in your <div class="rotate"> <span>best</span> <span>worst</span> <span>most disgusting</span> </div> <div class="rotate"> <span>memories</span> <span>hangover</span> </div> </h2>
I would also appreciate any tips on how to best start with this, as I am rather new to javascript.

Unstick a fixed element based on page scroll
I have a calculator on my site (written in php), and in the end of this calculator there is a div with the results.
On desktop it works fine, but on mobile, i want to fix the results div to the bottom until scrolling the original position of the div. After that i want to change its position to static.
In other words, there is a fixed (to bottom) element on the page and after scrolling, at the original position of the div, i want to unstick it.
I hope, its understandable, but there is an example page  Look at the MOBILE version!!!
How can i do that?
Thanks in advance!

JS Curry function with Recursion
Kindly read before you mark it as duplicate.
Im not asking for single curry call.
This functions multiplies, multiplication(4,4,4) //64
function multiplication(...args) { return args.reduce((accum, val) => accum * val, 1) }
But Im trying to achieve something else...
This same function should multiply its curry function parenthesis as well. e.g.
/* which return the multiplication of three numbers. The function can be called in any of the following forms: multiply(2, 3)(4) => 24 multiply(2)(3, 4) => 24 multiply(2)(3)(4) => 24 multiply(2, 3, 4) => 24 */
Kindly help.
After fiddling through a lot of code and reading some stack answers. Finally I came up with. But it still doesnt satisfy this
multiply(2)(3, 4) => 24
But works fine for rest of the cases
multiply(2,3,4) multiply(2,3)(4) multiply(2)(3)(4) var multiply = function(...args) { if (args.length === 3) { return args[0] * args[1] * args[2]; } else { return function() { args.push([].slice.call(arguments).pop()); return multiply.apply(this, args); }; } }
while
multiply(2)(3, 4) => 24 fail

understanding merge heap applying tree fold for heap sort
Please help me in understanding the below code. I understood how
map heapify
works with output
map heapify [34,25,28] [Node 34 [],Node 25 [],Node 28 []]
Now how shall i understand this expression step by step.
merge_heaps = treefold merge_heap Nil
tried this by executing merge_heap individually.
merge_heap Nil . map heapify [34,25,28]
error
<interactive>:13:18: error: * Couldn't match expected type `a > Heap a1' with actual type `[Heap Integer]'
How the left folded tree structure is being interpreted to make max heap. struck. How shall i proceed further to make sense step by step.
How to map my understanding of heap sort in imperative languages something of sort from wikipedia in functional sense. How the unbalanced tree fold structure is being heap sorted.
 treefold (*) z [a,b,c,d,e,f] = (((a*b)*(c*d))*(e*f)) treefold f zero [] = zero treefold f zero [x] = x treefold f zero (a:b:l) = treefold f zero (f a b : pairfold l) where pairfold (x:y:rest) = f x y : pairfold rest pairfold l = l  here l will have fewer than 2 elements data Heap a = Nil  Node a [Heap a] deriving Show heapify x = Node x [] heapsort :: Ord a => [a] > [a] heapsort = flatten_heap . merge_heaps . map heapify where merge_heaps :: Ord a => [Heap a] > Heap a merge_heaps = treefold merge_heap Nil flatten_heap Nil = [] flatten_heap (Node x heaps) = x:flatten_heap (merge_heaps heaps) merge_heap :: Ord a => [] merge_heap heap Nil = heap merge_heap node_a@(Node a heaps_a) node_b@(Node b heaps_b)  a < b = Node a (node_b: heaps_a)  otherwise = Node b (node_a: heaps_b)

How to perform weighted average smoothing on an array in functional programming
I am new to functional programming and I am stumped by the following problem.
Given an array of numbers I am trying to compute the following smoothing.
For 1 < I < length(Array): (1/4)*(Array[I1] + (3/4)*Array[I] + (1/4)*Array[I+1]
I am having trouble coming up with a good strategy to come up with a solution in Erlang, since I need to somehow keep track of where I am in the list.
Any direction would be very helpful.

Is print Haskell a pure function?
Is print in Haskell a pure function; why or why not? I'm thinking it's not because it does not always return the same value as pure functions should.

Is a function that calls Math.random() pure?
Is the following a pure function?
function test(min,max) { return Math.random() * (max  min) + min; }
My understanding is that a pure function follows these conditions:
 It returns a value computed from the parameters
 It doesn't do any work other than calculating the return value
If this definition is correct, is my function a pure function? Or is my understanding of what defines a pure function incorrect?

Will this be considered a pure reducer function in redux?
I have this reducer function and I am incrementing the value of voteScore by 1 inside it. Is this the right way to do it without breaking the constraint that reducer function should be pure function?
function comment (state = {}, action) { ... switch(action.type) { ... case UP_VOTE_COMMENT: const upVoteScore = parseInt(state[id]['voteScore'], 10) + 1 return { ...state, [id]: { ...state[id], voteScore: upVoteScore } } } }