Skip to content

Commit 7c1b4e6

Browse files
added all questions solution
1 parent eacd70f commit 7c1b4e6

9 files changed

+158
-0
lines changed

problem10-pingpong.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Problem 10: PingPong Challenge
2+
// Write a function that prints numbers from 1 to 20.
3+
4+
// Example:
5+
6+
// For multiples of 3, print "Ping"
7+
// For multiples of 5, print "Pong"
8+
// For multiples of both 3 and 5, print "PingPong"
9+
// If the number is not a multiple of 3 or 5, print the number itself
10+
// Example Output:
11+
12+
// 1, 2, Ping, 4, Pong, Ping, 7, 8, Ping, Pong, 11, Ping, 13, 14, PingPong, 16 …..
13+
14+
const pingPong = (input) => {
15+
let result = [];
16+
17+
for (i = 1; i <= input; i++) {
18+
if (i % 3 === 0 && i % 5 === 0) {
19+
result.push("PingPong");
20+
} else if (i % 5 === 0) {
21+
result.push("Pong");
22+
} else if (i % 3 === 0) {
23+
result.push("Ping");
24+
} else {
25+
result.push(i);
26+
}
27+
}
28+
29+
return result.join(", ");
30+
};
31+
32+
console.log(pingPong(20));

problem2-count-vowels.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Write a function that counts how many vowels (a, e, i, o, u) are in a given string.
2+
3+
// Example:
4+
5+
// Input: "programming"
6+
// Output: 3
7+
8+
const vowelCounting = (inputStr) => {
9+
const vowels = "aeiou";
10+
let vowelsCount = 0;
11+
12+
for (const char of inputStr) {
13+
if (vowels.includes(char)) {
14+
vowelsCount++;
15+
}
16+
}
17+
18+
return vowelsCount;
19+
};
20+
21+
console.log(vowelCounting("programming"));

problem3-palindrome.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Problem 3: Check for Palindrome
2+
// Write a function that checks if a string is a palindrome (reads the same forward and backward).
3+
4+
// Example:
5+
6+
// Input: "madam"
7+
// Output: true
8+
// Input: "hello"
9+
// Output: false
10+
11+
const checkPalindrome = (input) => {
12+
return input === input.split("").reverse().join("");
13+
};
14+
15+
console.log(checkPalindrome("madam"));
16+
console.log(checkPalindrome("hello"));

problem4-max-number.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Problem 4: Find the Maximum Number
2+
// Write a function that takes an array of numbers and returns the largest number.
3+
4+
// Example:
5+
6+
// Input: [5, 1, 9, 3]
7+
// Output: 9
8+
9+
const maximumNumber = (input) => {
10+
return Math.max(...input);
11+
};
12+
13+
console.log(maximumNumber([5, 1, 9, 3]));

problem5-remove-duplicates.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Problem 5: Remove Duplicates from an Array
2+
// Write a function that removes all duplicate numbers from an array.
3+
4+
// Example:
5+
6+
// Input: [1, 2, 2, 3, 4, 4]
7+
// Output: [1, 2, 3, 4]
8+
9+
const removeDuplicates = (input) => {
10+
return input.filter((item, index, array) => array.indexOf(item) === index);
11+
};
12+
13+
console.log(removeDuplicates([1, 2, 2, 3, 4, 4]));

problem6-sum-array.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Problem 6: Sum of All Numbers in an Array
2+
// Write a function that returns the sum of all numbers in an array.
3+
4+
// Example:
5+
6+
// Input: [1, 2, 3, 4]
7+
// Output: 10
8+
9+
const sumOfAllNumbers = (input) => {
10+
let total = 0;
11+
for (const i of input) {
12+
total = total + i;
13+
}
14+
return total;
15+
};
16+
17+
console.log(sumOfAllNumbers([1, 2, 3, 4]));

problem7-even-numbers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Problem 7: Find Even Numbers in an Array
2+
// Write a function that returns all even numbers from a given array.
3+
4+
// Example:
5+
6+
// Input: [1, 2, 3, 4, 5, 6]
7+
// Output: [2, 4, 6]
8+
9+
const evenNumbers = (input) => {
10+
return input.filter((item) => item % 2 === 0);
11+
};
12+
13+
console.log(evenNumbers([1, 2, 3, 4, 5, 6]));

problem8-capitalize-words.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Problem 8: Capitalize First Letter of Each Word
2+
// Write a function that capitalizes the first letter of each word in a string.
3+
4+
// Example:
5+
6+
// Input: "hello world"
7+
// Output: "Hello World"
8+
9+
const capitalizesFirstLetter = (input) => {
10+
const inputArray = input.split(" ");
11+
const capitalizeArray = inputArray.map((item) => item.charAt(0).toUpperCase() + item.slice(1));
12+
return capitalizeArray.join(" ");
13+
};
14+
15+
console.log(capitalizesFirstLetter("hello world"));

problem9-factorial.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Problem 9: Find the Factorial of a Number
2+
// Write a function that calculates the factorial of a number using a loop.
3+
4+
// Example:
5+
6+
// Input: 5
7+
// Output: 120
8+
9+
const factorialNumber = (input) => {
10+
let output = 1;
11+
for (let i = 2; i <= input; i++) {
12+
output *= i;
13+
}
14+
15+
return output;
16+
};
17+
18+
console.log(factorialNumber(5));

0 commit comments

Comments
 (0)