Pure Programmer

# Functions

Functions1.js
```#!/usr/bin/env node;
function printHello() {
console.log("Hello, world!");
}

const main = async () => {
printHello();
}
main().catch( e => { console.error(e) } );
```
Output
\$ node Functions1.js Hello, world!
Functions2.js
```#!/usr/bin/env node;
function printHello(name) {
console.log("Hello, " + name + "!");
}

const main = async () => {
printHello("Fred");
printHello("Wilma");
printHello("Barney");
printHello("Betty");
}
main().catch( e => { console.error(e) } );
```
Output
\$ node Functions2.js Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty!
Functions3.js
```#!/usr/bin/env node;
const Utils = require('./Utils');

function squared(j) {
return j * j;
}

const main = async () => {
for (let i = 1; i <= 10; ++i) {
console.log(Utils.format("{0:d}^2 = {1:d}", i, squared(i)));
}
}
main().catch( e => { console.error(e) } );
```
Output
\$ node Functions3.js 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25 6^2 = 36 7^2 = 49 8^2 = 64 9^2 = 81 10^2 = 100
Functions4.js
```#!/usr/bin/env node;
const Utils = require('./Utils');

function exponentiation(base, powerArg) {
let power = powerArg;
let result = 1;
while (power > 0) {
result *= base;
--power;
}
return result;
}

const main = async () => {
for (let b = 1.1; b < 2.05; b += 0.1) {
for (let p = 2; p <= 10; ++p) {
console.log(Utils.format("{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p)));
}
}
}
main().catch( e => { console.error(e) } );
```
Output
\$ node Functions4.js 1.1 ^ 2 = 1.2100000000000002 1.1 ^ 3 = 1.3310000000000004 1.1 ^ 4 = 1.4641000000000006 1.1 ^ 5 = 1.6105100000000008 1.1 ^ 6 = 1.771561000000001 1.1 ^ 7 = 1.9487171000000014 1.1 ^ 8 = 2.1435888100000016 1.1 ^ 9 = 2.357947691000002 1.1 ^ 10 = 2.5937424601000023 1.2000000000000002 ^ 2 = 1.4400000000000004 1.2000000000000002 ^ 3 = 1.7280000000000006 1.2000000000000002 ^ 4 = 2.073600000000001 1.2000000000000002 ^ 5 = 2.4883200000000016 1.2000000000000002 ^ 6 = 2.9859840000000024 1.2000000000000002 ^ 7 = 3.5831808000000036 1.2000000000000002 ^ 8 = 4.299816960000005 1.2000000000000002 ^ 9 = 5.1597803520000065 1.2000000000000002 ^ 10 = 6.191736422400009 1.3000000000000003 ^ 2 = 1.6900000000000006 1.3000000000000003 ^ 3 = 2.1970000000000014 1.3000000000000003 ^ 4 = 2.8561000000000023 1.3000000000000003 ^ 5 = 3.7129300000000036 1.3000000000000003 ^ 6 = 4.826809000000006 1.3000000000000003 ^ 7 = 6.274851700000009 1.3000000000000003 ^ 8 = 8.157307210000013 1.3000000000000003 ^ 9 = 10.604499373000019 1.3000000000000003 ^ 10 = 13.785849184900027 1.4000000000000004 ^ 2 = 1.960000000000001 1.4000000000000004 ^ 3 = 2.744000000000002 1.4000000000000004 ^ 4 = 3.8416000000000037 1.4000000000000004 ^ 5 = 5.378240000000006 1.4000000000000004 ^ 6 = 7.529536000000011 1.4000000000000004 ^ 7 = 10.541350400000018 1.4000000000000004 ^ 8 = 14.75789056000003 1.4000000000000004 ^ 9 = 20.661046784000046 1.4000000000000004 ^ 10 = 28.925465497600072 1.5000000000000004 ^ 2 = 2.2500000000000013 1.5000000000000004 ^ 3 = 3.375000000000003 1.5000000000000004 ^ 4 = 5.062500000000006 1.5000000000000004 ^ 5 = 7.5937500000000115 1.5000000000000004 ^ 6 = 11.390625000000021 1.5000000000000004 ^ 7 = 17.085937500000036 1.5000000000000004 ^ 8 = 25.62890625000006 1.5000000000000004 ^ 9 = 38.4433593750001 1.5000000000000004 ^ 10 = 57.66503906250016 1.6000000000000005 ^ 2 = 2.560000000000002 1.6000000000000005 ^ 3 = 4.0960000000000045 1.6000000000000005 ^ 4 = 6.553600000000009 1.6000000000000005 ^ 5 = 10.485760000000019 1.6000000000000005 ^ 6 = 16.777216000000035 1.6000000000000005 ^ 7 = 26.843545600000066 1.6000000000000005 ^ 8 = 42.94967296000012 1.6000000000000005 ^ 9 = 68.71947673600022 1.6000000000000005 ^ 10 = 109.95116277760039 1.7000000000000006 ^ 2 = 2.890000000000002 1.7000000000000006 ^ 3 = 4.913000000000005 1.7000000000000006 ^ 4 = 8.35210000000001 1.7000000000000006 ^ 5 = 14.198570000000023 1.7000000000000006 ^ 6 = 24.13756900000005 1.7000000000000006 ^ 7 = 41.0338673000001 1.7000000000000006 ^ 8 = 69.75757441000019 1.7000000000000006 ^ 9 = 118.58787649700037 1.7000000000000006 ^ 10 = 201.5993900449007 1.8000000000000007 ^ 2 = 3.2400000000000024 1.8000000000000007 ^ 3 = 5.832000000000007 1.8000000000000007 ^ 4 = 10.497600000000016 1.8000000000000007 ^ 5 = 18.895680000000038 1.8000000000000007 ^ 6 = 34.01222400000008 1.8000000000000007 ^ 7 = 61.222003200000174 1.8000000000000007 ^ 8 = 110.19960576000035 1.8000000000000007 ^ 9 = 198.3592903680007 1.8000000000000007 ^ 10 = 357.0467226624014 1.9000000000000008 ^ 2 = 3.610000000000003 1.9000000000000008 ^ 3 = 6.859000000000009 1.9000000000000008 ^ 4 = 13.032100000000023 1.9000000000000008 ^ 5 = 24.760990000000053 1.9000000000000008 ^ 6 = 47.04588100000012 1.9000000000000008 ^ 7 = 89.38717390000026 1.9000000000000008 ^ 8 = 169.83563041000056 1.9000000000000008 ^ 9 = 322.6876977790012 1.9000000000000008 ^ 10 = 613.1066257801026 2.000000000000001 ^ 2 = 4.0000000000000036 2.000000000000001 ^ 3 = 8.00000000000001 2.000000000000001 ^ 4 = 16.00000000000003 2.000000000000001 ^ 5 = 32.00000000000007 2.000000000000001 ^ 6 = 64.00000000000017 2.000000000000001 ^ 7 = 128.0000000000004 2.000000000000001 ^ 8 = 256.0000000000009 2.000000000000001 ^ 9 = 512.000000000002 2.000000000000001 ^ 10 = 1024.0000000000045
Functions5.js
```#!/usr/bin/env node;
const Utils = require('./Utils');

function factorial(x) {
if (x <= 1) {
return 1;
}
return x * factorial(x - 1);
}

const main = async () => {
for (let x = 1; x < 10; ++x) {
console.log(Utils.format("{0:d}! = {1:d}", x, factorial(x)));
}
}
main().catch( e => { console.error(e) } );
```
Output
Lists6.js
```#!/usr/bin/env node;
const Utils = require('./Utils');

function reverseList(x) {
let y = [];
for (let i = x.length - 1; i >= 0; --i) {
y.push(x[i]);
}
return y;
}

const main = async () => {
let names = ["Fred", "Wilma", "Barney", "Betty"];

for (let name of names) {
console.log("Hello, " + name + "!");
}

names = reverseList(names);
for (let name of names) {
console.log("Hello, " + name + "!");
}

console.log(Utils.listToString(names));
}
main().catch( e => { console.error(e) } );
```
Output
\$ node Lists6.js Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty! Hello, Betty! Hello, Barney! Hello, Wilma! Hello, Fred! ["Betty", "Barney", "Wilma", "Fred"]
Maps3.js
```#!/usr/bin/env node;
const Utils = require('./Utils');

function convertKMtoMiles(x) {
let y = new Map();
for (let planet of x.keys()) {
y.set(planet, Math.trunc(x.get(planet) * 0.621371 + 0.5));
}
return y;
}

const main = async () => {
let planetDiametersInKM = new Map([
["Mercury", 4879],
["Venus", 12103],
["Earth", 12756],
["Mars", 6794],
["Jupiter", 142985],
["Saturn", 120534],
["Uranus", 51115],
["Neptune", 49534],
["Pluto", 2374],
["Ceres", 946],
["Eris", 2326],
["Makemake", 1430]
]);

let planetDiametersInMiles = convertKMtoMiles(planetDiametersInKM);
for (let planet of planetDiametersInMiles.keys()) {
console.log(planet + " has a diameter of " + planetDiametersInMiles.get(planet) + " miles");
}
}
main().catch( e => { console.error(e) } );
```
Output
\$ node Maps3.js Mercury has a diameter of 3032 miles Venus has a diameter of 7520 miles Earth has a diameter of 7926 miles Mars has a diameter of 4222 miles Jupiter has a diameter of 88847 miles Saturn has a diameter of 74896 miles Uranus has a diameter of 31761 miles Neptune has a diameter of 30779 miles Pluto has a diameter of 1475 miles Ceres has a diameter of 588 miles Eris has a diameter of 1445 miles Makemake has a diameter of 889 miles
Tuples3.js
```#!/usr/bin/env node;
const Utils = require('./Utils');

function swap(x) {
let y;
y = [x[1], x[0]];
return y;
}

const main = async () => {
let pair1 = ["Hello", 5];
let pair2;
pair2 = swap(pair1);
console.log(Utils.tupleToString(pair1) + " becomes " + Utils.tupleToString(pair2));
}
main().catch( e => { console.error(e) } );
```
Output
\$ node Tuples3.js <"Hello", 5> becomes <5, "Hello">
javascript

## Questions

• {{Who's on first?}}
• {{Who's on second?}}
• {{Who's on third?}}

## Projects

More ★'s indicate higher difficulty level.

## References

• [[JavaScript Language Reference]], Mozilla Developer Network
• [[Mozilla Developer Network]]