Thursday, March 12, 2015

Javascript random number between two numbers

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

console.log(getRandomInt(1,100));

Javascript display numbers with commas

function commaSeparated(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }
    return val;
}

// Call Function
console.log(commaSeparated(1)); // 1
console.log(commaSeparated(10)); // 10
console.log(commaSeparated(100)); // 100
console.log(commaSeparated(1000)); // 1,000
console.log(commaSeparated(10000)); //10,000