[nodejs] read and write a file

https://nodejs.dev/learn/reading-files-with-nodejs

https://nodejs.dev/learn/writing-files-with-nodejs

        const fs = require("fs")

        try {
            // read from local folder
            const localPDF = fs.readFileSync('PDFs/myFile.pdf')

            //write back to local folder
            fs.writeFileSync('PDFs/writtenBack.pdf', localPDF )

        } catch (err) {
            console.error(err)
        }

Converting to Base64

        try {
            // read from local folder
            const localPDF = fs.readFileSync('PDFs/myFile.pdf')
            const localBase64 = localPDF.toString('base64')

            //write back to local folder
            fs.writeFileSync(`PDFs/writtenBack.pdf`, localBase64, {encoding: 'base64'})

        } catch (err) {
            console.error(err)
        }

Reading and writing using streams with pipe

        //read and write local file
        var reader = fs.createReadStream("PDFs/myFile.pdf")
        var writer = fs.createWriteStream('PDFs/writtenBack.pdf');
        reader.pipe(writer)

[ABAP] Convert JavaScript Timestamp to yyyy-MM-dd’T’HH:mm:ss

DATA(lv_js_timestamp) = "/Date(1615161600000)/".

"Extract /Date(1615161600000)/ to 1615161600000
FIND REGEX '([0-9]+)' IN lv_js_timestamp IGNORING CASE SUBMATCHES DATA(js_timestamp).

cl_pco_utility=>convert_java_timestamp_to_abap( EXPORTING iv_timestamp = js_timestamp
                                                IMPORTING ev_date      = DATA(lv_date)
                                                          ev_time      = DATA(lv_time) ).
"2021-03-08T00:00:00
CONVERT DATE lv_date TIME lv_time INTO TIME STAMP DATA(timestamp) TIME ZONE 'UTC'.
rv_datetime = |{ timestamp TIMESTAMP = ISO }|.

[nodejs] APIs and Microservices Projects

These are my notes while doing the course APIs and Microservices on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.


Timestamp Microservice

https://repl.it/@nocin/boilerplate-project-timestamp#server.js

app.get("/api/timestamp/", (req, res) => {
  res.json({ unix: Date.now(), utc: Date() });
});

app.get("/api/timestamp/:date?", (req, res) => {

  //utc date?
  let date = new Date(req.params.date)
  if (date != "Invalid Date") {
   res.json({unix: date.getTime(), utc: date.toUTCString()});
  }

  //unix timestamp?
  const dateInt = parseInt(req.params.date);
  date = new Date(dateInt).toUTCString();
  if (date != "Invalid Date") {
    res.json({unix: dateInt, utc: date});
  }
  
  //invalid input
  res.json({ error: date });
});


Request Header Parser Microservice

https://repl.it/@nocin/boilerplate-project-headerparser#server.js

https://www.npmjs.com/package/express-useragent
https://www.npmjs.com/package/express-request-language

var useragent = require('express-useragent');
var cookieParser = require('cookie-parser');
var requestLanguage = require('express-request-language');

// stuff...

app.use(useragent.express());
app.use(cookieParser());
app.use(requestLanguage({
  languages: ['en-US', 'zh-CN'],
  cookie: {
    name: 'language',
    options: { maxAge: 24*3600*1000 },
    url: '/languages/{language}'
  }
}));

app.get("/api/whoami", (req, res) => {
  res.json({"ipaddress": req.ip,
            "language": req.language,
            "software": req.useragent.source });
});


URL Shortener Microservice

https://repl.it/@nocin/boilerplate-project-urlshortener#server.js

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const dns = require('dns');


// Basic Configuration
const port = process.env.PORT || 3000;

app.use(cors());

app.use('/public', express.static(`${process.cwd()}/public`));

app.get('/', function(req, res) {
  res.sendFile(process.cwd() + '/views/index.html');
});

app.use(bodyParser.urlencoded({extended: false}));

let urls = [];

//POST
app.post("/api/shorturl/new", function(req, res) {
  
  const getHostnameFromRegex = (url) => {
  // run against regex
  const matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
  // extract hostname (will be null if no match is found)
  return matches && matches[1];
  }

  hostname = getHostnameFromRegex(req.body.url);
  console.log("Hostname: " + hostname);

  // if no hostname found, return here
  if (!hostname) res.json({ error: 'invalid url' });

  // check if url is valid
  dns.lookup(hostname, (error, addresses) => {
    console.error(error);
    console.log(addresses);

    if (!error) {
       let newUrl = { original_url : req.body.url, short_url : urls.length + 1};
      urls.push(newUrl);
      res.json(newUrl);
    } else {
      res.json({ error: 'invalid url' });
    }

  });

});

//GET
app.get('/api/shorturl/:num', function(req, res) {

  for (let i = 0; i < urls.length; i++) {
    console.log(urls[i].original_url);
    if (urls[i].short_url == req.params.num) {
        res.redirect(urls[i].original_url);
    }
  }

});

app.listen(port, function() {
  console.log(`Listening on port ${port}`);
});


Exercise Tracker

https://repl.it/@nocin/boilerplate-project-exercisetracker#server.js

const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/views/index.html')
});


const listener = app.listen(process.env.PORT || 3000, () => {
    console.log('Your app is listening on port ' + listener.address().port)
})


//BodyParser
app.use(bodyParser.urlencoded({ extended: false }));

//DB connect
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

const { Schema } = mongoose;

//User Schema
const userSchema = new Schema({
    username: { type: String, required: true },
});
const User = mongoose.model("User", userSchema);

//Exercise Schema
const exerciseSchema = new Schema({
    userId: Schema.Types.ObjectId,
    description: { type: String, required: true },
    duration: { type: Number, required: true },
    date: { type: Date, default: Date.now }
});
const Exercise = mongoose.model("Exercise", exerciseSchema);


//POST user to DB
app.post("/api/exercise/new-user", (req, res) => {

    let user = new User({ username: req.body.username });

    user.save((err, data) => {
        //console.log("created User: " + data);
        if (err) return console.error(err);
        res.json({ username: data.username, _id: data._id });
    });

});


//GET all users from DB
app.get("/api/exercise/users", (req, res) => {
    User.find((err, usersFound) => {
        if (err) return console.error(err);
        //console.error("users found: " + usersFound);
        res.json(usersFound);
    })
});


//POST exercise form data
app.post("/api/exercise/add", (req, res) => {

    let exercise = new Exercise({
        userId: req.body.userId,
        description: req.body.description,
        duration: req.body.duration,
        date: req.body.date ? req.body.date : Date.now()
    });

    exercise.save((err, data) => {
        //console.log("created exercise: " + data);
        if (err) return console.error(err);
        User.findById(exercise.userId, (err, userFound) => {
            if (err) return console.error(err);
            //console.log("userFound " + userFound.username); 
            res.json({
                _id: data.userId,
                username: userFound.username,
                date: data.date.toDateString(),
                duration: data.duration,
                description: data.description
            });
        });
    });
});


//GET exercise log
app.get("/api/exercise/log", (req, res) => {
    console.log(req.query.userId);
    console.log(req.query.from);
    console.log(req.query.to);
    console.log(req.query.limit);

    let userId = req.query.userId;
    let limit = Number(req.query.limit);

    //create query filter
    let filter = {};
    filter.userId = userId;

    if (req.query.from && req.query.to) {
        let fromDate = new Date(req.query.from);
        let toDate = new Date(req.query.to);
        filter.date = { $gte: fromDate, $lte: toDate };
    }

    console.log("Filter " + JSON.stringify(filter));

    const queryExercises = (done) => {
        Exercise.find(filter)
            .limit(limit)
            .exec((err, exercices) => {
                if (err) return console.error(err);
                done(exercices);
            })
    };

    const paseExercises = (exercices) => {
        let logArray = [];

        for (let i = 0; i < exercices.length; i++) {
            var obj = exercices[i];
            logArray.push({
                description: obj.description,
                duration: obj.duration,
                date: obj.date.toDateString()
            });
        }
        console.log(logArray);

        User.findById(userId, (err, userFound) => {
            if (err) return console.error(err);
            let logger = {
                _id: userId,
                username: userFound.username,
                count: logArray.length,
                log: logArray
            };
            res.json(logger);
        });
    }

    //Execute Query
    queryExercises(paseExercises);

});

File Metadata Microservice

https://repl.it/@nocin/boilerplate-project-filemetadata#server.js

https://www.npmjs.com/package/multer

var express = require('express');
var cors = require('cors');
require('dotenv').config()
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' });

var app = express();

app.use(cors());
app.use('/public', express.static(process.cwd() + '/public'));

app.get('/', function (req, res) {
    res.sendFile(process.cwd() + '/views/index.html');
});


const port = process.env.PORT || 3000;
app.listen(port, function () {
  console.log('Your app is listening on port ' + port)
});


//POST 
app.post('/api/fileanalyse', upload.single('upfile'), (req, res, next) => {
  res.json({ name: req.file.originalname, type: req.file.mimetype, size: req.file.size  });
})

[nodejs] MongoDB and Mongoose Challenges

These are my notes while doing the course APIs and Microservices on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.


MongoDB is a database that stores data records (documents) for use by an application. Mongo is a non-relational, “NoSQL” database. This means Mongo stores all associated data within one record, instead of storing it across many preset tables as in a SQL database.
Mongo’s use of JSON as its document storage structure makes it a logical choice when learning backend JavaScript. Accessing documents and their properties is like accessing objects in JavaScript.

Mongoose.js is an npm module for Node.js that allows you to write objects for Mongo as you would in JavaScript.

MongoDB Atlas is a MongoDB Database-as-a-Service platform.


Install and Set Up Mongoose

Add mongodb and mongoose to the project’s package.json.

    "dependencies": {
        "body-parser": "^1.15.2",
        "dotenv": "^8.2.0",
        "express": "^4.12.4",
        "mongodb": "^3.6.4",
        "mongoose": "^5.11.15"
    },

Store your MongoDB Atlas database URI in a private .env file as MONGO_URI. Replace user and password.

MONGO_URI=mongodb+srv://<User>:<Password>@cluster0.xvsqx.mongodb.net/<dbname>?retryWrites=true&w=majority

Connect to the database using the following syntax:

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

Create a Model

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

const { Schema } = mongoose;

const personSchema = new Schema({
  name: { type: String, required: true },
  age: Number,
  favoriteFoods: [String]
});

const Person = mongoose.model("Person", personSchema);

Create and Save a Record of a Model

The done() function is a callback that tells us that we can proceed after completing an asynchronous operation such as inserting, searching, updating, or deleting. It’s following the Node convention, and should be called as done(null, data) on success, or done(err) on error.

const createAndSavePerson = (done) => {

  let max = new Person({name: "Max", age: 31, favoriteFoods: ["Pasta"]});

  max.save((err, data) => {
    if (err) return console.error(err);
    done(null, data)
  });

};

Create Many Records with model.create()

var arrayOfPeople = [
    {name: "Max", age: 31, favoriteFoods: ["Pasta"]},
    {name: "Toni", age: 21, favoriteFoods: ["Pizza"]},
    {name: "Paul", age: 34, favoriteFoods: ["Bolo", "Penne"]}
    ];

const createManyPeople = (arrayOfPeople, done) => {
  Person.create(arrayOfPeople, (err, people) => {
    if (err) return console.error(err);
    done(null, people);
  });
};

Use model.find() to Search Your Database

Model.find() accepts a query document (a JSON object) as the first argument, then a callback. It returns an array of matches.

const findPeopleByName = (personName, done) => {
  Person.find({name: personName}, (err, personFound) => {
    if (err) return console.error(err);
    done(null, personFound);
  })
};

Use model.findOne() to Return a Single Matching Document from Your Database

Model.findOne() behaves like Model.find(), but it returns only one document (not an array), even if there are multiple items.

const findOneByFood = (food, done) => {
  Person.findOne({favoriteFoods: food}, (err, personFound) => {
    if (err) return console.error(err);
    done(null, personFound);
  })
};

Use model.findById() to Search Your Database By _id

When saving a document, MongoDB automatically adds the field _id, and set it to a unique alphanumeric key.

const findPersonById = (personId, done) => {
  Person.findById({_id: personId}, (err, personFound) => {
    if (err) return console.error(err);
    done(null, personFound);
  })
};

Perform Classic Updates by Running Find, Edit, then Save

const findEditThenSave = (personId, done) => {
  const foodToAdd = 'hamburger';

  Person.findById(personId, (err, person) => {
    if(err) return console.log(err); 
  
    person.favoriteFoods.push(foodToAdd);
    person.save((err, updatedPerson) => {
      if(err) return console.log(err);
      done(null, updatedPerson)
    })
  })
};

Perform New Updates on a Document Using model.findOneAndUpdate()

Use the function parameter personName as the search key. Set the person’s age to 20.
Note: You should return the updated document. To do that, you need to pass the options document { new: true } as the 3rd argument to findOneAndUpdate(). By default, these methods return the unmodified object.

const findAndUpdate = (personName, done) => {
  const ageToSet = 20;

  Person.findOneAndUpdate({name: personName}, {age: ageToSet}, { new: true }, (err, updatedDoc) => {
    if (err) return console.error(err);
    done(null, updatedDoc)
  })
};

Delete One Document Using model.findByIdAndRemove()

const removeById = (personId, done) => {

  Person.findByIdAndRemove(personId, (err, personDeleted) => {
    if (err) return console.error(err);
    done(null, personDeleted)
  })
};

Delete Many Documents with model.remove()

const removeManyPeople = (done) => {
  const nameToRemove = "Mary";

  Person.remove({name: nameToRemove}, (err, personsDeleted) => {
    if (err) return console.error(err);
    done(null, personsDeleted)
  })
};

Chain Search Query Helpers to Narrow Search Results

const queryChain = (done) => {
  const foodToSearch = "burrito";

  Person.find({favoriteFoods: foodToSearch})
  .sort({name: 1}) //sort bei name
  .limit(2) //only 2 results
  .select({age: 0})  //hide age
  .exec((err, twoPersonFound) => {
    if (err) return console.error(err);
    done(null, twoPersonFound)
  })
};

[nodejs] Basic Node and Express

These are my notes while doing the course APIs and Microservices on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.


Node.js is a JavaScript runtime that allows developers to write backend (server-side) programs in JavaScript. Node.js comes with a handful of built-in modules (small, independent programs) that help facilitate this purpose. Some of the core modules include:

  • HTTP: a module that acts as a server
  • File System: a module that reads and modifies files
  • Path: a module for working with directory and file paths
  • Assertion Testing: a module that checks code against prescribed constraints

Express (not included by default) runs between the server created by Node.js and the frontend pages of a web application. Also handles the app’s routing.

https://github.com/freeCodeCamp/boilerplate-express/


Start a Working Express Server

Let’s serve our first string! In Express, routes takes the following structure: app.METHOD(PATH, HANDLER). METHOD is an http method in lowercase. PATH is a relative path on the server. HANDLER is a function that Express calls when the route is matched.

app.get("/", (req, res) => {
  res.send("Hello Express");
});

Serve an HTML File

You can respond to requests with a file using the res.sendFile(path) method. This method needs an absolute file path. Use the Node global variable __dirname to calculate the path.
Send the /views/index.html file as a response to GET requests to the / path.

let absolutePath = __dirname + "/views/index.html";

app.get("/", (req, res) => {
  res.sendFile(absolutePath);
});

Serve Static Assets

Using express middleware to serve static files (stylesheets, scripts, images): app.use(path, middlewareFunction)

let staticFilesPath = __dirname + "/public";

app.use("/", express.static(staticFilesPath));

Serve JSON on a Specific Route

A REST (REpresentational State Transfer) API allows data exchange in a simple way, without the need for clients to know any detail about the server.

let dataJson = {"message": "Hello json"};

app.get("/json", (req, res) => {
  res.json(dataJson);
});

Use the .env File

The .env file is a hidden shell file that is used to pass environment variables to your application. Accessible from the app as process.env.VAR_NAME. Add variables to .env with this syntax: VAR_NAME=value

let dataJson = {"message": "Hello json"};

app.get("/json", (req, res) => {
 if (process.env.MESSAGE_STYLE === "uppercase") {
   dataJson.message = dataJson.message.toUpperCase();
 };

 res.json(dataJson);
});

Implement a Root-Level Request Logger Middleware

For every request, it should log to the console a string taking the following format: method path - ip. An example would look like this: GET /json - ::ffff:127.0.0.1.

app.use(function middleware(req, res, next) {
  console.log(req.method + " " + req.path + " - " + req.ip);
  next();
});

Chain Middleware to Create a Time Server

app.get('/now', 
(req, res, next) => {
  req.time = new Date().toString();
  next();
},  
(req, res) => {
  res.json({time: req.time})
});

Get Route Parameter Input from the Client

route_path: ‘/:word/echo’
actual_request_URL: ‘/myString/echo’
req.params: {word: ‘myString’}

app.get("/:word/echo", (req, res) => {
  res.json({"echo": req.params.word});
});

Get Query Parameter Input from the Client

route_path: ‘/name’
actual_request_URL: ‘/name?first=firstname&last=lastname’
req.query: {first: ‘firstname’, last: ‘lastname’}

app.get("/name", (req, res) => {
  res.json({name: `${req.query.first} ${req.query.last}`})
});

Use body-parser to Parse POST Requests

POST is the default method used to send client data with HTML forms. In REST convention, POST is used to send data to create new items in the database (a new user, or a new blog post).
In these kind of requests, the data doesn’t appear in the URL, it is hidden in the request body. The body is a part of the HTTP request, also called the payload.
Add "body-parser": "^1.15.2", in your package.json as dependencie to parse the body data.

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));

Get Data from POST Requests

Mount a POST handler at the path /name.

app.post("/name", function(req, res) {
  res.json({name: `${req.body.first} ${req.body.last}`})
});

[nodejs] Managing Packages with NPM

These are my notes while doing the course APIs and Microservices on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.


The Node Package Manager (npm) is a command-line tool to share and control modules (or packages) of JavaScript code written for use with Node.js.

When starting a new project, npm generates a package.json file. This file lists the package dependencies for your project. Since npm packages are regularly updated, the package.json file allows you to set specific version numbers for each dependency. This ensures that updates to a package don’t break your project.

npm saves packages in a folder named node_modules. These packages can be installed in two ways:

  1. globally in a root node_modules folder, accessible by all projects.
  2. locally within a project’s own node_modules folder, accessible only to that project.

The package.json file is the center of any Node.js project or npm package. It stores information about your project, similar to how the <head> section of an HTML document describes the content of a webpage. It consists of a single JSON object where information is stored in key-value pairs.

https://github.com/freeCodeCamp/boilerplate-npm/

{
  "name": "fcc-learn-npm-package-json", // your project name
  "author": "Max Mustermann",
  "description": "A project that does something awesome",
  "keywords": [ "descriptive", "related", "words", "freecodecamp" ],
  "license": "MIT", // inform users of what they are allowed to do with your project
  "version": "1.2.0", // describes the current version of your project
  "dependencies": {
    "package-name": "MAJOR.MINOR.PATCH", // Semantic Versioning
    "express": "^4.14.0", 
    "moment": "~2.10.2" // handy library for working with time and dates.
  }
}

PATCHes are bug fixes and MINORs add new features but neither of them break what worked before. Finally, MAJORs add changes that won’t work with earlier versions.

To allow an npm dependency to update to the latest PATCH version, you can prefix the dependency’s version with the tilde (~) character.
The caret (^) will allow both MINOR updates and PATCHes.

[JavaScript] Algorithms and Data Structures Projects

These are my notes while doing the course JavaScript Algorithms and Data Structures on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.


1. Palindrome Checker

function palindrome(str) {

  // clear string
  str = str.toLowerCase().replace(/[^a-z0-9]/g, "");

  // reverse string
  let strBack = str.split("").reverse().join("");

  // palindrome?
  return (str === strBack) ? true : false;

}

palindrome("_eye");
palindrome("five|\_/|four");
palindrome("1 eye for of 1 eye.");

2. Roman Numeral Converter

function convertToRoman(num) {

  const lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1};
  let roman = '';

  for ( let i in lookup ) {
    while ( num >= lookup[i] ) {
      roman += i;
      num -= lookup[i];
    }
  }
  return roman;
}

convertToRoman(6);

3. Caesars Cipher

function rot13(str) {

  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const length = alphabet.length;
  let decode = "";

  const getROT13 = index => {
    return (index+13 >= length) ? alphabet[13-(length-index)] : alphabet[index+13];
  }

  for (let i=0; i<str.length; i++) {

    let index = alphabet.indexOf(str[i]);

    decode += (index<0) ? str[i] : getROT13(index); 

  }

  return decode;
}

rot13("SERR PBQR PNZC")
rot13("SERR CVMMN!")

4. Telephone Number Validator

function telephoneCheck(str) {

  let regexCheck = /^(1.*){0,1}(\d{3}|[(]\d{3}[)])[\s|-]*\d{3}.*\d{4}$/g; 

  let result = regexCheck.test(str);

  if (result) {
    let length = str.match(/\d/g).length;
    result = (length == 7 || length == 10 || (length == 11 && str[0] == 1)) ? true : false;
  }

  console.log(str.match(regexCheck));
  console.log(result);

  return result;

}


telephoneCheck("555-555-5555");
telephoneCheck("1 555-555-5555");
telephoneCheck("1 (555) 555-5555");
telephoneCheck("5555555555");
telephoneCheck("555-555-5555")
telephoneCheck("(555)555-5555")
telephoneCheck("1(555)555-5555")
telephoneCheck("1 555)555-5555") //false
telephoneCheck("(6054756961)")  //false
telephoneCheck("2 (757) 622-7382") //false
telephoneCheck("10 (757) 622-7382") //false

5. Cash Register

function checkCashRegister(price, cash, cid) {

  let changeArr = [];

  const currency = { "ONE HUNDRED": 100, "TWENTY": 20, "TEN": 10, "FIVE": 5, "ONE": 1, "QUARTER": 0.25, "DIME": 0.1, "NICKEL": 0.05, "PENNY": 0.01 }

  const substract = (a, b) => parseFloat(a - b).toPrecision(4);

  const calcAmount = (change, curr) => {
                let rest = change % curr;
                let div = substract(change, rest) / curr;    
                return div * curr;
  }

  let change = substract(cash, price);

  //check if cid equals to change
  //or if cid is already not enough
  let sum = 0;
  for (let k=0; k<cid.length;k++) {
    sum += cid[k][1]
  }
  if (sum == change) {
    return {status: "CLOSED", change: cid};
  } else if (sum < change) {
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }

  //calculate change
  for (let i in currency) {
    if (change > currency[i]) {    
      for (let j=0; j<cid.length;j++) {
        if (i == cid[j][0]) {
          let amount = cid[j][1];
          if (substract(change, amount) < 0)  {
            amount = calcAmount(change, currency[i]);
          }
          change = substract(change, amount);
          changeArr.push([i, amount]);
        }
      }
    } 
  }

  if (change != 0) {
    //not able to give the correct change
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }
  return {status: "OPEN", change: changeArr};
}

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

[JavaScript] Intermediate Algorithm Scripting

These are my notes while doing the course JavaScript Algorithms and Data Structures on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.


1. Sum All Numbers in a Range

function sumAll(arr) {

  let num1 = Math.min(arr[0], arr[1]);
  let num2 = Math.max(arr[0], arr[1]);
  let result = 0;

  while (num1 < num2) {
      result += num1;
      num1++;
  }
  return result += num2;
}

sumAll([1, 4]);

2. Diff Two Arrays

function diffArray(arr1, arr2) {

  var newArr = [];

  for (let i = 0; i < arr1.length; i++) {
    if (arr2.indexOf(arr1[i]) == -1) {
      newArr.push(arr1[i]);
    }
  }

  for (let i = 0; i < arr2.length; i++) {
    if (arr1.indexOf(arr2[i]) == -1) {
      newArr.push(arr2[i]);
    }
  }

  return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

3. Seek and Destroy

function destroyer(...arr) {

  let newArr = arr.shift();

  for (let i = 0; i < arr.length; i++) {
    while (newArr.indexOf(arr[i]) > -1) {
      newArr.splice(newArr.indexOf(arr[i]), 1);
    };
  }

  return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

4. Wherefore art thou

function whatIsInAName(collection, source) {

  var arr = [];

  var srcKeys = Object.keys(source);

  for (let i = 0; i < collection.length; i++) {
    let obj = collection[i];
    let bool = true;

    for (let j = 0; j < srcKeys.length; j++) {
      let key = srcKeys[j];
      if (!obj.hasOwnProperty(key) || (obj[key] != source[key])) {
        bool = false;
      }
    }

    if (bool) {
      arr.push(obj);
    }
  }

  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", //last: "Capulet" }], { last: "Capulet" });

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 });

5. Spinal Tap Case

function spinalCase(str) {

  let myRegex = /[A-Z][a-z]+|[a-z]+/g;
  let arr = str.match(myRegex);

  return arr.join("-").toLowerCase();
}

spinalCase('This Is Spinal Tap');
spinalCase("thisIsSpinalTap");
spinalCase("The_Andy_Griffith_Show");
spinalCase("Teletubbies say Eh-oh");
spinalCase("AllThe-small Things");

6. Pig Latin

function translatePigLatin(str) {

  let regexAY = /(^[^aeiou]+)/g;
  let regexWAY = /(^[aeiou]+)/g; 
  
  if (regexAY.test(str)) {
    let cutStr = str.match(regexAY);
    let newStr = str.substr(cutStr[0].length);
    return newStr + cutStr[0] + "ay";

  } else if (regexWAY.test(str)) {
    return str + "way";
  }

}

translatePigLatin("consonant");
translatePigLatin("paragraphs");
translatePigLatin("glove");
translatePigLatin("algorithm");
translatePigLatin("eight");
translatePigLatin("rhythm");

7. Search and Replace

function myReplace(str, before, after) {

  if (/^[A-Z]/.test(before)) {
    after = after.charAt(0).toUpperCase() + after.slice(1);
  } else {
    after = after.charAt(0).toLowerCase() + after.slice(1);
  }

  return str.replace(before, after);

}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
myReplace("Let us go to the store", "store", "mall");
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");

8. DNA Pairing

function pairElement(str) {

  let arr = [];

  for (let i = 0; i < str.length; i++) {

    let tmp = [];
    tmp.push(str[i]);
    
    switch(str[i]) {
      case "A":
        tmp.push("T");
        break;
      case "T":
        tmp.push("A");
        break;
      case "C":
        tmp.push("G");
        break;
      case "G":
        tmp.push("C");
    }
  
    arr.push(tmp);
  }
  
  return arr;
}

pairElement("GCG");

9. Missing letters

function fearNotLetter(str) {

  const alphabet = "abcdefghijklmnopqrstuvwxyz";

  let startIndex = alphabet.indexOf(str[0]);

  const compareStr = alphabet.slice(startIndex, startIndex + str.length);

  for (let i = 0; i < compareStr.length; i++) {
    if (compareStr[i] !== str[i]) {
        return compareStr[i];
    }
  }

  return undefined;
}

fearNotLetter("abce");
fearNotLetter("bcdf");
fearNotLetter("stvwx");

10. Sorted Union

function uniteUnique(...arr) {

  const uniteArray = [].concat(...arr);

  let uniteUniqueArray = [];

  for (let i = 0; i < uniteArray.length; i++) {
    if (uniteUniqueArray.indexOf(uniteArray[i]) == -1) uniteUniqueArray.push(uniteArray[i]);
  }

  return uniteUniqueArray;
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

11. Convert HTML Entities

function convertHTML(str) {

  return str
  .replace(/&/g, "&")
  .replace(/</g, "<")
  .replace(/>/g, ">")
  .replace(/"/g, """)
  .replace(/'/g, "'")
  .replace(/<>/g, "<>");

}

convertHTML("Dolce & Gabbana");
convertHTML("Hamburgers < Pizza < Tacos");
convertHTML("Sixty > twelve");
convertHTML('Stuff in "quotation marks"');
convertHTML("Schindler's List");
convertHTML("<>");
convertHTML("abc");

12. Sum All Odd Fibonacci Numbers

function sumFibs(num) {

  let fibo = [1, 1];

  const nextNum = fibu => fibo[fibo.length - 1] + fibo[fibo.length - 2];

  while (nextNum(fibo) <= num) {
    fibo.push(nextNum(fibo));
  };

  return fibo
  .filter(currentNum => currentNum%2 != 0) //odd
  .reduce((sum, currentNum) => sum + currentNum, 0); 

}

sumFibs(1);
sumFibs(4);
sumFibs(1000)
sumFibs(4000000);

13. Sum All Primes

function sumPrimes(num) {

  let primArr = [];

  const isPrime = num => { for(let i = 2; i < num; i++)
                            if(num % i === 0) return false;
                           return num > 1;
                          }

  for (let i = 0; i <= num; i++) {
    if(isPrime(i)) primArr.push(i);
  }

  return primArr.reduce((sum, currentNum) => sum + currentNum, 0);
}

sumPrimes(10);
sumPrimes(977);

14. Smallest Common Multiple

function smallestCommons(arr) {

  let rangeArr = [...arr];

  for (let i = Math.min(...arr) + 1; i < Math.max(...arr); i++) {
    rangeArr.push(i);
  }

  rangeArr.sort((a, b) => b-a);

  let lar = rangeArr[0];
  let scm = lar;
  let bool = false;

  while(!bool){
    
    bool = true;

    for (let i = 0; i < rangeArr.length; i++) {
      if (scm % rangeArr[i] != 0) bool = false;
    }
    
    if (bool) return scm;
    
    scm += lar;
  }

}


smallestCommons([1,5]);

15. Drop it

function dropElements(arr, func) {

  let copy = [...arr];

  for (let i = 0; i < arr.length; i++) {

    if (func(arr[i])) {
      break;
    }

    copy.shift();
    
  }

  return copy;
}

dropElements([1, 2, 3], function(n) {return n < 3; });

16. Steamroller (Recursion)

function steamrollArray(arr) {
 
  var flatArr = [];

  const flatten = arr => {
    for (let i = 0; i < arr.length; i++){
      (Array.isArray(arr[i])) ? flatten(arr[i]) : flatArr.push(arr[i]);
    }
    return flatArr;
  }
  
  return flatten(arr);
}

console.log(steamrollArray([1, [2], [3, [[4]]]]));

17. Binary Agents

function binaryAgent(binary) {

  return binary.split(' ') //Split string in array of binary chars
   .map(bin => String.fromCharCode(parseInt(bin, 2))) //Map every binary char to real char
   .join(''); //Join the array back to a string

}

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

18. Everything Be True

function truthCheck(collection, pre) {
  
  return collection.every(col => col.hasOwnProperty(pre) && Boolean(col[pre]));

}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

19. Arguments Optionalf (was really tough… this is the sample solution)

function addTogether(first, second) {
  if (typeof first !== "number") {
    return undefined;
  }
  const sum = second =>
    typeof second === "number" ? first + second : undefined;
  return typeof second === "undefined" ? second => sum(second) : sum(second);
}

addTogether(2,3);
addTogether("http://bit.ly/IqT6zt");
addTogether(2, "3");
console.log(addTogether(2)([3]));

20. Make a Person

var Person = function(firstAndLast) {

  var _arr = firstAndLast.split(" ");

  // getter
  this.getFirstName = () => _arr[0];
  this.getLastName = () => _arr[1];
  this.getFullName = () => _arr.join(" ");

  // setter
  this.setFirstName = first => _arr[0] = first;
  this.setLastName = last => _arr[1] = last;
  this.setFullName = firstAndLast => _arr = firstAndLast.split(" ");

};

var bob = new Person('Bob Ross');
bob.getFullName();

21. Map the Debris

function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  
  for(let i in arr){
    arr[i].orbitalPeriod = Math.round((2*Math.PI)*Math.sqrt(Math.pow((earthRadius+arr[i].avgAlt),3)/GM));
    delete arr[i].avgAlt;
  }
  return arr;
}

orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}])