Skip to content

Instantly share code, notes, and snippets.

@SK-CSE
Last active January 28, 2017 10:25
Show Gist options
  • Save SK-CSE/a6a4078ed6f3b56a254393140232091a to your computer and use it in GitHub Desktop.
Save SK-CSE/a6a4078ed6f3b56a254393140232091a to your computer and use it in GitHub Desktop.
basic logics
// factorial
function factorial(number) {
var product = 1;
for (var i = 1; i <= number; i++) {
product *= i;
}
return product;
}
// factorial recursive
function factorial(number) {
if (number == 1) {
return number;
}
else {
return number * factorial(number-1);
}
}
// min element index in Array
function minElementIndex(intArr){
var index=0;
temp =intArr[0];
for(var i=0;i<intArr.length;i++){
if(intArr[i]< temp){
temp=intArr[i];
index = i;
}
}
return index;
}
// find all divisor of a number
function getDivisors(n){
var a1=[];
for(var i= 1; i<=n;i++){
if(n%i===0){
a1.push(i);
}
}
return a1;
}
// adding element in the 1st position of array
var nums = [2,3,4,5];
var newnum = 1;
var N = nums.length;
for (var i = N; i >= 0; --i) {
nums[i] = nums[i-1];
}
nums[0] = newnum;
console.log(nums); // 1,2,3,4,5
// delete 1st position element of array
var nums = [9,1,2,3,4,5];
console.log(nums);
for (var i = 0; i < nums.length; ++i) {
nums[i] = nums[i+1];
}
console.log(nums); // 1,2,3,4,5,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment