Code Examples

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1.

REVERSE:

const a='i love you'

console.log(a.split(' ').reverse().join(' '));

2.FACTORIAL:

using recurssion:

function factorial(n) {

if (n===1) {

return 1;

return n*factorial(n-1)

console.log(factorial(5));

3.longest word

function long(str) {

let a=str.split(' ')

console.log(a);

let so=a.sort((a,b)=>b.length-a.length)

console.log(so);

return so[0]

console.log(long('i have 3.2 years of experience as ui developer'));

4.adding from 1 to n if n,n-1,n-2......

function add(n){

if(n===1){
return 1

return n+add(n-1)

log(add(9))

5.Alternative captilize

function a(str){

let c=str.split('')

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

c[i]=c[i].toUpperCase(); c[i]=c[i][0].toUpperCase()+c[i].slice(1); it is for captailzing each words


first letter

return c.join('')

console.log(a('bhargava ram'));

6.time convert minutes:seconds

function a(n) {

return Math.floor(n/60)+ ':' +Math.floor(n%60)

console.log(a(123)); //2hrs:3minutes

7.vowels count

const a='vowels vowels aeiou'

const b=a.match(/[aeiou]/g).length

console.log(b);

8.no of occurence of a letter in a string


function a(str,letter) {

let count=0;

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

if (str.charAt(i)===letter) {

count++

return count

console.log(a('baaargav','a'));

9.palindrome check
function a(str) {
let a=str.length

for (let i = 0; i < a/2; i++) {


if (str[i]===str[a-1-i]) {
console.log('palin');

} else{
console.log('not palin');

}
}
}
console.log(a('racecar'));

10.duplicates in array,string
let a=[1,2,3,1,2,3,1,2,3]
let bb='aaassshhh'
let b=bb.split('')
console.log(b.filter((item,position)=>b.indexOf(item)===position));
11.SECOND LARGEST NUMBER
function a(arr){
let second=-Infinity;
let first=-Infinity;
for (let i = 0; i < arr.length; i++) {
if (arr[i]>first ) {
second=first
first=arr[i]
} else if(arr[i]>second){
second=arr[i]
}
}
return second RETURN FIRST;
}
console.log(a([1,2,3,4,5,9]));

12.MISSING NUMBERS IN RANGE


function missing(arr){
let miss=[]
let max=Math.max(...arr);
let min=Math.min(...arr);
for (let i = min; i < max; i++) {
if (!arr.includes(i)) {
miss.push(i)
}
}
return miss
}
console.log(missing([1,2,3,9]));

13.no of occurrence of letter in array and large number occurrence


let a=[1,1,2,2,3,4,3,4,3,3,4,3]

let op={}
let g=a.forEach((i)=>{

op[i]=(op[i] || 0)+1

})

console.log(op); //{1: 2, 2: 2, 3: 5, 4: 3}

console.log(Object.values(op).reduce((a,b)=>op[a]>op[b]?a:b));

14.INTERSECTION
let a=[1,2,3,4]
let b=[1,2,3,4,5,6]
console.log(b.filter((item)=>a.indexOf(item)!==-1));//[1,2,3,4]
console.log(b.filter((item)=>a.indexOf(item)===-1));//[5,6]

15.removing negative numbers

const a=[1,-2,3]
console.log(a.filter(x=>x>-1));
16.duplicates in array/string

const a=[1,-2,3,1,1]
console.log(a.filter((index,pos)=>a.indexOf(index)===pos));
//for duplicates in string
const as='aaabbbccc'
const a=as.split('')
console.log(a.filter((index,pos)=>a.indexOf(index)===pos));

17.FIZZBUZZ
function fizbuzz(n){

let res=[]
for (let i = 1; i <= n; i++) {
if (i%3===0&&i%5===0) {
res.push('fizzbuzz')
} else if(i%3===0){
res.push('fizz')
} else if(1%5===0){
res.push('buzz')
} else{
res.push(i)
}
}
return res
}
console.log(fizbuzz(15));

18.TWO SUM
function tsum(arr,s){
let sum=[];
for (let i = 0; i < arr.length; i++) {
for (let j = i+1; j < arr.length; j++) {
if (arr[i]+arr[j]===s) {
sum.push([arr[i],arr[j]])
}
}
}
return sum
}
let twoSum=tsum([1,2,3,4,5],9)
console.log((twoSum)); //[5,4]

19.THREE SUM
function tsum(arr,s){
let sum=[];
for (let i = 0; i < arr.length; i++) {
for (let j = i+1; j < arr.length; j++) {
for (let k = j+1; k < arr.length; k++) {
if (arr[i]+arr[j]+arr[k]===s) {
sum.push([arr[i],arr[j],arr[k]])
}
}
}
return sum
}
}
console.log(tsum([1,2,3,4,5],10));
20.INVERT KEY,VALUE PAIRS
let a={a:'baargav',s:123}
let op=[]
or (const key in a) {
op[a[key]]=key
}
console.log(op);

let oo=Object.fromEntries(Object.entries(a).map(a=>a.reverse()))
console.log(oo);

21.reverse words in a sentence


function a(params) {
let a=params.split(' ').join(' ')
let b= a.split(' ').map(w=>w.split('').reverse().join(''))
return b.join(' ')
}
console.log(a('i love you'));

22.turn array into chunks


https://stackabuse.com/how-to-split-an-array-into-even-chunks-in-
javascript/
let a=['a','bbb','cccc',1,2,3,4];
function chunks(arr,chop) {
let res=[]
for (let i = 0; i < arr.length; i+=chop) {
const chunk=arr.slice(i,i+chop)
res.push(chunk)
}
return res
}
console.log(chunks(a,2));

22.difference b/n two dates


const a=new Date('1/1/2022')
const b=new Date('1/1/2023')
const diffsecs=Math.abs(b-a)
const days=(diffsecs/(1000*24*60*60))
console.log(days);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy