Skip to content

Instantly share code, notes, and snippets.

View N-Asadnajafi's full-sized avatar
🎯
Focusing

N-Asadnajafi

🎯
Focusing
View GitHub Profile
@N-Asadnajafi
N-Asadnajafi / task3.js
Created July 6, 2019 13:48
Simple Recursive Binary Search Java Script
function binarySearch(Array,startIndex, endIndex, x){
middleIndex= Math.floor((startIndex + endIndex)/2);
if(startIndex > endIndex)
return -1;
if (x == Array[middleIndex])
return middleIndex;
else if (x < Array[middleIndex])
return binarySearch(Array, startIndex, middleIndex -1, x);
else
return binarySearch(Array, middleIndex + 1, endIndex, x);
@N-Asadnajafi
N-Asadnajafi / task2.js
Last active July 3, 2019 11:40
Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half. Given a ticket number n, determine if it's lucky or not with JavaScript.
function isLucky(n) {
var sumLeft = 0;
var sumRight = 0;
digits= n.toString().split('');//to make an array of characters, at first change the numbers to string and then use split() method.
ArrayNum = digits.map(Number);//we have an array of strings and want to change the type to numbers so we use .map() method.
len = ArrayNum.length;
if(len % 2 !==0){
return false;//check if the "n" has even number of digits or not.
@N-Asadnajafi
N-Asadnajafi / task1.js
Last active July 3, 2019 11:37
Given two strings, find the number of common characters between them with JavaScript
function commonCharacterCount(s1, s2){
var e=0;
ArrayS1 = s1.split('');//This method changes the string to an array.
ArrayS2 = s2.split('');
for(var i=0; i<ArrayS1.length;i++){
for(var j=0; j<ArrayS2.length; j++){
if(ArrayS1[i]==ArrayS2[j]){
e++;
ArrayS1.splice(i,1);//This method removes the ith and jth elements which are equal
ArrayS2.splice(j,1);