Pure Programmer
Blue Matrix


Cluster Map

Lists

L1

This page is under construction. Please come back later.

Lists1.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
	const NUM_SQUARES = 5;
	let squares = [];
// Put the squares into the list
	for (let i = 0; i < NUM_SQUARES; ++i) {
		squares.push(i * i);
	}
// Print out the squares from the list
	for (let i = 0; i < squares.length; ++i) {
		console.log(Utils.format("{0:d}^2 = {1:d}", i, squares[i]));
	}

	console.log(Utils.listToString(squares));
}
main().catch( e => { console.error(e) } );
Output
$ node Lists1.js 0^2 = 0 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 [0, 1, 4, 9, 16]
Lists2.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
	let squares = [0, 1, 4, 9, 16, 25];

// Print out the squares from the list
	for (let i = 0; i < squares.length; ++i) {
		console.log(Utils.format("{0:d}^2 = {1:d}", i, squares[i]));
	}
}
main().catch( e => { console.error(e) } );
Output
$ node Lists2.js 0^2 = 0 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25
Lists3.js
#!/usr/bin/env node;
const main = async () => {
	let names = ["Fred", "Wilma", "Barney", "Betty"];

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

	names = ["Harry", "Ron", "Hermione"];
	for (let name of names) {
		console.log("Hello, " + name + "!");
	}
}
main().catch( e => { console.error(e) } );
Output
$ node Lists3.js Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty! Hello, Harry! Hello, Ron! Hello, Hermione!
Lists4.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
// Print out the command line arguments
	for (let i = 1; i <= (process.argv.length - 2); ++i) {
		console.log(i + ":" + process.argv[i + 1]);
	}
}
main().catch( e => { console.error(e) } );
Output
$ node Lists4.js Fred Barney Wilma Betty 1:Fred 2:Barney 3:Wilma 4:Betty $ node Lists4.js 10 20 30 40 1:10 2:20 3:30 4:40
Lists5.js
#!/usr/bin/env node;
const Utils = require('./Utils');

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

// Print out the name based on command line argument 1-4
	for (let i = 1; i <= (process.argv.length - 2); ++i) {
		const x = Utils.stoiWithDefault(process.argv[i + 1], 0);
		console.log(i + ":" + names[x - 1]);
	}
}
main().catch( e => { console.error(e) } );
Output
$ node Lists5.js 1 2 3 4 1:Fred 2:Wilma 3:Barney 4:Betty $ node Lists5.js 4 3 2 1 1:Betty 2:Barney 3:Wilma 4:Fred $ node Lists5.js 1 3 1:Fred 2:Barney

Questions

Projects

More ★'s indicate higher difficulty level.

References