Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Problem 1: fizzbuzzChallenge
// write a function that accepts a number and
// 1. returns a string "fizz" if it's divisible by 3,
// 2. returns string "buzz" if divisible by 5,
// 3. returns "fizzbuzz" of divisible by both 3 and 5
// 4. Otherwise returns the input number (if above conditions are not matched)
const fizzbuzzChallenge = num => {
if (typeof num !== "number") {
return null;
}
if (num % 3 === 0 && num % 5 === 0) {
return "fizzbuzz";
} else if (num % 3 === 0) {
return "fizz";
} else if (num % 5 === 0) {
return "buzz";
} else {
return num;
}
};
//Time-complexity: Constant O(1)
//Test fizzbuzzChallenge
console.log(fizzbuzzChallenge(9) === "fizz");
console.log(fizzbuzzChallenge(10) === "buzz");
console.log(fizzbuzzChallenge(15) === "fizzbuzz");
console.log(fizzbuzzChallenge(7) === 7);
console.log(fizzbuzzChallenge(0) === "fizzbuzz");
console.log(fizzbuzzChallenge() === null);
console.log(fizzbuzzChallenge("string") === null);
// Problem 2: reverseChallenge
//write a function that accepts an array and returns another array containing values in reverse order
const reverseChallenge = array => {
const newArray = [];
const m = array.length / 2;
const n = array.length;
for (let i = 0; i < m; i++) {
newArray[i] = array[n - i - 1];
newArray[n - i - 1] = array[i];
}
return newArray;
};
//Time-complexity: O(n-1)
//Test reverseChallenge
const isArrayEqual = (array1, array2) => {
if (array1.length !== array2.length) {
return false;
}
for (let j = 0; j < array1.length; j++) {
if (array1[j] !== array2[j]) {
return false;
} else {
return true;
}
}
};
const isEqual = (a, b) => {
if (Array.isArray(a) && Array.isArray(b)) {
return isArrayEqual(a, b);
} else {
return a === b;
}
};
console.log(isEqual(reverseChallenge([1, 2, 3]), [3, 2, 1]));
console.log(isEqual(reverseChallenge([1]), [1]));
console.log(isEqual(reverseChallenge([0]), [0]));
console.log(isEqual(reverseChallenge(["a", "b", "c"]), ["c", "b", "a"]));
console.log(
isEqual(reverseChallenge(["apple", "banana", "cherry"]), [
"cherry",
"banana",
"apple"
])
);
console.log(isEqual(reverseChallenge(["apple"]), ["apple"]));
@vaibhavKumarTokyo

This comment has been minimized.

Copy link

commented Oct 14, 2019

Problem 3: Write a function which accepts an array as input and returns array of duplicates else empty array.
ex: input: [1,2,2], output: [2]
Problem 4: Write a function which accepts an array and returns true if it is a pallindrome else returns false.
ex: input: [1,2,1], output: true

@ShilpiMaurya

This comment has been minimized.

Copy link
Owner Author

commented Oct 21, 2019

// Problem 4: Write a function which accepts an array and returns true if it is a pallindrome else returns false.ex: input: [1,2,1], output: true
const araryPallindrone = array => {
  const newArray = [];
  const m = array.length / 2;
  const n = array.length;
  for (let i = 0; i < m; i++) {
    newArray[i] = array[n - i - 1];
    newArray[n - i - 1] = array[i];
  }
  return newArray;
};

//Time-complexity: O(n-1)

//test

const isPallindrone = (array1, array2) => {
  if (JSON.stringify(array1) === JSON.stringify(array2)) {
    return true;
  } else {
    return false;
  }
};
const isEqual = (a, b) => {
  if (Array.isArray(a) && Array.isArray(b)) {
    return isPallindrone(a, b);
  } else {
    return "only arrays can be written in this space";
  }
};
console.log(isEqual(araryPallindrone([1, 2, 1]), [1, 2, 1]));
console.log(isEqual(araryPallindrone([1, 2, 2, 2, 1]), [1, 2, 2, 2, 1]));
console.log(isEqual(araryPallindrone(["a", "b", "a"]), ["a", "b", "a"]));
console.log(isEqual(araryPallindrone(["a"]), ["a"]));
console.log(isEqual(araryPallindrone([1]), [1]));
console.log(isEqual(araryPallindrone([]), []));
console.log(isEqual(araryPallindrone([])));

// Problem 3: Write a function which accepts an array as input and returns array of duplicates else empty array.
// ex: input: [1,2,2], output: [2]

const duplicates = array => {
  const newArray = [];
};
@vaibhavKumarTokyo

This comment has been minimized.

Copy link

commented Oct 21, 2019

// Test
// Objective: 
// 1. Program is working for main-conditions
// 2. Program is working for boundary-conditions

// How
// Compare expected-output with actual-output
// isPallindrome([1,2,1]) === true
// isPallindrome([1,1]) === true
// isPallindrome([1,1,3]) === false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.