jmrker
12-01-2012, 03:54 AM
I'm trying to understand the properties of the map() function.
So far I've figured how to do some simple actions.
<html>
<head>
<title>JavaScript Array MAP Methods</title>
<style type="text/css">
body { font-family:monospace; }
</style>
</head>
<body>
<script type="text/javascript">
// Ideas from: http://www.codingforums.com/showthread.php?t=276006
function createSequence(n) { return Object.keys(Object(Array(n)+"r")).map(Number); }
function sqr(n) { return n * n; }
function incrementSequence(n) { n = n + 1; return n; }
function limitPrecision(n) { return n.toFixed(3); }
function scaleFactor(n,sf) { return (n * sf); }
var numbers = [1, 4, 9, 16, 25, 36, 49];
var roots = numbers.map(Math.sqrt);
document.write('Sequence is: '+numbers+'<br> roots are : ' + roots );
numbers = createSequence(10);
var squares = numbers.map(sqr);
document.write('<p>Sequence is : '+numbers+"<br>squares are : " + squares );
var NUMBERS = numbers.map(incrementSequence);
document.write('<p> Sequence is : '+numbers+"<br>increments are : " + NUMBERS );
roots = NUMBERS.map(Math.sqrt);
document.write('<p>Sequence is: '+NUMBERS);
var Roots = roots.map(limitPrecision);
document.write('<br> roots are : ' + Roots.join(', ') );
</script>
</body>
</html>
So my question becomes, can I pass more than one parameter to the map()?
See the function below. While I get no syntax errors, it does not work when I try to scale each element in the array by a factor of 0.5
numbers = createSequence(10);
var scaled = numbers.map(scaleFactor,0.5);
document.write('<p>Sequence is : '+numbers+"<br> scaled are : " + scaled );
Is the secret to creating a scaled array to define another map() function passing only the scale factor?
Are multiple parameters not allowed in the map() function?
So far I've figured how to do some simple actions.
<html>
<head>
<title>JavaScript Array MAP Methods</title>
<style type="text/css">
body { font-family:monospace; }
</style>
</head>
<body>
<script type="text/javascript">
// Ideas from: http://www.codingforums.com/showthread.php?t=276006
function createSequence(n) { return Object.keys(Object(Array(n)+"r")).map(Number); }
function sqr(n) { return n * n; }
function incrementSequence(n) { n = n + 1; return n; }
function limitPrecision(n) { return n.toFixed(3); }
function scaleFactor(n,sf) { return (n * sf); }
var numbers = [1, 4, 9, 16, 25, 36, 49];
var roots = numbers.map(Math.sqrt);
document.write('Sequence is: '+numbers+'<br> roots are : ' + roots );
numbers = createSequence(10);
var squares = numbers.map(sqr);
document.write('<p>Sequence is : '+numbers+"<br>squares are : " + squares );
var NUMBERS = numbers.map(incrementSequence);
document.write('<p> Sequence is : '+numbers+"<br>increments are : " + NUMBERS );
roots = NUMBERS.map(Math.sqrt);
document.write('<p>Sequence is: '+NUMBERS);
var Roots = roots.map(limitPrecision);
document.write('<br> roots are : ' + Roots.join(', ') );
</script>
</body>
</html>
So my question becomes, can I pass more than one parameter to the map()?
See the function below. While I get no syntax errors, it does not work when I try to scale each element in the array by a factor of 0.5
numbers = createSequence(10);
var scaled = numbers.map(scaleFactor,0.5);
document.write('<p>Sequence is : '+numbers+"<br> scaled are : " + scaled );
Is the secret to creating a scaled array to define another map() function passing only the scale factor?
Are multiple parameters not allowed in the map() function?