Link Search Menu Expand Document

JavaScript

How to create an array containing 1…N

Array.from(Array(10).keys());
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Generate random number between two numbers in JavaScript

function randomIntFromInterval(min, max) {
  // min and max included
  return Math.floor(Math.random() * (max - min + 1) + min);
}

How do I save JSON to local text file

const fs = require("fs");

const data = [
  { name: "cliff", age: "34" },
  { name: "ted", age: "42" },
];

fs.writeFile("test.json", JSON.stringify(data, null, 2), function (err) {
  if (err) {
    console.log(err);
  }
});

Convert string to Title Case with JavaScript

function toTitleCase(str) {
  let upper = true;
  let newStr = "";
  for (let i = 0, l = str.length; i < l; i++) {
    if (str[i] === " ") {
      upper = true;
      newStr += str[i];
      continue;
    }
    newStr += upper ? str[i].toUpperCase() : str[i].toLowerCase();
    upper = false;
  }
  return newStr;
}