How to find the sum of an array of numbers

Recommended (reduce with default value) Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.  Run code snippetExpand snippet Without default value You get a TypeError  Run code snippetExpand snippet Prior to ES6’s arrow functions  Run code snippetExpand snippet Non-number inputs If non-numbers are … Read more

Setting “checked” for a checkbox with jQuery

Modern jQuery Use .prop(): DOM API If you’re working with just one element, you can always just access the underlying HTMLInputElement and modify its .checked property: The benefit to using the .prop() and .attr() methods instead of this is that they will operate on all matched elements. jQuery 1.5.x and below The .prop() method is not available, so you need to use .attr(). Note that this is the … Read more

data.map is not a function

Objects, {} in JavaScript do not have the method .map(). It’s only for Arrays, []. So in order for your code to work change data.map() to data.products.map() since products is an array which you can iterate upon.

speedtest.net api

speedtest.net is run by Ookla and their Speed Test application. Unfortunately they don’t provide any public APIs for speedtest.net which you could use. Although I doubt either of these meet your needs, they do provide Speed Test Mini and a hosted reporting solution for their full Speed Test software package (which includes CSV exporting capabilities). The reason you’re unable to … Read more

Adding attribute in jQuery

You can add attributes using attr like so: However, for DOM properties like checked, disabled and readonly, the proper way to do this (as of JQuery 1.6) is to use prop.