1

I'm just starting to learn JavaScript and I have to do a task:

Create function with local variables which turns each element of array [0,1,2,3,4,5] into a sqrt and sums them up.

I know how to do a function and then I have to do a loop - I have a problem with the next step and using Math.sqrt...

function myFunction() {  
   var numbers = ['0','1', '2', '3', '4', '5'];
   var total = 1;

   for (var i = 1; i < numbers.length; i++) {
        var result = Math.sqrt(numbers[i++]) * +numbers[i];
        console.log(result);
   }
}

myFunction();
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
asheri
  • 15
  • 1
  • 7

5 Answers5

4

This (ES2015 syntax) function will sum the square roots of an array of numbers:

let sumRoots = array => array.map(Math.sqrt).reduce((a, b) => a + b, 0);

The Array.prototype.map function is the standard way to transmogrify the elements of an array into a (new) modified array, and Array.prototype.reduce is used to collapse the resulting array into its sum.

If you can't handle ES2015 syntax, get a better browser, or use this:

function sumRoots(array) {
    return array.map(Math.sqrt)
                .reduce(function(a, b) {
                    return a + b;
                }, 0);
}
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Like my comment, but without handling the string numbers – TryingToImprove Dec 08 '16 at 16:05
  • 4
    Why not just `a.reduce((a, b) => a + Math.sqrt( b ) ), 0);`? It seems unesseccary to map when each element is visited during the reduce anyway. – Paul Dec 08 '16 at 16:05
  • 2
    @Paulpro The map followed by the reduce is more semantic. it better captures the nature of the problem. –  Dec 08 '16 at 16:06
  • 1
    @Paulpro yeah, I considered that, but I figured the two step process is easier to understand – Alnitak Dec 08 '16 at 16:06
  • @torazaburo how so? Paulpro's suggestion is more optimal without losing any readability. – Damon Dec 08 '16 at 16:08
  • 1
    The code as it stands correctly separates the concerns of (1) some computation involving each element and (2) adding up the results of those computations. If the problem were changed to add up squares, it would be trivial to change `map(Math.sqrt)` to `map(square)`. –  Dec 08 '16 at 16:10
  • @torazaburo right - this is the "functional programmer's" approach to the problem – Alnitak Dec 08 '16 at 16:11
  • @Alnitak thanks but using map may be a little to difficult to me ATM but I will keep studying :) – asheri Dec 08 '16 at 16:52
  • @asheri stick with it - learning the standard library functions will result in better code – Alnitak Dec 08 '16 at 17:20
1

Since the OP is in the early stages of learning how to use JavaScript I present a straight forward external iteration based method below. While the other answer presents a declarative approach, it seems important for the OP to get a handle on the basics first.

function sumSquareRoots(numbers) {  
   var total = 0;
   for (var i = 0; i < numbers.length; i++) {
        var sqrt = Math.sqrt(numbers[i]);
        total = total + sqrt;
   }
   return total;
}

var numbers = ['0','1', '2', '3', '4', '5'];
var result = sumSquareRoots(numbers);
console.log(result);
bhspencer
  • 13,086
  • 5
  • 35
  • 44
  • You almost certainly want to return the result, and print it from outside, rather than printing it inside. –  Dec 08 '16 at 16:05
  • The OP just logged the result to the console. No mention of returning the result. – bhspencer Dec 08 '16 at 16:06
  • @bhspencer thanks so much, I indeed needed something simple since I'm just starting. I understand now what I did wrong! – asheri Dec 08 '16 at 16:48
0
    function myFunction() {  
    var numbers = ['0','1', '2', '3', '4', '5'];
    var sum=0;
    for (var i = 0; i < numbers.length; i++) {
        sum += Math.sqrt(numbers[i]);
    }
    console.log(sum);
}

myFunction();

Is that you looking for?

  • yes, it's good! I quess I overlooked the part with "sum+= etc " :) Thanks! It's so simple hahaha – asheri Dec 08 '16 at 16:43
0
  var sum = ['0','1','2','3','4','5']
                                    .reduce((a,b)=>(a+Math.sqrt(b)),0);

For this you have to understand internally mechanism of reduce method . in this method the value of b is always array element . where

in each iteration a =return value.

Mahi
  • 1,707
  • 11
  • 22
0

We can simply map through and multiply each item by its self.

let numbers = ['0','1', '2', '3', '4', '5'];
let storeMultipliedIndex = []
numbers.map(item => storeMultipliedIndex.push(item * item))
console.log(storeMultipliedIndex)