Lists

This page is under construction. Please come back later.
#!/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]
#!/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
#!/usr/bin/env node;
const main = async () => {
let names = ["Fred", "Wilma", "Barney", "Betty"];
for (let name of names) {
console.log(["Hello, ", name, "!"].join(''));
}
names = ["Harry", "Ron", "Hermione"];
for (let name of names) {
console.log(["Hello, ", name, "!"].join(''));
}
}
main().catch( e => { console.error(e) } );
Output
$ node Lists3.js
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Harry!
Hello, Ron!
Hello, Hermione!
#!/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]].join(''));
}
}
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
#!/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]].join(''));
}
}
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
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Array Last Element
- Central Limit Theorem
- Chromatic Scale Frequencies
- Income Tax (Revisited)
- Morse Code
- Renard Numbers (Preferred Numbers)
- Sample Mean and Standard Deviation
- Sieve of Eratosthenes
References
- [[JavaScript Language Reference]], Mozilla Developer Network
- [[Mozilla Developer Network]]
- Download [[node.js]]
- [[w3schools.com]]
Pure Programmer


