最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to turn each element of array into a square root and sum them up - Stack Overflow

programmeradmin3浏览0评论

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();

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();
Share Improve this question edited Dec 8, 2016 at 16:06 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Dec 8, 2016 at 16:00 asheriasheri 151 silver badge7 bronze badges 8
  • First you may want to consider making the elements of the numbers array numbers, rather than strings. – Heretic Monkey Commented Dec 8, 2016 at 16:02
  • @MikeMcCaughan he did actually allow for that, but at the moment the loop isn't actually summing anything – Alnitak Commented Dec 8, 2016 at 16:03
  • numbers.map(function(strNumber) { return Math.sqrt(parseInt(strNumber)) }) .reduce(function(prev, num) { return prev + num }, 0) – TryingToImprove Commented Dec 8, 2016 at 16:03
  • @Alnitak I realize that, but what's the point of making them strings, only to convert them to numbers again? – Heretic Monkey Commented Dec 8, 2016 at 16:04
  • @MikeMcCaughan indeed - it would be better if the numbers were actually numbers in the first place – Alnitak Commented Dec 8, 2016 at 16:07
 |  Show 3 more ments

5 Answers 5

Reset to default 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);
}

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);
    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?

  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.

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)
发布评论

评论列表(0)

  1. 暂无评论