Skip to content

Instantly share code, notes, and snippets.

View qodirovshohijahon's full-sized avatar
🇺🇿
Focusing

Shokhijakhon Kodirov qodirovshohijahon

🇺🇿
Focusing
View GitHub Profile
@qodirovshohijahon
qodirovshohijahon / array_arrayIsUniqueWithXOR.js
Last active January 9, 2020 16:32
In this example it is described how to specify unique numbers in array through XOR(bitwise).
let result = 0;
let arr = [1, 2, 5, 4, 6, 1, 2, 5, 4];
for(let i = 0; i <= arr.length; i++){
result = result ^ arr[i];
}
console.log(" this is an unique number " + result);
@qodirovshohijahon
qodirovshohijahon / array_reversing_v_0_1.js
Last active January 11, 2020 13:55
In this example you can see one of the common way to reverse array elements using for loop.
function reversedString (mystr) {
let str = "";
for(let i = mystr.length; i > 0; i--){
str += i;
}
return str;
}
@qodirovshohijahon
qodirovshohijahon / array_reversing_v_0_2.js
Created January 11, 2020 13:54
In this example you can see one of the common way to reverse array elements using js build-in functions.
function reversedStringWihtBuildinFunc(str) {
return str.split("").reverse().join("");
}
@qodirovshohijahon
qodirovshohijahon / array_reversing_v_0_3.js
Created January 11, 2020 13:56
In this example you can see one of unique way to reverse array elements using recursive functions
function reverseString (str) {
if(str === "") return ""; else
return reverseString(str.substr(1)) + str.charAt(0);
}
@qodirovshohijahon
qodirovshohijahon / swapping_v_0.1.js
Created January 13, 2020 13:30
In this example you will see swapping in js.
function swappingUsingTwoVars(num1, num2) {
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
return ("First number " + num1 + "\nSecond number " + num2);
}
@qodirovshohijahon
qodirovshohijahon / MultiplicationAndSumofDigits_v_0.1.js
Created January 28, 2020 10:16
Subtract the Product and Sum of Digits of an Integer
**
* @param {number} n
* @return {number}
*/
var subtractProductAndSum = function(n) {
let num = n.toString().split("");
let count = num.reduce((acc, sum)=>{
acc += Number(sum);
return acc;
}, 0);
**
* @param {number} n
* @return {number}
*/
var subtractProductAndSum = function(n) {
let strNum = n.toString();
let arr = strNum.split("");
let mul = 1, count = 0;
for (let i = 0; i < arr.length; i++) {
mul *= arr[i];
@qodirovshohijahon
qodirovshohijahon / MultiplicationAndSumofDigits_optimal.js
Created January 28, 2020 10:53
Subtract the Product and Sum of Digits of an Integer
var subtractProductAndSum = function(n) {
if (n / 10 == 0)
return 0;
let mul = 1, sum = 0;
while( n != 0) {
mul *= n % 10;
sum += n % 10;
n = Math.trunc(n / 10);
}
return mul - sum;
@qodirovshohijahon
qodirovshohijahon / palindrom_integer.js
Created February 6, 2020 07:55
In this example you can see to check given number wether pali or not.
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
let str = "";
let digit = x.toString();
for(let i = digit.length - 1; i >= 0; i-- ) {
str += digit[i];
}
@qodirovshohijahon
qodirovshohijahon / palindrom_integer_v_0.1.js
Created February 6, 2020 08:59
In this example you can see solving palindrom numbers in another way.
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
let sum = 0, n, m = x;
if (x < 0) return false;
while (x != 0 ) {
sum = sum * 10 + x % 10;;
x = Math.floor (x / 10);