This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPrime(num) | |
{ | |
for (var i = 2; i<num ; i++) | |
{ | |
if(num%i == 0) | |
{ | |
return num+" is not a prime number"; | |
} | |
} | |
return num+" in a prime number"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if you want whole fibonacci series till n | |
let fibb = [0, 1]; | |
function fibonacciTillN(n) { | |
if (fibb[n - 1] != undefined) { | |
return fibb[n - 1]; | |
} else { | |
return (fibb[n - 1] = fibonacciTillN(n - 1) + fibonacciTillN(n - 2)); | |
} | |
} | |
fibonacciTillN(10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if you want the whole series of fibonacci | |
var fib = [0, 1]; | |
var n = 10; // you can change this value | |
if (n == 1 || n == 2) { | |
console.log(fib); | |
} | |
else { | |
for (var i = 2; i < n; i++) { | |
fib.push(fib[i-1]+fib[i-2]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = [1,2,5,3,7,5,6,8,9,9,0,3,4,5,6,5,5,4,3,3,4,4]; | |
function duplicateRemover(numbers) | |
{ | |
var uniqueArray = [] | |
for(var i = 0; i< numbers.length ; i++) | |
{ | |
if(uniqueArray.indexOf(numbers[i]) == -1) | |
{ | |
uniqueArray.push(numbers[i]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// how to swap two variable | |
//*************** * way -1**************** | |
var a = 5; | |
var b = 7; | |
console.log("brfore swap a = ",a,"and b =",b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var words = "amake amar moto thakte dao. ami nijeke nijer moto ghuchiye niyechi"; | |
var counter=0; | |
for (var i = 0; i< words.length ; i++) | |
{ | |
if(words[i] == " " && words[i-1] != " " && i != 0 && i != words.length-1) | |
{ | |
counter++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using a loop | |
var str = "i am a javascript programmer"; | |
var reverseString = "" | |
for(var i = str.length ; i > 0; i--) | |
{ | |
reverseString += str[i-1]; | |
} | |
console.log(reverseString); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// how to create random numbers | |
// random numbers | |
var rand = Math.random(); //will create random number between 0 - 1 | |
console.log(rand); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let numbers = [1,2,3,4,5,6,7,8,9,10]; | |
let result1 = numbers.map( (x) => x*10 ); | |
console.log(result1); | |
let result2 = numbers.filter( (x) => x%2 ); //returns all values that meet the condition | |
console.log(result2); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// genarate random number with specific digit using function | |
function randNumber(n){ | |
return Math.floor((Math.random()*10**n)); //if you want n digit random number as a number | |
} | |
function randNumString(n){ | |
return (Math.random()*10**n+" ").split('.')[0]; //if you want n digit random number as a string | |
} | |
OlderNewer