Skip to content

Commit 8056718

Browse files
initial commit
0 parents  commit 8056718

30 files changed

+476
-0
lines changed

10_removeArrayBaseOnObject.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const array = [
2+
{ field: "id", operator: "eq" },
3+
{ field: "cStatus", operator: "eq" },
4+
{ field: "money", operator: "eq" },
5+
];
6+
7+
const filterField = "money"
8+
9+
function removeArrayElement(filterField) {
10+
// write your solution here
11+
const newArray = array.filter(element => element.field !== filterField);
12+
return newArray;
13+
}
14+
15+
16+
console.log(`filtered array: ${removeArrayElement(filterField)}`)

11_N-thValueOfFibonacci.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function fibonacci(n) {
2+
// write your solution here
3+
if (n === 0) {
4+
return 0;
5+
}
6+
if (n === 1) {
7+
return 1;
8+
}
9+
return fibonacci(n - 1) + fibonacci(n - 2);
10+
}
11+
12+
console.log(`fibonacci value at position 5: ${fibonacci(5)}`)

12_givenNumberSpellOut.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const sayNumberInEnglish = (n) => {
2+
const SINGLE_ARRAY = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
3+
const TEEN_ARRAY = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
4+
const DOUBLE_ARRAY = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
5+
6+
//For Single Digit
7+
if(n.toString().length === 1){
8+
return SINGLE_ARRAY[n]
9+
}
10+
11+
//For Double Digits
12+
if(n.toString().length === 2){
13+
if(parseInt(n.toString().charAt(n.length - 2)) === 1){
14+
return TEEN_ARRAY[(n%10)]
15+
}
16+
17+
if(n % 10 === 0){
18+
return DOUBLE_ARRAY[(n/10) - 2]
19+
}
20+
21+
const partOne = DOUBLE_ARRAY[Math.floor((n/10)) - 2]
22+
const partTwo = SINGLE_ARRAY[n%10]
23+
24+
return partOne + "-" + partTwo
25+
}
26+
27+
if(n%100 === 0 && parseInt(n.toString().charAt(0)) !== 0){
28+
return SINGLE_ARRAY[(n/100)] + " hundred"
29+
}
30+
31+
const remainingNumber =n - parseInt(${parseInt(n.toString().charAt(0))}00)
32+
33+
if(remainingNumber.toString().length === 1){
34+
return SINGLE_ARRAY[Math.floor((n/100))] + " hundred " + SINGLE_ARRAY[remainingNumber]
35+
}
36+
37+
if(parseInt(remainingNumber.toString().charAt(remainingNumber.length - 2)) === 1){
38+
return SINGLE_ARRAY[Math.floor((n/100))] + " hundred " + TEEN_ARRAY[(remainingNumber%10)]
39+
}
40+
41+
if(remainingNumber % 10 === 0){
42+
return SINGLE_ARRAY[Math.floor((n/100))] + " hundred " + DOUBLE_ARRAY[(remainingNumber/10) - 2]
43+
}
44+
45+
const partOne = DOUBLE_ARRAY[Math.floor((remainingNumber/10)) - 2]
46+
const partTwo = SINGLE_ARRAY[remainingNumber%10]
47+
48+
return SINGLE_ARRAY[Math.floor((n/100))] + " hundred " + partOne + "-" + partTwo
49+
};
50+
51+
52+
53+
console.log(`5635 in english is: ${sayNumberInEnglish(5635)}`)

13_covertSecondsToSpaceAge.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const spaceAge = (seconds) => {
2+
const yearsInAllPlanets = {
3+
Mercury: 0,
4+
Venus: 0,
5+
Earth: 0,
6+
Mars: 0,
7+
Jupiter: 0,
8+
Saturn: 0,
9+
Uranus: 0,
10+
Neptune: 0,
11+
};
12+
13+
const secondsInYear = 31557600;
14+
const years = seconds / secondsInYear;
15+
16+
for (let planet in yearsInAllPlanets) {
17+
yearsInAllPlanets[planet] =
18+
years /
19+
(planet === "Earth"
20+
? 1
21+
: planet === "Mercury"
22+
? 0.2408467
23+
: planet === "Venus"
24+
? 0.61519726
25+
: planet === "Mars"
26+
? 1.8808158
27+
: planet === "Jupiter"
28+
? 11.862615
29+
: planet === "Saturn"
30+
? 29.447498
31+
: planet === "Uranus"
32+
? 84.016846
33+
: planet === "Neptune"
34+
? 164.79132
35+
: 0);
36+
}
37+
38+
return yearsInAllPlanets;
39+
};
40+
41+
console.log(spaceAge(Math.round(Math.random() * 99999999)));
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*
3+
* @param {number[]} digits Array of valid digits of baseA
4+
* @param {number} baseA base a
5+
* @param {number} baseB base b in which digits are to be converted
6+
* @returns {number[]} Array of valid digits of baseB
7+
*/
8+
const convertDigitsToAskedBase = (digits, baseA, baseB) => {
9+
10+
11+
// convert digits to base 10
12+
const base10 = digits.reduce((acc, digit, i) => {
13+
return acc + digit * Math.pow(baseA, digits.length - i - 1);
14+
}, 0);
15+
16+
// convert base 10 to base b
17+
let base10_b = base10;
18+
let baseB_digits = [];
19+
do {
20+
baseB_digits.push(base10_b % baseB);
21+
base10_b = Math.floor(base10_b / baseB);
22+
} while (base10_b > 0);
23+
24+
return baseB_digits.reverse();
25+
}

15_determinesentenceIsAPangram.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const isPangram = (input) => {
2+
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
3+
const inputLowerCase = input.toLowerCase();
4+
const inputLowerCaseArray = inputLowerCase.split('');
5+
const inputLowerCaseArrayWithoutSpaces = inputLowerCaseArray.filter(char => char !== ' ');
6+
const inputLowerCaseArrayWithoutSpacesAndPunctuation = inputLowerCaseArrayWithoutSpaces.filter(char => alphabet.includes(char));
7+
const inputLowerCaseArrayWithoutSpacesAndPunctuationSet = new Set(inputLowerCaseArrayWithoutSpacesAndPunctuation);
8+
const inputLowerCaseArrayWithoutSpacesAndPunctuationSetArray = Array.from(inputLowerCaseArrayWithoutSpacesAndPunctuationSet);
9+
const inputLowerCaseArrayWithoutSpacesAndPunctuationSetArrayLength = inputLowerCaseArrayWithoutSpacesAndPunctuationSetArray.length;
10+
const alphabetLength = alphabet.length;
11+
if (inputLowerCaseArrayWithoutSpacesAndPunctuationSetArrayLength === alphabetLength) {
12+
return true;
13+
} else {
14+
return false;
15+
}
16+
};
17+

16_askTheBob.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function hey(message) {
2+
if (message.endsWith('?')) {
3+
return 'Sure.';
4+
}
5+
6+
if (message.endsWith('!')) {
7+
return 'Whoa, chill out!';
8+
}
9+
10+
if (message.trim() === '') {
11+
return 'Fine. Be that way!';
12+
}
13+
14+
return 'Whatever.';
15+
}

17_longestConsecutiveSequence.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
* @param {number[]} inputArray Array of numbers
4+
*/
5+
const longestConsecutiveSequence = (inputArray) => {
6+
7+
const set = new Set(inputArray);
8+
let maxLength = 0;
9+
for (let i = 0; i < inputArray.length; i++) {
10+
if (!set.has(inputArray[i] - 1)) {
11+
let currentNum = inputArray[i];
12+
let currentLength = 1;
13+
while (set.has(currentNum + 1)) {
14+
currentNum += 1;
15+
currentLength += 1;
16+
}
17+
maxLength = Math.max(maxLength, currentLength);
18+
}
19+
}
20+
return maxLength;
21+
}

18_calGrainsOnASquareOnChessboard.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const totalGrains = () => {
2+
// Code here
3+
4+
5+
return
6+
7+
}
8+
9+
console.log(`Grains on 5th square: ${grainsOn(5)}`)
10+
console.log(`Total grains on the Chess Board: ${totalGrains()}`)

19_resistorColorMap.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const colorCode = (color) => {
2+
// Code here
3+
switch (color) {
4+
case 'black':
5+
return 0;
6+
case 'brown':
7+
return 1;
8+
case 'red':
9+
return 2;
10+
case 'orange':
11+
return 3;
12+
case 'yellow':
13+
return 4;
14+
case 'green':
15+
return 5;
16+
case 'blue':
17+
return 6;
18+
case 'violet':
19+
return 7;
20+
case 'grey':
21+
return 8;
22+
case 'white':
23+
return 9;
24+
}
25+
}

1_randomNumber.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function randomNumberGeneratorInRange(rangeStart, rangeEnd) {
2+
// write your solution here
3+
4+
return Math.random() * (rangeEnd - rangeStart) + rangeStart;
5+
}
6+
7+
console.log(`My random number: ${randomNumberGeneratorInRange(5, 100)}`);

20_addTwoNumbers.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const addTwoNumbers = (a, b) => {
2+
// code here
3+
4+
return a + b;
5+
6+
}

21_UnionOfTwoArrays.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const unionOfArrays = (arr1, arr2) => {
2+
// code below here
3+
let newArr = [];
4+
for (let i = 0; i < arr1.length; i++) {
5+
newArr.push(arr1[i]);
6+
}
7+
for (let i = 0; i < arr2.length; i++) {
8+
if (!newArr.includes(arr2[i])) {
9+
newArr.push(arr2[i]);
10+
}
11+
}
12+
return newArr;
13+
// code above here
14+
};
15+
16+
17+
console.log(`The union is ${unionOfArrays([1, 2, 34, 45, 3], [3, 24, 21])}`);
18+

22_UniqueInOrder.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let uniqueInOrder = (iterable) => {
2+
//your code here - remember iterable can be a string or an array
3+
let result = [];
4+
let prev = null;
5+
for (let i = 0; i < iterable.length; i++) {
6+
7+
if (iterable[i] !== prev) {
8+
result.push(iterable[i]);
9+
prev = iterable[i];
10+
}
11+
}
12+
return result;
13+
};
14+

23_EqualSidesOfAnArray.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function findEvenIndex(arr) {
2+
//Code goes here!
3+
for (var i = 0; i < arr.length; i++) {
4+
var leftSum = 0;
5+
var rightSum = 0;
6+
for (var j = 0; j < i; j++) {
7+
leftSum += arr[j];
8+
}
9+
for (var k = i + 1; k < arr.length; k++) {
10+
rightSum += arr[k];
11+
}
12+
if (leftSum === rightSum) {
13+
return i;
14+
}
15+
}
16+
return -1;
17+
18+
}
19+

24_numberInExpandedForm.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function expandedForm(num) {
2+
// Your code here
3+
const currentNumArray = num.toString().split("");
4+
let currentMultiplyingFactor = 1;
5+
let currentPos = currentNumArray.length - 1;
6+
7+
while (currentPos >= 0) {
8+
currentNumArray[currentPos] = (
9+
parseInt(currentNumArray[currentPos]) * currentMultiplyingFactor
10+
).toString();
11+
currentMultiplyingFactor *= 10;
12+
currentPos--;
13+
}
14+
15+
return currentNumArray.join("+").trim();
16+
}

25_stopGninnipsMysdrow.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function spinWords(string) {
2+
//TODO Have fun :)
3+
var words = string.split(" ");
4+
var newWords = [];
5+
for (var i = 0; i < words.length; i++) {
6+
if (words[i].length >= 5) {
7+
newWords.push(words[i].split("").reverse().join(""));
8+
} else {
9+
newWords.push(words[i]);
10+
}
11+
}
12+
return newWords.join(" ");
13+
}

26_findTheOddInt.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function findOdd(arr) {
2+
//happy coding!
3+
4+
let count = {};
5+
for (let i = 0; i < arr.length; i++) {
6+
if (count[arr[i]]) {
7+
count[arr[i]]++;
8+
} else {
9+
count[arr[i]] = 1;
10+
}
11+
}
12+
for (let key in count) {
13+
if (count[key] % 2 !== 0) {
14+
return Number(key);
15+
}
16+
}
17+
}

27_vowelCount.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getCount(str) {
2+
let vowelsCount = 0;
3+
// enter your magic here
4+
const vowels = ["a", "e", "i", "o", "u"];
5+
const strArray = str.toLowerCase().split("");
6+
for (let i = 0; i < strArray.length; i++) {
7+
if (vowels.includes(strArray[i])) {
8+
vowelsCount++;
9+
}
10+
}
11+
return vowelsCount;
12+
}
13+
14+
console.log(getCount("abracadabra"));

28_welrDStringCase.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function toWeirdCase(string) {
2+
// Your code goes here
3+
var newString = "";
4+
for (var i = 0; i < string.length; i++) {
5+
if (i % 2 === 0) {
6+
newString += string[i].toUpperCase();
7+
} else {
8+
newString += string[i].toLowerCase();
9+
}
10+
}
11+
return newString;
12+
}
13+
14+
console.log(
15+
`The weird case of ${"A test case"} is ${toWeirdCase("A test case")}`
16+
);

0 commit comments

Comments
 (0)