T

@Tammar_Haider

42. Write a JavaScript function to find the unique elements from two arrays.

NodeJS
3 years ago
console.log(difference([1, 2, 3], [100, 2, 1, 10])); function difference(ar1,ar2) { var temp=[] for(var i =0 ;i <ar1.length ; i++) { for(var j = 0 ; j<ar2.length; j++) { if(ar1[i] == ar2[j])

25. Function that accept a list of country names as input and returns the longest country name O/P

NodeJS
3 years ago
console.log(Longest_Country_Name(["Australia", "Germany","UnitedStates of America"])) function Longest_Country_Name() { }

28. Write a JavaScript program to pass a 'JavaScript function' as parameter.

NodeJS
3 years ago
function Gt() { return 'Bye'; } function OL(user,func) { const mesg=func() console.log( mesg+' '+user); }

29. Write a JavaScript function to get the function name

NodeJS
3 years ago
function CallMe() { console.log( arguments.callee.name ); } CallMe() function Call() { console.log( arguments.callee.name ); }

Switch Case Example

NodeJS
3 years ago
// program using switch statement let a = 1; switch (a) { case 1: a = 'one'; break; case 2: a = 'two';

Prototype

NodeJS
3 years ago
const obj = { property : "Tammar", p: console.log("Prototype"), property3 : "Val" }; console.log(obj.property3); //console.log(obj) let str = '{"name": "john", "age": 15}';

32. Write a JavaScript function to remove non-printable ASCII chars.

NodeJS
3 years ago
console.log(remove_non_ascii('äÄçÇéÉêPHP-MySQLöÖÐþúÚ')); console.log(remove_non_ascii('PHP ~!@#$%^&*()+`-={}[]|\\:";\'/?><., MySQL')); function remove_non_ascii(str) { var temp = str.split(''); var ar = []; var t for(var i=0 ; i<temp.length ; i++)

22. Write a JavaScript function to get a part of a string after a specified character.

NodeJS
3 years ago
console.log(subStrAfterChars('w3resource: JavaScript Exercises', ':')); console.log(subStrAfterChars('w3resource: JavaScript Exercises', 'E')); console.log(subStrAfterChars('w3resource: JavaScript Exercises', 'r')); function subStrAfterChars(str,spc) { var temp =str.split(''); // console.log(temp); var ar = [];

17. Write a JavaScript function to chop a string into chunks of a given length.

NodeJS
3 years ago
//console.log(string_chop('w3resource')); console.log(string_chop('w3resource',2)); //console.log(string_chop('w3resource',3)); function string_chop(str,chunk) { // var temp= str.split('') // console.log(temp); var ar = []; for(var i=0 ; i<str.length ; i+=chunk)

16. Function to truncates a string if it is longer than the specified number of char. Truncated

NodeJS
3 years ago
console.log(text_truncate('We are doing JS string exercises.')) console.log(text_truncate('We are doing JS string exercises.',19,'...')) console.log(text_truncate('We are doing JS string exercises.',15,'!!')) function text_truncate(str,pos,ch) { var temp= str.split(''); var ar = []; var ch; for(var i = 0; i<temp.length ; i++)

15. Write a JavaScript function to humanized number (Formats a number to a human-readable string.)

NodeJS
3 years ago
console.log(humanize_format());console.log(humanize_format(1)); console.log(humanize_format(8));console.log(humanize_format(301)); console.log(humanize_format(402)); function humanize_format(num) { var d = num%10; var ch; if(d == 1) { ch = 'st';

14. Write a JavaScript function to insert a string within a string at a particular position

NodeJS
3 years ago
console.log(insert('We are doing some exercises.')); console.log(insert('We are doing some exercises.','JavaScript ')); console.log(insert('We are doing some exercises.','JavaScript ',18)); function insert(str,ch,pos) { var temp = str.split(' '); var arr = []; for(var i=0 ; i<=temp.length ; i++) {

12. Write a JavaScript function to uncamelize a string.

NodeJS
3 years ago
console.log(uncamelize('helloWorld')); console.log(uncamelize('helloWorld','-')); console.log(uncamelize('helloWorld','_')); function uncamelize(str,ch) { var temp = str.split('') var arr = []; for(var i=0 ;i<str.length ; i++) {

11. Write a JavaScript function to convert a string into camel case.

NodeJS
3 years ago
console.log(camelize("JavaScript Exercises")); console.log(camelize("JavaScript exercises")); console.log(camelize("Java Script")); console.log(camelize("Java c")); function camelize(str) { var temp = str.split(''); var arr = [] for(var i =0 ; i<temp.length ; i++) {

5. Write a JavaScript function to convert a string in abbreviated form.

NodeJS
3 years ago
console.log(abbrev_name("Robin Singh")); function abbrev_name(str) { var temp = str.split(''); var ar = []; for(var i =0 ; i<temp.length ; i++) { // ar.push(temp[i]); if(temp[i] == ' ') { ar.push(temp[i]) ar.push(temp[i+1]); break; } ar.push(temp[i]); } return ar.join(''); }

1. Write a JavaScript function to check whether an `input` is a string or not.

NodeJS
3 years ago
console.log(is_string('w3resource')); console.log(is_string([1, 2, 4, 0])); function is_string(str) { var temp = Array.isArray(str); return temp }

2. Write a JavaScript function to check whether a string is blank or not.

NodeJS
3 years ago
console.log(is_Blank('')); console.log(is_Blank('abc')); function is_Blank(str) { var temp = str.split(''); console.log(temp.length); if(temp.length>0) { return false;

3. Write a JavaScript function to split a string and convert it into an array of words.

NodeJS
3 years ago
console.log(string_to_array("Robin Singh")); function string_to_array(str) { var temp = str.split(' '); return temp; }

4. Write a JavaScript function to remove specified number of characters from a string

NodeJS
3 years ago
console.log(truncate_string("Robin Singh",4)); function truncate_string(str,pos) { var temp = str.split('') var ar = []; for(var i=0 ; i<pos ; i++) { ar.push(temp[i]); }

6. Write a JavaScript function to hide email addresses to protect from unauthorized user.

NodeJS
3 years ago
console.log(protect_email("robin_singh@example.com")); function protect_email(str) { var temp = str.split('') var ar = []; var ch ='...'; for(var i = 0 ; i< temp.length ; i++) { if(i>4 && i<12) {