Back to Exercises

Conditionals & Loops

20 Exercises Available

If/else, loops, early return — learn to write clean logic.

1.Medium

Password Strength (basic)

Create a function `passwordStrength(pw)` that returns "weak" | "medium" | "strong". Rules: - strong: length>=8 AND has uppercase AND has lowercase AND has digit - medium: length>=6 AND (has digit OR has uppercase) - else weak

Sample Output:

passwordStrength("Password123") => "strong"
passwordStrength("Pass1") => "medium"
passwordStrength("pass") => "weak"
View Solution
2.Medium

Find First Missing Positive (simple version)

Given an array of integers, create a function `firstMissingPositive(arr)` that returns the smallest missing positive integer. Constraint: simple approach allowed (O(n log n) ok).

Sample Output:

firstMissingPositive([3, 4, -1, 1]) => 2
firstMissingPositive([1, 2, 3]) => 4
View Solution
3.Easy

FizzBuzz (clean)

Print 1..n. 3→Fizz, 5→Buzz, both→FizzBuzz, else number.

Sample Output:

fizzBuzz(5) => [1, 2, "Fizz", 4, "Buzz"]
fizzBuzz(15) => [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
View Solution
4.Easy

Order Status Message (real-world)

Create a function `statusMessage(status)`. Statuses: "pending" | "paid" | "shipped" | "delivered" | "cancelled" Default message: "Unknown status".

Sample Output:

statusMessage("pending") => "Order placed. Payment pending."
statusMessage("paid") => "Payment received. Preparing shipment."
statusMessage("delivered") => "Delivered. Enjoy!"
View Solution
5.Easy

Count Vowels in Text

Create a function `countVowels(text)` that counts vowels (a,e,i,o,u) case-insensitive.

Sample Output:

countVowels("Hello") => 2
countVowels("JavaScript") => 3
countVowels("xyz") => 0
View Solution
6.Easy

Check if Array Contains Duplicates

Create a function `hasDuplicates(arr)` that checks if array contains duplicates. Example: - hasDuplicates([1, 2, 3, 2]) → true - hasDuplicates([1, 2, 3]) → false

Sample Output:

hasDuplicates([1, 2, 3, 2]) => true
hasDuplicates([1, 2, 3]) => false
hasDuplicates(["a", "b", "a"]) => true
View Solution
7.Easy

Find Longest Word in Sentence

Create a function `longestWord(sentence)` that returns the longest word from sentence. Example: longestWord("The quick brown fox") → "quick"

Sample Output:

longestWord("The quick brown fox") => "quick"
longestWord("Hello World") => "Hello"
View Solution
8.Easy

Check if String is Palindrome

Create a function `isPalindrome(str)` that checks if string is palindrome (case-insensitive). Example: - isPalindrome("racecar") → true - isPalindrome("hello") → false

Sample Output:

isPalindrome("racecar") => true
isPalindrome("hello") => false
isPalindrome("A man a plan a canal Panama") => true
View Solution
9.Medium

Find Second Largest Number

Create a function `secondLargest(arr)` that returns second largest number from array. Example: secondLargest([3, 7, 2, 9, 1]) → 7

Sample Output:

secondLargest([3, 7, 2, 9, 1]) => 7
secondLargest([10, 10, 5]) => 10
secondLargest([1]) => null
View Solution
10.Easy

Count Occurrences of Each Character

Create a function `charCount(str)` that returns count of each character in string. Example: charCount("hello") → {h: 1, e: 1, l: 2, o: 1}

Sample Output:

charCount("hello") => {h: 1, e: 1, l: 2, o: 1}
charCount("aabbcc") => {a: 2, b: 2, c: 2}
View Solution
11.Easy

Check if Number is Perfect Square

Create a function `isPerfectSquare(n)` that checks if number is perfect square. Example: - isPerfectSquare(16) → true - isPerfectSquare(15) → false

Sample Output:

isPerfectSquare(16) => true
isPerfectSquare(15) => false
isPerfectSquare(0) => true
View Solution
12.Easy

Find Common Elements in Two Arrays

Create a function `commonElements(arr1, arr2)` that returns common elements in both arrays. Example: commonElements([1, 2, 3], [2, 3, 4]) → [2, 3]

Sample Output:

commonElements([1, 2, 3], [2, 3, 4]) => [2, 3]
commonElements(["a", "b"], ["b", "c"]) => ["b"]
View Solution
13.Easy

Check if String Contains Only Digits

Create a function `isNumeric(str)` that checks if string contains only digits. Example: - isNumeric("123") → true - isNumeric("12a") → false

Sample Output:

isNumeric("123") => true
isNumeric("12a") => false
isNumeric("") => false
View Solution
14.Easy

Find Factorial of Number

Create a function `factorial(n)` that calculates factorial of a number. Example: - factorial(5) → 120 - factorial(0) → 1

Sample Output:

factorial(5) => 120
factorial(0) => 1
factorial(3) => 6
View Solution
15.Easy

Check if Array is Sorted

Create a function `isSorted(arr, order="asc")` that checks if array is sorted. Example: - isSorted([1, 2, 3]) → true - isSorted([3, 2, 1], "desc") → true

Sample Output:

isSorted([1, 2, 3]) => true
isSorted([3, 2, 1], "desc") => true
isSorted([1, 3, 2]) => false
View Solution
16.Medium

Find All Prime Numbers up to N

Create a function `primesUpTo(n)` that returns all prime numbers up to n. Example: primesUpTo(10) → [2, 3, 5, 7]

Sample Output:

primesUpTo(10) => [2, 3, 5, 7]
primesUpTo(20) => [2, 3, 5, 7, 11, 13, 17, 19]
View Solution
17.Easy

Find Maximum Difference in Array

Create a function `maxDifference(arr)` that returns maximum difference (max - min) in array. Example: maxDifference([3, 7, 2, 9, 1]) → 8 (9 - 1)

Sample Output:

maxDifference([3, 7, 2, 9, 1]) => 8
maxDifference([10, 5, 20]) => 15
View Solution
18.Easy

Check if String Starts with Substring

Function `startsWith(str, prefix)` banao jo check kare ki string prefix se start hoti hai ya nahi (case-insensitive). Example: - startsWith("Hello", "he") → true - startsWith("World", "he") → false

Sample Output:

startsWith("Hello", "he") => true
startsWith("World", "he") => false
View Solution
19.Easy

Find Sum of All Even Numbers

Create a function `sumEven(arr)` that returns sum of all even numbers in array. Example: sumEven([1, 2, 3, 4, 5]) → 6 (2 + 4)

Sample Output:

sumEven([1, 2, 3, 4, 5]) => 6
sumEven([2, 4, 6]) => 12
View Solution
20.Easy

Check if Year is Leap Year

Create a function `isLeapYear(year)` that checks if year is leap year. Rules: Divisible by 4, but not by 100 unless also by 400. Example: - isLeapYear(2024) → true - isLeapYear(1900) → false

Sample Output:

isLeapYear(2024) => true
isLeapYear(1900) => false
isLeapYear(2000) => true
View Solution