0% found this document useful (0 votes)
1 views4 pages

Js Coding

Uploaded by

piyush padhy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Js Coding

Uploaded by

piyush padhy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Find Unquearr all method

By loop
let arr=[1,2,3,4,4,5,9];

function removeDuplicate(arr){
let uniqueArr=[];
for(i=0;i<[Link];i++){
if([Link](i) === -1){
[Link](i);
}
}
return uniqueArr
}
[Link](removeDuplicate(arr));

By filter

let arr=[1,2,3,4,4,5,9];

function removeDuplicate(arr){
return [Link]((val,index)=>[Link](val)===index)
}
[Link](removeDuplicate(arr));

with Reduce

let arr=[1,2,3,4,4,5,9];

function removeDuplicate(arr){
return [Link]((uniqueVal,curr)=>{
if([Link](curr)===-1){ //if(![Link](curr)){
[Link](curr);
}
return uniqueVal
},[])
}
[Link](removeDuplicate(arr));
Find a unique array of object

function uniqueObj(arr){
let uniqueObjects=[];
let seenObjects= new Set();
for (const obj of arr) {
// We use the object reference to check uniqueness
if (![Link]([Link])) {
[Link]([Link]);
[Link](obj);
}
}
return uniqueObjects
}

const arrayOfObjects = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 1, name: 'Alice' }, // Same object as the first one
{ id: 3, name: 'Charlie' },
{ id: 2, name: 'Bob' }, // Same object as the second one
];

[Link](uniqueObj(arrayOfObjects));
// [{ id: 1, name: 'Alice' },{ id: 2, name: 'Bob' },{ id: 3, name: 'Charlie' }]

2. Write a code to explain Async and Await

async function fetchData(){


try{
let response = await fetch('[Link]
let data = [Link]();
return data
}
catch(err){
[Link]([Link])
}
}
fetchData().then(data=>[Link](data))
3. let arr = [3, 5, 7, 9, 11, 13, 19]; let target = 33; Output: [[3, 11, 19]]
function findCombination(arr, target) {
const result = [];

function backtrack(startIndex, currentCombination, currentSum) {


if (currentSum === target) {
[Link]([...currentCombination]);
return; // Found a valid combination
}
if (currentSum > target) {
return; // Exceeded the target
}

for (let i = startIndex; i < [Link]; i++) {


[Link](arr[i]); // Choose the current number
backtrack(i + 1, currentCombination, currentSum + arr[i]); // Recur with the next index
[Link](); // Backtrack, remove the last number
}
}

backtrack(0, [], 0);


return result;
}

Find the output

let arr = [3, 5, 7, 9, 11, 13, 19];


let target = 33;

let combinations = findCombination(arr, target);


[Link](combinations); // Output: [[3, 11, 19]]

[Link](!!null);//false
[Link](typeof NaN);//number
[Link](1 < 2 < 3);//true
[Link](1 + '1' - 1)//10

function noParams(a, b, c) {}
[Link]([Link]);//3
const a = [1, 2, 3];
a[10] = 11;

[Link] a unique substring from a string

[Link] number in descending order

let a = {
name:'test',
surname:'test1',
details:{
empid:123,
company:'test'
}
}
Copy this to another object deep copy

[Link] occurance of a number array

[Link] an array of only even placed number

You might also like