[go: up one dir, main page]

0% found this document useful (0 votes)
183 views112 pages

JS Basic Q's

This document contains code snippets that demonstrate various JavaScript functions and methods for working with dates, strings, numbers, and other data types. Some key functions include: - Getting the current date/time and formatting it - Checking if a year is a leap year - Finding the maximum, minimum, or numbers within a given range of other numbers - Manipulating and checking properties of strings like length, characters, and substrings - Basic math operations and conditional logic The document contains over 40 short code snippets that each test a JavaScript function, many related to validating, manipulating, or comparing different data types through conditionals and returning true/false.

Uploaded by

Aibrahim84
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)
183 views112 pages

JS Basic Q's

This document contains code snippets that demonstrate various JavaScript functions and methods for working with dates, strings, numbers, and other data types. Some key functions include: - Getting the current date/time and formatting it - Checking if a year is a leap year - Finding the maximum, minimum, or numbers within a given range of other numbers - Manipulating and checking properties of strings like length, characters, and substrings - Basic math operations and conditional logic The document contains over 40 short code snippets that each test a JavaScript function, many related to validating, manipulating, or comparing different data types through conditionals and returning true/false.

Uploaded by

Aibrahim84
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
You are on page 1/ 112

const today = new Date();

const day = today.getDay();


const daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
console.log(`Today is : ${daylist[day]}.`); // // Today is : Tuesday.

let hour = today.getHours();


const minute = today.getMinutes();
const second = today.getSeconds();
let prepand = (hour >= 12)? " PM ":" AM ";

hour = (hour >= 12)? hour - 12: hour;


if (hour===0 && prepand===' PM ') {
if (minute===0 && second===0){
hour=12;
prepand=' Noon';
} else {
hour=12;
prepand=' PM';
}
}
if (hour===0 && prepand===' AM ') {
if (minute===0 && second===0) {
hour=12;
prepand=' Midnight';
} else {
hour=12;
prepand=' AM';
}
}
console.log(`Current Time : ${hour}${prepand} : ${minute} : ${second}`);
// Current Time : 10 PM : 30 : 38
let today = new Date();
let dd = today.getDate();

let mm = today.getMonth()+1;
const yyyy = today.getFullYear();

if(dd<10) {
dd=`0${dd}`;
}

if(mm<10) {
mm=`0${mm}`;
}
today = `${mm}-${dd}-${yyyy}`;
console.log(today); // 01-09-2018
today = `${mm}/${dd}/${yyyy}`;
console.log(today); // 01/09/2018
today = `${dd}-${mm}-${yyyy}`;
console.log(today); // 09-01-2018
today = `${dd}/${mm}/${yyyy}`;
console.log(today); // 09/01/2018

const side1 = 5;
const side2 = 6;
const side3 = 7;
const perimeter = (side1 + side2 + side3)/2;
const area = Math.sqrt(perimeter*((perimeter-side1)*(perimeter-
side2)*(perimeter-side3)));
console.log(area); // 14.696938456699069
leapyear = (year) => {
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}

console.log(leapyear(2016)); // true
console.log(leapyear(2000)); // true
console.log(leapyear(1700)); // false
console.log(leapyear(1800)); // false
console.log(leapyear(100)); // false

console.log('--------------------');
for (var year = 2014; year <= 2050; year++) {
var d = new Date(year, 0, 1);
if ( d.getDay() === 0 ) {
console.log("1st January is being a Sunday "+year);
}
}
console.log('--------------------');
Output
--------------------
1st January is being a Sunday 2017
1st January is being a Sunday 2023
1st January is being a Sunday 2034
1st January is being a Sunday 2040
1st January is being a Sunday 2045
--------------------

var num = Math.ceil(Math.random() * 10);


var gnum = prompt('Guess the number between 1 and 10 inclusive');
if (gnum == num){
alert('Matched');
} else {
alert('Not matched, the number was ' + num);
}
today=new Date();
var cmas=new Date(today.getFullYear(), 11, 25);
if (today.getMonth()==11 && today.getDate()>25) {
cmas.setFullYear(cmas.getFullYear()+1);
}
var one_day=1000*60*60*24;
console.log(Math.ceil((cmas.getTime()-today.getTime())/(one_day))+
" days left until Christmas!"); // 349 days left until Christmas!

<body>
<form>
1st Number : <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" onClick="multiplyBy()" Value="Multiply" />
<input type="button" onClick="divideBy()" Value="Divide" />
</form>
<p>The Result is : <br>
<span id = "result"></span>
</p>
</body>

multiplyBy = () => {
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 * num2;
}
divideBy = () => {
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 / num2;
}
cToF = (celsius) => {
var cTemp = celsius;
var cToFahr = cTemp * 9 / 5 + 32;
var message = cTemp+'\xB0C is ' + cToFahr + ' \xB0F.';
console.log(message);
}

fToC = (fahrenheit) => {


var fTemp = fahrenheit;
var fToCel = (fTemp - 32) * 5 / 9;
var message = fTemp+'\xB0F is ' + fToCel + '\xB0C.';
console.log(message);
}

cToF(60); // 60°C is 140°F.


fToC(45); // 45°F is 7.222222222222222°C.
const var_name = 'abcd';
const n = 120;
this[var_name] = n;

console.log(this[var_name]) // 120

filename = "system.php"
console.log(filename.split('.').pop()); //php
filename = "abc.js"
console.log(filename.split('.').pop()); //js
difference = (n) => {
if (n <= 13)
return 13 - n;
else
return (n - 13) * 2;
}

console.log(difference(32)) // 32
console.log(difference(11)) // 2

function sumTriple (x, y) {


if (x == y) {
return 3 * (x + y);
} else {
return (x + y);
}
}
console.log(sumTriple(10, 20)); // 30
console.log(sumTriple(10, 10)); // 60

function diff_num(n) {
if (n <= 19) {
return (19 - n);
} else {
return (n - 19) * 3;
}
}
console.log(diff_num(12)); // 7
console.log(diff_num(19)); // 0
console.log(diff_num(22)); // 9
test50 = (x, y) => {
return ((x == 50 || y == 50) || (x + y == 50));
}
console.log(test50(50, 50)) // true
console.log(test50(20, 50)) // true
console.log(test50(20, 20)) // false
console.log(test50(20, 30)) // true

testhundred = (x) => {


return ((Math.abs(100 - x) <= 20) || (Math.abs(400 - x) <= 20));
}

console.log(testhundred(10)); // false
console.log(testhundred(90)); // true
console.log(testhundred(99)); // true
console.log(testhundred(199)); // false
console.log(testhundred(200)); // false
positive_negative = (x, y) => {
if ((x < 0 && y > 0) || x > 0 && y < 0) {
return true;
} else {
return false;
}
}
console.log(positive_negative(2, 2)); // false
console.log(positive_negative(-2, 2)); // true
console.log(positive_negative(2, -2)); // true
console.log(positive_negative(-2, -2)); // false
string_check = (str1) => {
if (str1 === null || str1 === undefined || str1.substring(0, 2) === 'Py') {
return str1;
}

return "Py"+str1;
}
console.log(string_check("Python")); // Python
console.log(string_check("thon")); // Python
remove_character = (str, char_pos) => {
part1 = str.substring(0, char_pos);
part2 = str.substring(char_pos + 1, str.length);
return (part1 + part2);
}
console.log(remove_character("Python",0)); // ython
console.log(remove_character("Python",3)); // Pyton
console.log(remove_character("Python",5)); // Pytho
first_last = (str1) => {
if (str1.length <= 1) {
return str1;
}
mid_char = str1.substring(1, str1.length - 1);
return (str1.charAt(str1.length - 1)) + mid_char + str1.charAt(0);
}
console.log(first_last('a')); // a
console.log(first_last('ab'));// ba
console.log(first_last('abc'));// cba

front_back = (str) => {


first = str.substring(0,1);
return first + str + first;
}
console.log(front_back('a'));//aaa
console.log(front_back('ab'));//aaba
console.log(front_back('abc'));//aabca
test37 = (x) => {
if (x % 3 == 0 || x % 7 == 0) {
return true;
} else {
return false;
}
}
console.log(test37(12)); // true
console.log(test37(14)); // true
console.log(test37(10)); // false
console.log(test37(11)); // false
front_back3 = (str) => {
if (str.length>=3) {
str_len = 3;
back = str.substring(str.length-3);
return back + str + back;
} else {
return false;
}
}
console.log(front_back3("abc")); // abcabcabc
console.log(front_back3("ab")); // false
console.log(front_back3("abcd"));// bcdabcdbcd
start_spec_str = (str) => {
if (str.length < 4) {
return false;
}

front = str.substring(0, 4);


if (front == 'Java') {
return true;
} else {
return false;
}
}
console.log(start_spec_str("JavaScript"));// true
console.log(start_spec_str("Java")); // true
console.log(start_spec_str("Python")); // false
check_numbers = (x, y) => {
if ((x >= 50 && x <= 99) || (y >= 50 && y <= 99)) {
return true;
} else {
return false;
}
}
console.log(check_numbers(12, 101)); // false
console.log(check_numbers(52, 80)); // true
console.log(check_numbers(15, 99)); // true
check_three_nums = (x, y, z) => {
return (x >= 50 && x <= 99)
|| (y >= 50 && y <= 99)
|| (z >= 50 && z <= 99);
}
console.log(check_three_nums(50, 90, 99)); // true
console.log(check_three_nums(5, 9, 199)); // false
console.log(check_three_nums(65, 89, 199)); // true
console.log(check_three_nums(65, 9, 199)); // true
check_script = (str) =>{
if (str.length < 6) {
return str;
}

let result_str = str;


if (str.substring(10, 4) == 'Script') {
result_str = str.substring(0, 4) + str.substring(10,str.length);
}
return result_str;
}
console.log(check_script("JavaScript")); // Java
console.log(check_script("CoffeeScript"));// CoffeeScript
max_of_three = (x, y, z) => {
max_val = 0;
if (x > y) {
max_val = x;
} else {
max_val = y;
}

if (z > max_val) {
max_val = z;
}
return max_val;
}
console.log(max_of_three(1,0,1)); // 1
console.log(max_of_three(0,-10,-20)); // 0
console.log(max_of_three(1000,510,440));// 1000
near_100 = (x, y) => {
if (x != y){
x1 = Math.abs(x - 100);
y1 = Math.abs(y - 100);

if (x1 < y1){


return x;
}
if (y1 < x1){
return y;
}
return 0;
} else {
return false;
}
}

console.log(near_100(90, 89)); // 90
console.log(near_100(-90, -89));//-89
console.log(near_100(90, 90)); //false
numbers_ranges = (x, y) => {
if ((x >= 40 && x <= 60 && y >= 40 && y <= 60) ||
(x >= 70 && x <= 100 && y >= 70 && y <= 100)) {
return true;
} else {
return false;
}
}
console.log(numbers_ranges(44, 56)); // true
console.log(numbers_ranges(70, 95)); // true
console.log(numbers_ranges(50, 89)); // false
max_townums_range = (x, y) => {
if (x >= 40 && y <= 60) {
let max_val =0
if (x > y) {
max_val =x;
} else {
max_val =y;
return max_val;
}
} else {
return false;
}
}
console.log(max_townums_range(45, 60)); // 60
console.log(max_townums_range(25, 60)); // false
console.log(max_townums_range(45, 80)); // false
check_char = (str, char) => {
ctr = 0;
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) == char) {
ctr++;
}
}
return (ctr >= 2 && ctr <= 4);
}
console.log(check_char("Python", "y")); // false
console.log(check_char("JavaScript", "a"));// true
console.log(check_char("Console", "o"));// true
last_digit = (x, y, z) => {
if ((x > 0) && y > 0 && z > 0) {
return (x % 10 == y % 10 && y % 10 == z % 10 && x % 10 == z % 10);
} else {
return false;
}
}
console.log(last_digit(20, 30, 400)); // true
console.log(last_digit(-20, 30, 400)); // false
console.log(last_digit(20, -30, 400)); // false
console.log(last_digit(20, 30, -400)); // false
upper_lower = (str) => {
if (str.length < 3) {
return str.toUpperCase();
}
front_part = (str.substring(0, 3)).toLowerCase();
back_part = str.substring(3, str.length);
return front_part + back_part;
}
console.log(upper_lower("Python")); // python
console.log(upper_lower("Py")); // PY
console.log(upper_lower("JAVAScript"));// javAScript
exam_status = (totmarks,is_exam) => {
if (is_exam) {
return totmarks >= 90;
}
return (totmarks >= 89 && totmarks <= 100);
}
console.log(exam_status("78", " ")); // false
console.log(exam_status("89", "true ")); // false
console.log(exam_status("99", "true ")); // true

sortaSum = (x, y) => {


const sum_nums = x + y;
if (sum_nums >= 50 && sum_nums <= 80) {
return 65;
}
return 80;
}
console.log(sortaSum(30,20)); //65
console.log(sortaSum(90,80)); //80
check8 = (x, y) => {
if (x == 8 || y == 8) {
return true;
}

if (x + y == 8 || Math.abs(x - y) == 8){
return true;
}
return false;
}
console.log(check8(7, 8)); // true
console.log(check8(16, 8)); // true
console.log(check8(24, 32)); // true
console.log(check8(17, 18)); // false
three_numbers = (x, y, z) => {
if (x == y && y == z) {
return 30;
}

if (x == y || y == z || z == x) {
return 40;
}

return 20;
}
console.log(three_numbers(8, 8, 8)); //30
console.log(three_numbers(8, 8, 18));//40
console.log(three_numbers(8, 7, 18));//20

number_order = (x, y, z ) => {


if ( y > x && z > y) {
return "strict mode";
} else if(z > y){
return "Soft mode";
} else {
return "Undefinded";
}
}
console.log(number_order(10,15,31)); // strict mode
console.log(number_order(24,22,31)); // Soft mode
console.log(number_order(50,21,15)); // Undefinded
same_last_digit = (p, q, r) => {
return (p % 10 === q % 10) ||
(p % 10 === r % 10) ||
(q % 10 === r % 10);

}
console.log(same_last_digit(22,32,42)); // true
console.log(same_last_digit(102,302,2)); // true
console.log(same_last_digit(20,22,45)); // false
lessby20_others = (x, y, z) => {
return (x >= 20 && (x < y || x < z)) ||
(y >= 20 && (y < x || y < z)) ||
(z >= 20 && (z < y || z < x));
}
console.log(lessby20_others(23, 45, 10)); // true
console.log(lessby20_others(23, 23, 10)); // false
console.log(lessby20_others(21, 66, 75)); // true
test_nmuber = (x, y) => {
return (x === 15 ||
y === 15 ||
x + y === 15 ||
Math.abs(x - y) === 15);
}

console.log(test_nmuber(15, 9));// true


console.log(test_nmuber(25, 15)); // true
console.log(test_nmuber(7, 8)); // true
console.log(test_nmuber(25, 10)); // true
console.log(test_nmuber(5, 9)); // false
console.log(test_nmuber(7, 9)); // false
console.log(test_nmuber(9, 25)); // false

valCheck = (a, b) => {


if (!((a % 7 == 0 || a % 11 == 0) &&
(b % 7 == 0 || b % 11 == 0))) {
return ((a % 7 == 0 || a % 11 == 0) ||
(b % 7 == 0 || b % 11 == 0));
} else {
return false;
}
}
console.log(valCheck(14, 21)); // false
console.log(valCheck(14, 20)); // true
console.log(valCheck(16, 20)); // false
test_digit = (x, y, n) => {
if (n < 40 || n > 10000){
return false;
} else {
if (n >= x && n <= y){
return true;
}else {
return false;
}
}
}
console.log(test_digit(40, 4000, 45)); // true
console.log(test_digit(80, 320, 79)); // false
console.log(test_digit(89, 4000, 30)); // false
string_reverse = (str) => {
return str.split("").reverse().join("");
}
console.log(string_reverse("w3resource")); // ecruoser3w
console.log(string_reverse("www")); // www
console.log(string_reverse("JavaScript"));// tpircSavaJ
LetterChanges = (text) => {
//https://goo.gl/R8gn7u
var s = text.split('');
for(var i = 0; i < s.length; i++) {
// Caesar cipher
switch(s[i]) {
case ' ':
break;
case 'z':
s[i] = 'a';
break;
case 'Z':
s[i] = 'A';
break;
default:
s[i] = String.fromCharCode(1 + s[i].charCodeAt(0));
}

// Upper-case vowels
switch(s[i]) {
case 'a': case 'e': case 'i': case 'o': case 'u':
s[i] = s[i].toUpperCase();
}
}
return s.join('');
}
console.log(LetterChanges("PYTHON")); // QZUIPO
console.log(LetterChanges("W3R")); // X4S
console.log(LetterChanges("php")); // qIq
capital_letter = (str) => {
str = str.split(" ");
for (var i = 0, x = str.length; i < x; i++) {
str[i] = str[i][0].toUpperCase() + str[i].substr(1);
}
return str.join(" ");
}

console.log(capital_letter("Write a JavaScript program to


capitalize the first letter of each word of a given string."));
// Write A JavaScript Program To Capitalize The First Letter Of
Each Word Of A Given String.

time_convert = (num) => {


const hours = Math.floor(num / 60);
const minutes = num % 60;
return hours + ":" + minutes;
}
console.log(time_convert(71)); // 1:11
console.log(time_convert(450)); // 7:30
console.log(time_convert(1441)); // 24:1
function alphabet_Soup(str) {
return str.split("").sort().join("");
}
console.log(alphabet_Soup("Python")); // Phnoty
console.log(alphabet_Soup("Exercises"));// Eceeirssx
ab_Check = (str) => {
return (/a...b/).test(str) ||
(/b...a/).test(str);
}
console.log(ab_Check("Chainsbreak"));// true
console.log(ab_Check("pane borrowed"));// true
console.log(ab_Check("abCheck"));// false

vowel_Count = (str) => {


return str.replace(/[^aeiou]/g, "").length;
}
console.log(vowel_Count("Python")); // 1
console.log(vowel_Count("w3resource.com")); // 5
equal_pt = (str) => {
const str_p = str.replace(/[^p]/g, "");
const str_t = str.replace(/[^t]/g, "");
const p_num = str_p.length;
const s_num = str_t.length;
return p_num === s_num;
}
console.log(equal_pt("paatpss"));// false
console.log(equal_pt("paatps"));// false

division_string = (n1, n2) => {


n1 = 80;
n2 = 6;
var div = Math.round(n1 / n2).toString(),
result_array = div.split("");

if (div >= 1000){


for (var i = div.length - 3; i > 0; i -= 3) {
result_array.splice(i, 0, ",");
}
result_array;
}
console.log(result_array); // ["1","3"]
string_copies = (str, n) => {
if (n < 0) {
return false;
} else {
return str.repeat(n);
}
}
console.log(string_copies("abc", 5)); // abcabcabcabcabc
console.log(string_copies("abc", 0)); //
console.log(string_copies("abc", -2)); // false
newstring = (str) => {
if (str.length >= 3) {
result_str = str.substring(str.length - 3);
return result_str + result_str + result_str + result_str;
} else {
return false;
}
}
console.log(newstring("Python 3.0")); // 3.03.03.03.0
console.log(newstring("JS")); // false
console.log(newstring("JavaScript")); // iptiptiptipt
first_half = (str) => {
if (str.length % 2 == 0) {
return str.slice(0, str.length / 2);
}
return str;
}
console.log(first_half("Python")); // Pyt
console.log(first_half("JavaScript")); // JavaS
console.log(first_half("PHP")); // PHP
without_first_end = (str) => {
return str.substring(1, str.length - 1);
}
console.log(without_first_end('JavaScript')); // avaScrip
console.log(without_first_end('JS')); //
console.log(without_first_end('PHP')); // H

concatenate = (str1, str2) => {


str1 = str1.substring(1, str1.length);
str2 = str2.substring(1, str2.length);
return str1 + str2;
}

console.log(concatenate("PHP","JS")); // HPS
console.log(concatenate("A","B")); //
console.log(concatenate("AA","BB")); // AB
right_three = (str) => {
if (str.length > 1){
return str.slice(-3) + str.slice(0, -3);
}
return str;
}
console.log(right_three("Python")); // honPyt
console.log(right_three("JavaScript")); // iptJavaScr
console.log(right_three("Hi")); // Hi
middle_three = (str) => {
if (str.length % 2!= 0) {
mid = (str.length + 1)/2;
return str.slice(mid - 2, mid + 1);
}
return str;
}
console.log(middle_three('abcdefg')); // cde
console.log(middle_three('HTML5')); // TML
console.log(middle_three('Python')); // Python
console.log(middle_three('PHP')); // PHP
console.log(middle_three('Exercises')); // rci
str_con_cat = (str1, str2) => {
const m = Math.min(str1.length, str2.length);
return str1.substring(str1.length - m) + str2.substring(str2.length - m);
}
console.log(str_con_cat("Python", "JS")); // onJS
console.log(str_con_cat("ab", "cdef")); // abef
end_script = (str) => {
if (str.substring(str.length - 6, str.length) == 'Script') {
return true;
} else {
return false;
}
}
console.log(end_script("JavaScript"));// true
console.log(end_script("Java Script"));// true
console.log(end_script("Java Scripts"));// false

city_name = (str) => {


if(str.length >= 3 &&
((str.substring(0, 3) == 'Los') ||
(str.substring(0, 3) == 'New'))) {
return str;
}
return '';
}

console.log(city_name("New York"));// New York


console.log(city_name("Los Angeles"));// Los Angeles
console.log(city_name("London"));//
nop = (str) => {
let start_pos = 0;
let end_pos = str.length;
if (str.length > 0 && str.charAt(0) == 'P') {
start_pos = 1;
}
if (str.length > 1 && str.charAt(str.length - 1) == 'P') {
end_pos--;
}
return str.substring(start_pos, end_pos);
}

console.log(nop("PythonP")); // ython
console.log(nop("Python")); // ython
console.log(nop("JavaScript"));// JavaScript
two_string = (str, n) => {
first_part = str.substring(0, n);
last_part = str.substring(str.length - n);
return first_part + last_part;
}
console.log(two_string("JavaScript", 2)); // Japt
console.log(two_string("JavaScript", 3)); // Javipt
sum_three = (nums) => {
return nums[0] + nums[1] + nums[2];
}
console.log(sum_three([10, 32, 20])); // 62
console.log(sum_three([5, 7, 9])); // 21
console.log(sum_three([0, 8, -11])); // -3
rotate_elements_left = (array) => {
return [array[1], array[2], array[0]];
}
console.log(rotate_elements_left([3, 4, 5])); // [4,5,3]
console.log(rotate_elements_left([0, -1, 2])); // [-1,2,0]
console.log(rotate_elements_left([7, 6, 5])); // [6,5,7]

first_last_1 = (nums) => {


var end_pos = nums.length - 1;
return nums[0] == 1 || nums[end_pos] == 1;
}
console.log(first_last_1([1, 3, 5])); // true
console.log(first_last_1([1, 3, 5, 1])); // true
console.log(first_last_1([2, 4, 6])); // false
first_last_same = (nums) => {
var end = nums.length - 1;
if (nums.length >= 1){
return nums[0] == nums[end];
} else {
return false;
}
}
console.log(first_last_same([10, 20, 30])); // true
console.log(first_last_same([10, 20, 30, 10])); // true
console.log(first_last_same([20, 20, 20])); // false
reverse3 = (array) => {
return array.map((element, idx, arr) => {
arr[(arr.length - 1) - idx]);
}
}
console.log(reverse3([5, 4, 3])); // [3,4,5]
console.log(reverse3([1, 0, -1])); // [-1,0,1]
console.log(reverse3([2, 3, 1])); // [1,3,2]
all_max = (nums) => {
var max_val = nums[0] > nums[2] ? nums[0] : nums[2];
nums[0] = max_val;
nums[1] = max_val;
nums[2] = max_val;
return nums;
}
console.log(all_max([20, 30, 40])); // [40,40,40]
console.log(all_max([-7, -9, 0])); // [0,0,0]
console.log(all_max([12, 10, 3])); // [12,12,12]

middle_elements = (a, b) => {


var new_array = []
new_array.push(a[1], b[1]);
return new_array;
}
console.log(middle_elements([1, 2, 3], [1, 5, 6])); // [2,5]
console.log(middle_elements([3, 3, 3], [2, 8, 0])); // [3,8]
console.log(middle_elements([4, 2, 7], [2, 4, 5])); // [2,4]
started = (nums) => {
var array1 = [];
array1.push(nums[0], nums[nums.length - 1]);
return array1;
}
console.log(started([20, 20, 30])); // [20,30]
console.log(started([5, 2, 7, 8])); // [5,8]
console.log(started([17, 12, 34, 78])); // [17,78]
function contins13 = (nums) => {
if (nums.indexOf(1) != -1 || nums.indexOf(3) != -1){
return true
} else {
return false
}
}
console.log(contins13([1, 5])); // true
console.log(contins13([2, 3])); // true
console.log(contins13([7, 5])); // false
is13 = (nums) => {
if (nums.indexOf(1) == -1 && nums.indexOf(3) == -1){
return true;
} else {
return false;
}
}
console.log(is13([7, 8])); // true
console.log(is13([3, 2])); // false
console.log(is13([0, 1])); // false
function twice3040(arra1) {
let a = arra1[0],
b = arra1[1];
return (a === 30 && b === 30) ||
(a === 40 && b === 40);
}
console.log(twice3040([30, 30])); // true
console.log(twice3040([40, 40])); // true
console.log(twice3040([20, 20])); // false
console.log(twice3040([30])); // true
swap = (arra) => {
[arra[0], arra[arra.length - 1]] = [arra[arra.length - 1], arra[0]];
return arra;
}
console.log(swap([1, 2, 3, 4])); // [4,2,3,1]
console.log(swap([0, 2, 1])); // [1,2,0]
console.log(swap([3])); // [3]
add_two_digits = (n) => {
return n % 10 + Math.floor(n / 10);
}
console.log(add_two_digits(25)); // 7
console.log(add_two_digits(50)); // 5
add_two_int_without_carrying = (n1, n2) => {
let result = 0,
let x = 1;
while (n1 > 0 && n2 > 0) {
result += x * ((n1 + n2) % 10);
n1 = Math.floor(n1 / 10);
n2 = Math.floor(n2 / 10);
x*= 10;
}
return result;
}
console.log(add_two_int_without_carrying(222, 911)); // 133
console.log(add_two_int_without_carrying(200, 900)); // 100

longest_string = (str_ara) => {


var max = str_ara[0].length;
str_ara.map(v => max = Math.max(max, v.length));
result = str_ara.filter(v => v.length == max);
return result;
}
console.log(longest_string(['a', 'aa', 'aaa','aaaaa','aaaa']))
// ["aaaaa"]
alphabet_char_Shift = (str) => {
const all_chars = str.split("");
for(let i = 0; i < all_chars.length; i++) {
let n = all_chars[i].charCodeAt() - 'a'.charCodeAt();
n = (n + 1) % 26;
all_chars[i] = String.fromCharCode(n + 'a'.charCodeAt());
}
return all_chars.join("");
}
console.log(alphabet_char_Shift("abcdxyz")); // bcdeyza
alternate_Sums = (arr) => {
var result = [0, 0];
for(var i = 0; i < arr.length; i++) {
if(i % 2) {
result[1] += arr[i];
} else {
result[0] += arr[i];
}
}
return result
}
console.log(alternate_Sums([1, 2, 3, 4, 5, 6])); // [9,12]
angle_Type = (angle) => {
if(angle < 90) {
return "Acute angle.";
}
if(angle === 90) {
return "Right angle.";
}
if(angle < 180) {
return "Obtuse angle.";
}
return "Straight angle.";
}

console.log(angle_Type(47)); // Acute angle.


console.log(angle_Type(90)); // Right angle.
console.log(angle_Type(145)); // Obtuse angle.
console.log(angle_Type(180)); // Straight angle.
array_checking = (arra1, arra2) => {
for(let i = 0; i < arra1.length; i++) {
for(let j = i; j < arra1.length; j++) {
let result = true;
const temp = arra1[i];
arra1[i] = arra1[j];
arra1[j] = temp;
for(let k = 0; k < arra1.length; k++) {
if(arra1[k] !== arra2[k]) {
result = false;
break;
}
}
if(result) {
return true;
}
arra1[j] = arra1[i];
arra1[i] = temp;
}
}
return false;
}
console.log(array_checking([10,20,30], [10,20,30])); // true
console.log(array_checking([10,20,30], [30,10,20])); // true
console.log(array_checking([10,20,30,40], [10,30,20,40])); // false
checking_numbers = (x, y, divisor) => {
if(x % divisor === 0 &&
y % divisor === 0 ||
x % divisor !== 0 &&
y % divisor !== 0) {
return true;
}
return false;
}
console.log(checking_numbers(10, 25, 5)); // true
console.log(checking_numbers(10, 20, 5)); // true
console.log(checking_numbers(10, 20, 4)); // false
check_arithmetic_Expression = (x, y, z) => {
return x + y == z ||
x * y == z ||
x / y == z ||
x - y == z;
}
console.log(check_arithmetic_Expression(10, 25, 35)); // true
console.log(check_arithmetic_Expression(10, 25, 250)); // true
console.log(check_arithmetic_Expression(30, 25, 5)); // true
console.log(check_arithmetic_Expression(100, 25, 4.0)); // true
console.log(check_arithmetic_Expression(100, 25, 25)); // false
Kth_greatest_in_array = (arr, k) => {
for(let i = 0; i < k; i++) {
let max_index = i;
const tmp = arr[i];
for(let j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[max_index]) {
max_index = j;
}
}
arr[i] = arr[max_index];
arr[max_index] = tmp;
}
return arr[k - 1];
}
console.log(Kth_greatest_in_array([1,2,3,4,5], 3)); // 3
console.log(Kth_greatest_in_array([-10,-25,-47,-36,0], 1)); // 0
array_max_consecutive_sum = (nums, k) => {
let result = 0;
let temp_sum = 0;
for (var i = 0; i < k - 1; i++) {
temp_sum += nums[i];
}
for (var i = k - 1; i < nums.length; i++) {
temp_sum += nums[i];
if (temp_sum > result) {
result = temp_sum;
}
temp_sum -= nums[i - k + 1];
}
return result;
}
console.log(array_max_consecutive_sum([1, 2, 3, 14, 5], 2)); // 19
console.log(array_max_consecutive_sum([2, 3, 5, 1, 6], 3)); // 12
console.log(array_max_consecutive_sum([9, 3, 5, 1, 7], 2)); // 12
function max_difference(arr) {
let max = -1;
let temp;
for(let i = 0; i < arr.length - 1; i++) {
temp = Math.abs(arr[i] - arr[i + 1]);
max = Math.max(max, temp);
}
return max;
}
console.log(max_difference([1, 2, 3, 8, 9])); // 5
console.log(max_difference([1, 2, 3, 18, 9])); // 15
console.log(max_difference([13, 2, 3, 8, 9])); // 11

array_max_diff = (arr) => {


let max_result = 0;
for(let i=0;i<arr.length;i++) {
for(let k=0; k!=i && k<arr.length; k++) {
let diff = Math.abs(arr[i]-arr[k]);
max_result = Math.max(max_result, diff);
}
}
return max_result;
}
console.log(array_max_diff([1, 2, 3, 8, 9])); // 8
console.log(array_max_diff([1, 2, 3, 18, 9])); // 17
console.log(array_max_diff([13, 2, 3, 8, 9])); // 11
array_element_mode = (arr) => {
const ctr = [];
let ans = 0;
for(var i = 0; i < 10; i++) {
ctr.push(0);
}
for(var i = 0; i < arr.length; i++) {
ctr[arr[i] - 1]++;
if(ctr[arr[i] - 1] > ctr[ans]) {
ans = arr[i] - 1;
}
}
return ans + 1;
}
console.log(array_element_mode([1, 2, 3, 2, 2, 8, 1, 9])); // 2
array_element_replace = (arr, old_value, new_value) => {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === old_value) {
arr[i] = new_value;
}
}
return arr;
}
num = [1, 2, 3, 2, 2, 8, 1, 9];
console.log("Original Array: "+num); // Original Array: 1,2,3,2,2,8,1,9
console.log(array_element_replace(num, 2, 5)); // [1,5,3,5,5,8,1,9]
sum_adjacent_difference = (arr) => {
let result = 0;
for (let i = 1; i < arr.length; i++) {
result += Math.abs(arr[i] - arr[i - 1]);
}
return result;
}

console.log(sum_adjacent_difference([1, 2, 3, 2, -5])); // 10
function build_Palindrome(new_str) {
let flag;
for (let i = new_str.length;; i++) {
flag = true;
for (var j = 0; j < i - j - 1; j++) {
if (i - j - 1 < new_str.length && new_str[j] != new_str[i - j - 1]) {
flag = false;
break;
}
}
if (flag) {
for (var j = new_str.length; j < i; j++) {
new_str += new_str[i - j - 1];
}
return new_str;
}
}
}

console.log(build_Palindrome("abcddc")); // abcddcba
console.log(build_Palindrome("122")); // 1221

change_case = (new_str) => {


let x = 0;
let y = 0;
for(let i = 0; i < new_str.length; i++) {
if (/[A-Z]/.test(new_str[i])) {
x++;
} else y++;
}
if(y > x) {
return new_str.toLowerCase();
}
return new_str.toUpperCase();
}
console.log(change_case("Write")); // write
console.log(change_case("PHp")); // PHP
rearrangement_characters = (str1, str2) => {
const first_set = str1.split('');
const second_set = str2.split('');
let result = true;
first_set.sort();
second_set.sort();

for(let i = 0; i < Math.max(first_set.length, second_set.length); i++) {


if (first_set[i] !== second_set[i]) {
result = false;
}
}
return result;
}
console.log(rearrangement_characters("xyz", "zyx")); // true
console.log(rearrangement_characters("xyz", "zyp")); // false

check_common_element = (arra1, arra2) => {


for (var i = 0; i < arra1.length; i++) {
if (arra2.indexOf(arra1[i]) != -1) {
return true;
}
}
return false;
}
console.log(check_common_element([1,2,3], [3,4,5])); // true
console.log(check_common_element([1,2,3], [5,6,7])); // false
test_string = (input_str) => {
const is_lower_case = symbol => {
if ('a' <= symbol && symbol <= 'z') {
return true;
}
return false;
};

const is_upper_case = symbol => {


if ('A' <= symbol && symbol <= 'Z') {
return true;
}
return false;
};

const is_first_char_lower = is_lower_case(input_str[0]);


const is_first_char_upper = is_upper_case(input_str[0]);
if (!(is_first_char_lower || is_first_char_upper)) {
return false;
}

for (let i = 1; i < input_str.length; i++) {


if (i % 2) {
if (is_lower_case(input_str[i]) === is_first_char_lower || is_upper_case(input_str[i]) === is_first_char_upper) {
return false;
}
} else {
if (is_lower_case(input_str[i]) !== is_first_char_lower || is_upper_case(input_str[i]) !== is_first_char_upper) {
return false;
}
}
}
return true;
}
console.log(test_string('xYr')); // true
console.log(test_string('XXyx')); // false
number_of_InversionsNaive = (arr) => {
let ctr = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
ctr++;
}
}
}
return ctr;
}

console.log(number_of_InversionsNaive([0, 3, 2, 5, 9])); // 1
console.log(number_of_InversionsNaive([1, 5, 4, 3])); // 3
console.log(number_of_InversionsNaive([10, 30, 20, -10])); // 4
digit_delete = (num) => {
let result = 0;
const num_digits = [];
while (num) {
num_digits.push(num % 10);
num = Math.floor(num / 10);
}
for (let index_num = 0; index_num < num_digits.length; index_num++) {
let n = 0;
for (let i = num_digits.length - 1; i >= 0; i--) {
if (i !== index_num) {
n = n * 10 + num_digits[i];
}
}
result = Math.max(n, result);
}
return result;
}

console.log(digit_delete(100));
console.log(digit_delete(10));
console.log(digit_delete(1245));

different_values = (ara, n) => {


let max_val = -1;
for (let i = 0; i < ara.length; i++) {
for (let j = i + 1; j < ara.length; j++) {
const x = Math.abs(ara[i] - ara[j]);
if (x <= n) {
max_val = Math.max(max_val, x)
}
}
}
return max_val
}
console.log(different_values([12, 10, 33, 34], 10));// 2
console.log(different_values([12, 10, 33, 34], 24)); // 24
console.log(different_values([12, 10, 33, 44], 40)); // 34
digit_to_one = (num) => {
const digitSum = num => {
let digit_sum = 0;
while (num) {
digit_sum += num % 10;
num = Math.floor(num / 10);
}
return digit_sum;
};

let result = 0;
while (num >= 10) {
result += 1;
num = digitSum(num);
}
return result;
}

console.log(digit_to_one(123)); // 1
console.log(digit_to_one(156)); // 2
divide_digit = (num, d) => {
if (d==1){
return num;
} else {
while (num % d === 0) {
num /= d;
}
}
return num;
}
}
console.log(divide_digit(-12, 2)); // -3
console.log(divide_digit(13, 2)); // 13
console.log(divide_digit(13, 1)); // 13

arr_pairs = (arr) => {


let result = 0;
for(let i = 0; i < arr.length; i++) {
for(let j = i + 1; j < arr.length; j++){
if (arr[i] % arr[j] === 0 ||
arr[j] % arr[i] === 0){
result++;
}
}
}
return result;
}
console.log(arr_pairs([1,2,3])); // 2
console.log(arr_pairs([2,4,6])); // 2
console.log(arr_pairs([2,4,16])); // 3
dot_product = (vector1, vector2) => {
let result = 0;
for (let i = 0; i < 3; i++) {
result += vector1[i] * vector2[i];
}
return result;
}
console.log(dot_product([1,2,3], [1,2,3])); // 14
console.log(dot_product([2,4,6], [2,4,6])); // 56
console.log(dot_product([1,1,1], [0,1,-1])); // 0

sort_prime = (num) => {


const prime_num1 = [];
const prime_num2 = [];
for (var i = 0; i <= num; i++) {
prime_num2.push(true);
}
for(var i = 2; i <= num; i++) {
if(prime_num2[i]) {
prime_num1.push(i);
for(let j = 1; i * j <= num; j++) {
prime_num2[i * j] = false;
}
}
}
return prime_num1;
}

console.log(sort_prime(5)); // [2,3,5]
console.log(sort_prime(11)); // [2,3,5,7,11]
console.log(sort_prime(19)); // [2,3,5,7,11,13,17,19]
find_numbers = (arr_num, num) => {
let result = 0;
for(let i = 0; i < arr_num.length; i++) {
if(arr_num[i] % 2 === 0 && arr_num[i] !== num) {
result++;
}
if(arr_num[i] === num) {
return result;
}
}
return -1;
}

console.log(find_numbers([1,2,3,4,5,6,7,8], 5)); // 2
console.log(find_numbers([1,3,5,6,7,8], 6)); // 0
find_third_number = (x, y, z) => {
if((x!==y) && (x!==z) && (y!==z)) {
return "Three numbers are unequal.";
}
if(x==y) {
return z;
}
if(x==z) {
return y;
}
return x;
}

console.log(find_third_number(1,2,2)); // 1
console.log(find_third_number(1,1,2)); // 2
console.log(find_third_number(1,2,3)); // Three numbers are unequal.

trailing_zeros_factorial = (n) => {


let result = 0;
for (let i = 5; i <= n; i += 5) {
let num = i;
while (num % 5 === 0) {
num /= 5;
result++;
}
}
return result;
}

console.log(trailing_zeros_factorial(8)); // 1
console.log(trailing_zeros_factorial(9)); // 1
console.log(trailing_zeros_factorial(10)); // 2
int_sum = (num) => {
let s_sum = 0;
while (num > 0) {
s_sum += num;
num = Math.floor(num / 2);
}
return s_sum;
}
console.log(int_sum(8)); // 15
console.log(int_sum(9)); // 16
console.log(int_sum(26)); // 49
function is_correct_Sentence(input_str) {
var first_char = input_str[0];
var last_char = input_str[input_str.length - 1];
return /[A-Z]/.test(first_char) && last_char == "."
}

console.log(is_correct_Sentence("This tool will help you write


better English and efficiently corrects texts.")); // false
console.log(is_correct_Sentence("This tool will help you write
better English and efficiently corrects texts")); // false
console.log(is_correct_Sentence("this tool will help you write
better English and efficiently corrects texts.")); // true

is_diagonal_matrix = (user_matrix) => {


for (let i = 0; i < user_matrix.length; i++) {
for (let j = 0; j < user_matrix.length; j++) {
if (i !== j && user_matrix[i][j] !== 0) {
return false;
}
}
}
return true;
}

console.log(is_diagonal_matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3] ])); // true


console.log(is_diagonal_matrix([[1, 0, 0], [0, 2, 3], [0, 0, 3] ])); // false
is_divisible_by3 = (mask_str) => {
let digitSum = 0;
const left = '0'.charCodeAt();
const right = '9'.charCodeAt();
const result = [];
const mask_data = mask_str.split('');
let hash_pos = -1;

for(var i = 0; i < mask_data.length; i++) {


if (left <= mask_data[i].charCodeAt() &&
mask_data[i].charCodeAt() <= right) {
digitSum += mask_data[i].charCodeAt() - left;
} else {
hash_pos = i;
}
}

for (var i = 0; i < 10; i++) {


if ((digitSum + i) % 3 === 0) {
mask_data[hash_pos] = String.fromCharCode(left + i);
result.push(mask_data.join(''));
}
}
return result;
}

console.log(is_divisible_by3("2#0"));// ["210","240","270"]
console.log(is_divisible_by3("4#2"));// ["402","432","462","492"]
is_identity_Matrix = (matrix_data) => {
for(let i = 0; i < matrix_data.length; i++) {
for(let j = 0; j < matrix_data.length; j++) {
if(matrix_data[i][j] !== 1 && i === j ||
matrix_data[i][j] && i !== j) {
return false;
}
}
}
return true;
}

console.log(is_identity_Matrix([[1, 0, 0],[0, 1, 0],[0, 0, 1]])); // true


console.log(is_identity_Matrix([[1, 0, 1],[0, 1, 0],[0, 0, 1]])); // false
is_inrange = (x, y, z) => {
return y >= x && y <= z;
}

console.log(is_inrange(1,2,3)); // true
console.log(is_inrange(1,2,-3)); // false
console.log(is_inrange(1.1,1.2,1.3)); // true
is_increasing_digits_Sequence = (num) => {
var arr_num = ('' + num).split('');
for(var i = 0; i < arr_num.length - 1; i++) {
if(parseInt(arr_num[i]) >= parseInt(arr_num[i + 1])){
return false;
}
}
return true;
}

console.log(is_increasing_digits_Sequence(123)); // true
console.log(is_increasing_digits_Sequence(1223)); // false
console.log(is_increasing_digits_Sequence(45677)); // false
check_a_point = (a, b, x, y, r) => {
const dist_points = (a - x) * (a - x) + (a - y) * (a - y);
r *= r;
if (dist_points < r) {
return true;
}
return false;
}

console.log(check_a_point(0, 0, 2, 4, 6)); // true


console.log(check_a_point(0, 0, 6, 8, 6)); // false

lower_triangular_matrix = (user_matrix) => {


for (let i = 0; i < user_matrix.length; i++) {
for (let j = 0; j < user_matrix[0].length; j++) {
if (j > i && user_matrix[i][j] !== 0){
return false;
}
}
}
return true;
}
console.log(lower_triangular_matrix([[1,0,0],[2,0,0],[0,3,3]])); // true
console.log(lower_triangular_matrix([[1,0,1],[2,0,0],[0,3,3]])); // false
is_monotonous = (num) => {
if (num.length === 1) {
return true;
}
const num_direction = num[1] - num[0];
for (let i = 0; i < num.length - 1; i++) {
if (num_direction * (num[i + 1] - num[i]) <= 0) {
return false;
}
}
return true;
}
console.log(is_monotonous([1, 2, 3])); // true
console.log(is_monotonous([1, 2, 2])); // false
console.log(is_monotonous([-3, -2, -1])); // true

function is_permutation(input_arr, n) {
for (let i = 0; i < n; i++) {
if (!input_arr.includes(i + 1)) {
return false;
}
}
return true;
}
console.log(is_permutation([1,2,3,4,5], 5)); // true
console.log(is_permutation([1,2,3,5], 5)); // false
test_logical_Nor = (a, b) => {
return (!a && !b)
}
console.log(test_logical_Nor(true,false)); // false
console.log(test_logical_Nor(false,false)); // true
console.log(test_logical_Nor(true,true)); // false
longest_str_in_array = (arra) => {
let max_str = arra[0].length;
let ans = arra[0];
for (let i = 1; i < arra.length; i++) {
const maxi = arra[i].length;
if (maxi > max_str) {
ans = arra[i];
max_str = maxi;
}
}
return ans;
}
console.log(longest_str_in_array(["ab","a","abcd"]));// abcd
console.log(longest_str_in_array(["ab","ab","ab"]));// ab

max_even = (arra) => {


arra.sort((x, y) => y - x);
for (let i = 0; i < arra.length; i++) {
if (arra[i] % 2 == 0)
return arra[i];
}
}

console.log(max_even([20, 40, 200])); // 200


console.log(max_even([20, 40, 200, 301])); // 200
mirror_bits = (n) => {
return parseInt(n.toString(2).split("").reverse().join(""), 2);
}

console.log(mirror_bits(56)); // 7
console.log(mirror_bits(234)); // 87
nearest_round_number = (num) => {
while (num % 10) {
num++;
}
return num;
}

console.log(nearest_round_number(56)); // 60
console.log(nearest_round_number(592)); // 600

next_Prime_num = (num) => {


for (let i = num + 1;; i++) {
let isPrime = true;
for (let d = 2; d * d <= i; d++) {
if (i % d === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
return i;
}
}
}

console.log(next_Prime_num(3)); // 5
console.log(next_Prime_num(17)); // 19
even_digits = (num) => {
let ctr = 0;
while (num) {
ctr += num % 2 === 0;
num = Math.floor(num / 10);
}
return ctr;
}

console.log(even_digits(123)); // 1
console.log(even_digits(1020)); // 3
console.log(even_digits(102)); // 2
prefix_sums = (arr) => {
const new_arr = [];
for (let i = 0; i < arr.length; i++) {
new_arr[i] = 0;
for (let j = 0; j < i + 1; j++) {
new_arr[i] += arr[j];
}
}
return new_arr;
}
console.log(prefix_sums([1,2,3,4,5])); // [1,3,6,10,15]
console.log(prefix_sums([1,2,-3,4,5])); // [1,3,0,4,9]
prime_factors = (num) => {
function is_prime(num) {
for(let i = 2; i <= Math.sqrt(num); i++) {
if(num % i === 0) {
return false;
}
}
return true;
}
const result = [];
for(let i = 2; i <= num; i++) {
while(is_prime(i) && num % i === 0) {
if (!result.includes(i)) {
result.push(i);
}
num /= i;
}
}
return result;
}
console.log(prime_factors(100)); // [2,5]
console.log(prime_factors(101)); // [101]
console.log(prime_factors(103)); // [103]
console.log(prime_factors(104)); // [2,13]
console.log(prime_factors(105)); // [3,5,7]

function proper_improper_test(num) {
return Math.abs(num[0] / num[1]) < 1
? "Proper fraction."
: "Improper fraction.";
}

console.log(proper_improper_test([12, 300])); // Proper fraction.


console.log(proper_improper_test([2, 4])); // Proper fraction.
console.log(proper_improper_test([103, 3])); // Improper fraction.
console.log(proper_improper_test([104, 2])); // Improper fraction.
console.log(proper_improper_test([5, 40])); // Proper fraction.
function change_char(str1) {
const result = [];
for (let i = 0; i < str1.length; i++) {
const char_order = str1.charCodeAt(i) - 'a'.charCodeAt(0);
const change_char = 25 - char_order + 'a'.charCodeAt(0);
result.push(String.fromCharCode(change_char));
}
return result.join("");
}

console.log(change_char("abcxyz"));// zyxcba
console.log(change_char("python"));// kbgslm
remove_duplicate_cchars = (str) => {
const arr_char = str.split("");
const result_arr = [];
for (let i = 0; i < arr_char.length; i++) {
if (str.indexOf(arr_char[i]) === str.lastIndexOf(arr_char[i])){
result_arr.push(arr_char[i]);
}
}
return result_arr.join("");
}

console.log(remove_duplicate_cchars("abcdabc"));// d
console.log(remove_duplicate_cchars("python"));// python
console.log(remove_duplicate_cchars("abcabc"));//
console.log(remove_duplicate_cchars("1365451"));// 364
replace_first_digit = (input_str) => {
return input_str.replace(/[0-9]/g, '$');
}

console.log(replace_first_digit("abc1dabc"));// abc$dabc
console.log(replace_first_digit("p3ython"));// p$ython
console.log(replace_first_digit("ab1cabc")); // ab$cabc
test_fifteen = (num) => {
while(num<15){
num++;
}
return num;
}
console.log(test_fifteen("123"));// 123
console.log(test_fifteen("10"));// 15
console.log(test_fifteen("5"));// 15

sixteen_bits_reverse = (num) => {


let result = 0;
for (let i = 0; i < 16; i++) {
result = result * 2 + (num % 2);
num = Math.floor(num / 2);
}
return result;
}

console.log(sixteen_bits_reverse(12345)); // 39948
console.log(sixteen_bits_reverse(10)); // 20480
console.log(sixteen_bits_reverse(5)); // 40960
find_rightmost_round_number = (input_arr) => {
let result = 0;
for(let i = 0; i < input_arr.length; i++){
if(input_arr[i] % 10 === 0) {
result = i;
}
}
return result;
}

console.log(find_rightmost_round_number([1, 22, 30, 54, 56])); // 2


console.log(find_rightmost_round_number([1, 22, 32, 54, 56])); // 0
console.log(find_rightmost_round_number([1, 22, 32, 54, 50])); // 4
test_same_digit = (num) => {
const first = num % 10;
while (num) {
if (num % 10 !== first) {
return false;
}
num = Math.floor(num / 10);
}
return true
}

console.log(test_same_digit(1234)); // false
console.log(test_same_digit(1111)); // true
console.log(test_same_digit(22222222)); // true
test_same_elements_both_arrays = (arra1, arra2) => {
let result = 0;
for(let i = 0; i < arra1.length; i++) {
for(let j = 0; j < arra2.length; j++){
if(arra1[i] === arra2[j]) {
result++;
}
}
}
return result;
}

console.log(test_same_elements_both_arrays([1,2,3,4],[1,2,3,4])); // 4
console.log(test_same_elements_both_arrays([1,2,3,4],[1,2,3,5])); // 3
console.log(test_same_elements_both_arrays([1,2,3,4],[11,22,33,44])); // 0
simplify_path = (main_path) => {
const parts = main_path.split('/');
const new_path = [];
let length = 0;
for(var i = 0; i < parts.length; i++) {
const part = parts[i];
if (part === '.' || part === '' || part === '..') {
if (part === '..' && length > 0) {
length--;
}
continue;
}
new_path[length++] = part;
}

if (length === 0) {
return '/';
}

let result = '';


for (var i = 0; i < length; i++) {
result += `/${new_path[i]}` ;
}
return result;
}
console.log(simplify_path("/home/var/./www/../html//sql/"));// /home/var/html/sql
sort_by_string_length = (arra) => {
for (var i = 0; i < arra.length; i++) {
for (var j = i + 1; j < arra.length; j++) {
if (arra[i].length > arra[j].length) {
var m = arra[i];
arra[i] = arra[j];
arra[j] = m;
}
}
}
return arra;
}

var arra = ["xyz","acd","aa","bb","zzz","","a","b"];

console.log("Original array: "+ arra+"\n");


// Original array: xyz,acd,aa,bb,zzz,,a,b
console.log(sort_by_string_length(["xyz","acd","aa","bb","zzz","","a","b"]));
// ["","a","b","bb","aa","xyz","acd","zzz"]
break_address = (url_add) => {
let data = url_add.split("://");
const protocol = data[0];
data = data[1].split(".com");
const domain = data[0];
data = data[1].split("/");
if(data[1]){
return [protocol,domain,data[1]]
}
return [protocol,domain]
}

var url_add = "https://www.w3resource.com/javascript-exercises/"


console.log(`Original address: ${url_add}`);
// Original address: https://www.w3resource.com/javascript-exercises/
console.log(break_address(url_add));
// ["https","www.w3resource","javascript-exercises"]
sumn = (val) => {
let sn = 0;
let i = 0;
while (sn <= val) {
sn += i++;
}
return i - 2;
}
console.log(sumn(11)); // 4
console.log(sumn(15)); // 5

sum_Of_Cubes = (n) => {


let sumn = 0;
for(let i = 1; i <= n; i++) {
sumn += i ** 3;
}
return sumn;
}

console.log(sum_Of_Cubes(3)); // 36
console.log(sum_Of_Cubes(4)); // 100
function sum_digits_from_string(dstr) {
let dsum = 0;
for (let i = 0; i < dstr.length; i++) {
if (/[0-9]/.test(dstr[i])) {
dsum += parseInt(dstr[i])
}
}
return dsum;
}

console.log(sum_digits_from_string("abcd12efg9"));// 12
console.log(sum_digits_from_string("w3resource"));// 3
halv_array_swap = (iarra) => {
if(((iarra.length)%2)!=0) {
return false;
}
for(let i = 0; i < iarra.length / 2; i++) {
const tmp = iarra[i];
iarra[i] = iarra[i + iarra.length / 2];
iarra[i + iarra.length / 2] = tmp;
}
return iarra;
}

console.log(halv_array_swap([1,2,3,4,5,6])); // [4,5,6,1,2,3]
console.log(halv_array_swap([1,2,3,4,5,6,7])); // false

change_case = (txt) => {


let str1 = "";
for (let i = 0; i < txt.length; i++) {
if (/[A-Z]/.test(txt[i])) {
str1 += txt[i].toLowerCase();
} else {
str1 += txt[i].toUpperCase();
}
}
return str1;
}

console.log(change_case("w3resource"));// W3RESOURCE
console.log(change_case("Germany"));// gERMANY
swap_adjacent_digits = (n) => {
if(n%2!=0) {
return false;
}

let result = 0;
let x = 1;
while(n != 0) {
const dg1 = n % 10;
const dg2 = ((n - dg1) / 10) % 10;
result += x * (10 * dg1 + dg2);
n = Math.floor(n / 100);
x *= 100;
}
return result;
}

console.log(swap_adjacent_digits(1234)); // 2143
console.log(swap_adjacent_digits(123456)); // 214365
console.log(swap_adjacent_digits(12345)); // false

You might also like