-
-
Save ShilpiMaurya/3852153e62cbf47007ba2c55fe83b7c4 to your computer and use it in GitHub Desktop.
// 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"])); |
ShilpiMaurya
commented
Nov 17, 2019
•
Please refresh your js string manipulation here (or someplace else):
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods
Next challenge:
Programming challenge 7.1
Create a function called fixStart
. It should take a single argument, a string, and return a version where all occurences of its first character have been replaced with '*', except for the first character itself. You can assume that the string is at least one character long. For example:
fixStart('babble'): 'ba**le'
Programming challenge 7.2
Create a function called notBad
that takes a single argument, a string.
- It should find the first appearance of the substring 'not' and 'bad'.
- If the 'bad' follows the 'not', then it should replace the whole 'not'...'bad' substring with 'good' and return the result.
- If it doesn't find 'not' and 'bad' in the right sequence (or at all), just return the original sentence.
For example:
notBad('This dinner is not that bad!'): 'This dinner is good!'
notBad('This movie is not so bad!'): 'This movie is good!'
notBad('This dinner is bad!'): 'This dinner is bad!'
Programming challenge 7.3
How to find the maximum occurring character in given String?
If two or more characters have same occurrence then return the first appeared-character in the string.
For example:
solution("Today is Monday"): 'a'
Programming challenge 7.4
How do you count a number of vowels and consonants in a given string?
Programming challenge 7.5
How do you check if a given String contains valid parentheses?
For example:
isValid("this is (properly closed)"): true
isValid("this is) not ((properly closed)"): false
isValid("(())"): true
isValid(")(())"): false