T

@Tammar_Haider

Write a JavaScript function to create a Zerofilled value

NodeJS
3 years ago
console.log(zeroFill(11090, 8)); console.log(zeroFill(120, 2)); console.log(zeroFill(120, 5)); function zeroFill(num,val) { var temp = num.toString().split(''); var gen = val-(temp.length); var arr = []; if(val<=(temp.length))

Write a JavaScript function to test case insensitive string comparison.

NodeJS
3 years ago
console.log(compare_strings('abcd', 'AbcD')); console.log(compare_strings('abcd', 'Abce')); console.log(compare_strings('ABCD', 'ABCD')); console.log(compare_strings('abcd', 'Abce')); function compare_strings(str1,str2) { var temp = str1.split(''); var temp2 = str2.split('') for(var i =0; i<temp.length ; i++)

JavaScript function to create a case-insensitive search

NodeJS
3 years ago
console.log(case_insensitive_search('JavaScript Exercises','Exercises')); console.log(case_insensitive_search('JavaScript Exercises','Exercisesss')); function case_insensitive_search(str1, str2) { var temp = str1.includes(str2); // console.log(temp); if(temp) {

Function to move an array element from one position to another.

NodeJS
3 years ago
console.log(move([10, 20, 30, 40, 50 ], 0, 2)); function move(arr,pos,to) { var temp = Array.from(arr); console.log(temp); temp[to]=arr[pos] for(var i = to-1 ; i>=pos ; i--)

function to filter false, null, 0 and blank values from an array

NodeJS
3 years ago
console.log(filter_array_values([58, '', 'abcd', true, null, false, 0])); function filter_array_values(arr) { arr = arr.filter(check); return arr; } function check(value) { if(value !== false || value !== null || value !== 0 || value !== "") { return value;

Generate an array of specified length, filled Integer numbers, increase by one from starting pos.

NodeJS
3 years ago
console.log(array_range(1, 4)) console.log(array_range(-6, 4)) function array_range(Start,Till) { var temp = [] for(var i = Start ; i <= Till ; i++) { temp.push(i);

JavaScript function to generate an array between two integers

NodeJS
3 years ago
console.log(rangeBetwee(7, 4)); function rangeBetwee(A,B) { var temp = new Set(); if(A>B) { for(var i=B ; i<=A ; i++) { temp.add(i); } } else { for(var i=A ; i<=B ; i++) { temp.add(i); } } return temp; }

find the unique elements from two arrays.

NodeJS
3 years ago
console.log(difference([1, 2, 3], [100, 2, 1,9, 10])); function difference(setA,setB) { var union = new Set(setA); console.log('union 1st : '+setA); console.log('union 2nd : '+setB); console.log('-------------'); for (var i=0; i<setB.length ; i++) {

19) Sum of each individual index value from the given arrays.

NodeJS
3 years ago
array1 = [1,0,2,3,4]; array2 = [3,5,6,7,8,13]; var len = array1.length> array2.length ? true : false console.log(len); var sum =[]; for(var i =0;i<array2.length ;i++) { sum[i] = array1[i] + array2[i]; }

17. Write a JavaScript program to shuffle an array.

NodeJS
3 years ago
function shuffleArray(array) { return array.slice(2) +','+ array.slice(0,3); } console.log('Real Array '+[1,2,3,4,5,6]) console.log(shuffleArray([1,2,3,4,5,6]));

16. Find the leap years in a given range of years

NodeJS
3 years ago
Input = [2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2016, 2011, 2012] console.log(checkYear(Input)); var res=[]; function checkYear(year) { for(var i =0; i<year.length ; i++) { if(((year[i] % 4 == 0) && (year[i] % 100 != 0)) || (year[i] % 400 == 0)) {

15. We have the following arrays

NodeJS
3 years ago
var color = ["Blue ", "Green", "Red", "Orange", "Violet", "Indigo", "Yellow ","Red"]; var item = ["th","st","nd","rd"]; console.log('1'+item[1]+' Choice is '+color[0]); console.log('2'+item[2]+' Choice is '+color[1]); console.log('3'+item[3]+' Choice is '+color[2]); for(var i = 3 ; i<color.length ; i++) { console.log(i+1 +'th Choice is '+color[i]); }

Write a JavaScript program to remove duplicate items from an array

NodeJS
3 years ago
let chars = [1,4,6,1,6,8,9,9,6]; let uniqueChars = [...new Set(chars)]; // using sets console.log(uniqueChars);

Write a JavaScript program to compute the sum and product of an array of integers.

NodeJS
3 years ago
var ar = [1,2,3,4,5]; console.log("Array is "+ar); console.log(Sum(ar)); console.log(Product(ar)); function Sum(Arr) { var sum = 0; for(var i= 0; i< ar.length ; i++) {

Write a JavaScript program to find the sum of squares of a numeric vector

NodeJS
3 years ago
var arr = [0,1,2,3,4]; console.log(SumofSq(arr)); function SumofSq(Arr) { sum = 0; for(var i =0; i< Arr.length ;i++) { sum =sum + Arr[i]*Arr[i]; }

10. Write a JavaScript program which prints the elements of the following array. Note : Use nested f

NodeJS
3 years ago
var a = [[1, 2, 1, 24], [8, 11, 9, 4], [7, 0, 7, 27], [7, 4, 28, 14], [3, 10, 26, 7]]; console.log(a); for(var i =0 ; i<a.length ; i++) { for(var j =0 ; j<i ; j++) {

program which accept a string as input and swap the case of each character

NodeJS
3 years ago
var str = "The Quick Brown Fox"; console.log(str.split("").join("")); var result = str.split("").map(item => (item ===item.toUpperCase() ? item.toLowerCase() : item.toUpperCase())).join(""); console.log(result);

8.) Program to find the most frequent item of an array

NodeJS
3 years ago
console.log(Freq([3, '3', '3', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3])); function Freq(arr1) { var count = 1; var m = 0; var x; for (var i=0; i<arr1.length; i++) { for (var j=i; j<arr1.length; j++) {

7) To sort the items of an array. Sample array : var arr1 = [ 3, 8, 7,] O/P [3,7,8]

NodeJS
3 years ago
console.log(sorting([3,1,9,-1,4,4])); function sorting(arr) { var NewArr = arr.sort(); return NewArr; }

6) program which accept a number as input and insert dashes (-) between each two even number

NodeJS
3 years ago
console.log(DashesGenerator(025468)); var arr = 025468; function DashesGenerator(x) { var num =Array.from(String(x), Number); console.log(num); }