Skip to content

mdsharia4613/js-loop-task

Repository files navigation

🧠 JavaScript Practice Problems

(Loops + String Methods)


Problem 1: Count Characters in a String

Write a program to count how many characters are in a given string (without using .length directly inside the loop).
Hint: Use a loop to go through each character and count them manually.

Sample Input: "Hello"
Sample Output: Total characters: 5


Problem 2: Reverse a String

Write a program that reverses a given string.
Hint: Use a loop to add each character to a new string in reverse order.

Sample Input: "World"
Sample Output: "dlroW"


Problem 3: Count Vowels in a String

Write a program to count how many vowels (a, e, i, o, u) are in a given string.
Hint: Use a loop and if condition to check each character.

Sample Input: "Programming"
Sample Output: Vowels: 3


Problem 4: Convert String to Uppercase

Write a program to convert a string to uppercase manually (without using .toUpperCase() inside loop).
Hint: Use .toUpperCase() once after reading the string.

Sample Input: "hello"
Sample Output: "HELLO"


Problem 5: Find a Character Occurrence

Write a program to count how many times a given character appears in a string.
Hint: Use a loop and compare each character with the given one.

Sample Input:
String: "banana"
Character: "a"
Sample Output: "a appears 3 times"


Problem 6: Check Palindrome

Write a program to check whether a string is a palindrome (same forward and backward).
Hint: Compare the string with its reversed version.

Sample Input: "madam"
Sample Output: "Palindrome"


Problem 7: Print Each Character

Write a program that prints each character of a string on a new line.
Hint: Use a for loop to go through the string.

Sample Input: "Code"
Sample Output:

C
o
d
e

Problem 8: Word Counter

Write a program to count how many words are in a sentence.
Hint: Use .split(" ") to separate words.

Sample Input: "I love JavaScript"
Sample Output: Words: 3


Problem 9: Replace All Spaces

Write a program to replace all spaces in a string with a hyphen (-).
Hint: Use .replaceAll(" ", "-").

Sample Input: "I love JS"
Sample Output: "I-love-JS"


Problem 10: Repeat Word Using Loop

Write a program that prints a given word 5 times using a loop.
Hint: Use for loop and console.log().

Sample Input: "Hello"
Sample Output:

Hello
Hello
Hello
Hello
Hello

Problem 11: Remove Extra Spaces

Write a program to remove extra spaces from a sentence.
Hint: Use .trim() and .replaceAll(" ", " ").

Sample Input: " Hello JS "
Sample Output: "Hello JS"


Problem 12: Find Longest Word

Write a program to find the longest word in a sentence.
Hint: Use .split(" ") and compare lengths using a loop.

Sample Input: "I love programming"
Sample Output: "programming"


Problem 13: Count Digits in a String

Write a program to count how many digits (0–9) are in a given string.
Hint: Use a loop and if to check if a character is between '0' and '9'.

Sample Input: "abc123xyz45"
Sample Output: Digits: 5


Problem 14: String Without Vowels

Write a program to remove all vowels from a string.
Hint: Use .replaceAll() or a loop to skip vowels.

Sample Input: "Hello"
Sample Output: "Hll"


Problem 15: Combine Loop and Slice

Write a program that prints every substring of a given string using loop and .slice().
Hint: Use for loop and slice from 0 to i.

Sample Input: "JS"
Sample Output:

J
JS

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published