JS Basic Q's
JS Basic Q's
JS Basic Q's
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
--------------------
<body>
<form>
1st Number : <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" Value="Multiply" />
<input type="button" 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);
}
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 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
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
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);
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
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
}
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);
}
// 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(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
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]
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
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));
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
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.
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_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_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;
}
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
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
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(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
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(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 '/';
}
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
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