// 1.Find the Largest Element in an Array // [3, 1, 4, 1, 5, 9] → 9 // Given an array of numbers, write a function to return the largest number. // function largnumber (arr){ // let max =arr[0]; // let min =arr[0]; // for(i=0;i<arr.length;i++){ // if(arr[i]>max){ // max =arr[i] // } // if(arr[i]<min){ // min=arr[i] // } // } // console.log("largest element in array: " + max) // console.log("smallest element in array: " + min) // } // let arr =[1,2,3,4,5,68,9] // largnumber(arr) // 2.Find the Second Largest Element // [3, 1, 4, 1, 5, 9] → 5 // Write a function to return the second largest number in an array. // function secondmax (arr){ // let max =Math.max(...arr); // let secondmaxnumber = 0; // let min =Math.min(...arr); // let secondminnumber = Infinity; // for(i=0;i<arr.length;i++){ // if(arr[i] < max && arr[i]>secondmaxnumber){ // secondmaxnumber =arr[i] // } // if(arr[i]>min && arr[i]<secondminnumber){ // secondminnumber =arr[i] // } // } // console.log("largest element in array: " + max) // console.log("secondmax element in array: " + secondmaxnumber) // console.log("smallest element in array: " + min) // console.log("secondmin element in array: " + secondminnumber) // } // let arr=[1,2,5,63,8,22,9] // secondmax(arr) // 3.Sum of All Elements // [1, 2, 3, 4] → 10 // Write a function that returns the sum of all elements in an array. // function sum (arr){ // let total=0 // for(i=0;i<arr.length;i++){ // total += arr[i] // } // console.log(total) // } // let arr =[1,2,3,4,5] // sum(arr) // 4.Remove Duplicates from an Array // [1, 2, 2, 3, 4, 4, 5] → [1, 2, 3, 4, 5] // Write a function to remove duplicate values from an array. // function duplicate(arr){ // let dup=[] // for(i=0;i<arr.length;i++){ // if(!dup.includes(arr[i])){ // dup.push(arr[i]) // } // } // console.log("Duplicates : "+ dup) // } // arr=[1, 2, 2, 3, 4, 4, 5] // duplicate(arr) // 5.Check if Array is Sorted // [1, 2, 3, 4, 5] → true // Write a function to check if an array is sorted in ascending order. // function issorted(arr){ // for(i=1;i<arr.length;i++){ // if(arr[i] > arr[i+1]){ // return false // } // } // return true // } // let arr =[1,2,3,4,5] // let arr=[4,3,2,1] // console.log( issorted(arr)) // 6.Reverse an Array // [1, 2, 3, 4, 5] → [5, 4, 3, 2, 1] // Write a function to reverse the elements in an array. // function reverse(arr){ // let rev=[] // for(i=arr.length-1;i>=0;i--){ // rev.push(arr[i]) // } // console.log(rev) // } // let arr=[1,2,3,4,5] // reverse(arr) // 7.Remove Falsy Values // [0, 1, false, 2, '', 3] → [1, 2, 3] // Write a function that removes all falsy values (false, 0, "", null, undefined, NaN) from an array. // function falsyremove(arr){ // let falsy=[] // for(i=0;i<arr.length;i++){ // if(arr[i]){ // falsy.push(arr[i]) // } // } // console.log(falsy) // } // let arr=[0, 1, false, 2, '', 3, "", null, undefined, NaN] // falsyremove(arr) // 8.Sum of Even Numbers & odd numbers // even: [1, 2, 3, 4, 5] → 6 odd :[1, 2, 3, 4, 5] → 9 // Write a function that returns the sum of all even numbers in an array. // function sum(arr){ // evensum=0 // oddsum=0 // for(i=0;i<arr.length;i++){ // if(arr[i] % 2 ==0){ // evensum += arr[i] // } // if(arr[i] % 2 !==0){ // oddsum += arr[i] // } // } // console.log("evensum : "+evensum) // console.log("oddsum :" +oddsum) // } // arr= [1, 2, 3, 4, 5] // sum(arr) // 9.Find Unique Elements // [1, 2, 2, 3, 4, 4, 5] → [1, 3, 5] // Write a function to find the unique elements in an array (elements that appear only once). // function unique (arr){ // let unq=[] // let empty=[] // for(i=0;i<arr.length;i++){ // if(!empty.includes(arr[i])){ // empty.push(arr[i]) // } // } // console.log("after removing Duplicates :"+empty) // for(j=0;j<empty.length;j++){ // let count =0 // for(k=0;k<arr.length;k++){ // if(empty[j]===arr[k]){ // count++ // } // } // if(count ===1){ // unq.push(empty[j]) // } // } // console.log("Unique elements :"+unq) // } // let arr= [1, 2, 2, 3, 4, 4, 5] // unique(arr) // MEDIUM LEVEL // 1.Intersection of Two Arrays // [1, 2, 3], [2, 3, 4] → [2,3] // Write a function that returns the common elements between two arrays. // function Intersection(arr,arr1){ // let common=[] // for(i=0;i<arr.length;i++){ // for(j=0;j<arr1.length;j++){ // if(arr[i] ===arr1[j]){ // common.push(arr[i]) // } // } // } // console.log(common) // } // let arr=[1,2,3] ; let arr1=[2,3,4] // Intersection(arr,arr1) // 2.Find Missing Number // [1, 2, 4, 5] → [3] // Given an array of consecutive numbers with one missing, find the missing number. // function missingnumber(arr){ // let max =Math.max(...arr); // let missing=[] // for(i=1;i<= max;i++){ // if(!arr.includes(i)){ // missing.push(i) // } // } // console.log(missing) // } // let arr=[1,3,4,7,9,10] // missingnumber(arr) // 3.Move Zeros to End // [0, 1, 0, 3, 12] → [1, 3, 12, 0, 0] // Write a function that moves all zeros in an array to the // end while maintaining the order of other elements. // function zeros(arr){ // let zero=[] // let num=[] // for(i=0;i<arr.length;i++){ // if(arr[i] !== 0){ // num.push(arr[i]) // } // else{ // zero.push(arr[i]) // } // } // const outputArray= [...num,...zero] // console.log(outputArray) // } // let arr= [0, 1, 0, 3, 12] // zeros(arr) // 4.Find the First Duplicate // [2, 1, 3, 5, 3, 2] → 3 // Write a function to return the first duplicate value in an array. // function firstduplicate (arr){ // for(let i=0;i<arr.length;i++){ // for( let j=0;j<i;j++){ // if(arr[i] ===arr[j]){ // return arr[i] // } // } // } // return -1 // } // let arr= [2, 1, 3, 5, 3, 2] // console.log(firstduplicate(arr)) // 5.Pair Sum // [2, 4, 3, 5, 7, 8, 9], 7 → [[4, 3], [2, 5]] // Write a function to find all pairs in an array whose sum is equal to a given target. function Pairsum(arr,target){ let result=[] for(i=0;i<arr.length;i++){ for(j=i+1;j<arr.length;j++){ if(arr[i]+arr[j] === target){ result.push([arr[i],arr[j]]) } } } console.log(result) } let arr= [2, 4, 3, 5, 7, 8, 9] ,target=6 Pairsum(arr,target)
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.
Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("Hello, " + line);
});
Keyword | Description | Scope |
---|---|---|
var | Var is used to declare variables(old way of declaring variables) | Function or global scope |
let | let is also used to declare variables(new way) | Global or block Scope |
const | const is used to declare const values. Once the value is assigned, it can not be modified | Global or block Scope |
let greetings = `Hello ${name}`
const msg = `
hello
world!
`
An array is a collection of items or values.
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
let mobiles = ["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
// changing an array element
mobiles[3] = "Nokia";
Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.
() => expression
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);
let [firstName, lastName] = ['Foo', 'Bar']
let {firstName, lastName} = {
firstName: 'Foo',
lastName: 'Bar'
}
const {
title,
firstName,
lastName,
...rest
} = record;
//Object spread
const post = {
...options,
type: "new"
}
//array spread
const users = [
...adminUsers,
...normalUsers
]
function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
console.log(`Hello ${name}!`);
}
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar
IF is used to execute a block of code based on a condition.
if(condition){
// code
}
Else part is used to execute the block of code when the condition fails.
if(condition){
// code
} else {
// code
}
Switch is used to replace nested If-Else statements.
switch(condition){
case 'value1' :
//code
[break;]
case 'value2' :
//code
[break;]
.......
default :
//code
[break;]
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.
class className {
constructor() { ... } //Mandatory Class method
method1() { ... }
method2() { ... }
...
}
class Mobile {
constructor(model) {
this.name = model;
}
}
mbl = new Mobile("iPhone");