Skip to content

A list of programming problems to improve logic building skills from beginner to intermediate with problem statements and solutions in javascript.

Notifications You must be signed in to change notification settings

panindrakumar84/logic-building-in-javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Programming problems in Javascript

A list of programming problem statements and with solutions implemented in javascript (Node js).

These list of problems helps to understand the building blocks of logical building includes questions from beginner to intermediate.

Below solution are coded to execute with node.js with command: node filename.js and some lines are different to execute in browser console please modify those if you want to execute in browser console.

  1. Write a program to add 5 numbers. The value of numbers are num1=5, num2=13, num3=7, num4=21 and num5=48.
function solution(a,b,c,d,e){
	return a+b+c+d+e;
}
console.log(solution(5,13,7,21,48));
  1. Write a program to take a number input from user and determine whether the number is odd or even.
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Please enter a number: ', function(input) {
	if (input % 2 === 0) {
		console.log(input + " is a Even number");
	} 
	else { console.log(input + " is a odd number");
	}
  rl.close();
});
  1. Write a program to find the maximum and minimum out of two given numbers. The numbers are num1=129 and num2=251.
function solution(a,b){
	let min = Math.min(a,b);
	let max = Math.max(a,b);
	return "Max is "+ max + ", Min is " + min;
}    
console.log(solution(129,251));
  1. Find Minimum value from an Array
function solution(arr){
	let minVal = arr[0];
	for(i = 1; i < arr.length; i++){
		if (arr[i] <= minVal)
			minVal = arr[i];
	}
	return minVal
}
let arr = [23,21,45,67,12]
console.log("Min of array: "+solution(arr));
  1. Write a program to find the maximum out of three given numbers. The numbers are num1=8, num2=23 and num3=17
function solution(a,b,c){
	if (a > b) return a;
	else if (b > c) return b;
	else return c;
}
console.log("Max of three numbers: "+ solution(8,23,17));
  1. Write a program to find the minimum out of three given numbers. The numbers are num1=35, num2=29 and num3=46.
function solution(a,b,c){
	if (a < b) return a;
	else if (b < c) return b;
	else return c;
}
console.log("Min of three numbers: "+ solution(35,29,46));
  1. Write program to take a month as an input from the user and find out whether the month has 31 days or not.
function solution(month){
	oddMonths = ["january","march","may","july","august","october","december"]
	month = month.toLowerCase();

	if (month === "february") return 28;
	else if (oddMonths.includes(month)) return 31;
	else return 30;
}
result = solution("july")
console.log(result+" days");
  1. Fizzbuzz - Write a program to return an array from 1 to 100. But for every multiple of 3, replace the number with "Fizz", for every multiple of 5, replace the number with "Buzz" and for every multiples of 3 & 5, replace with "FizzBuzz".

    • Your output should look something like this `1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17 ..... `
      
function solution(){
	result = []
	for (i = 1; i <= 100; i++){
		if (i % 15 === 0) result.push("FizzBuzz");
		else if (i % 5 === 0) result.push("Buzz");
		else if (i % 3 === 0) result.push("Fizz");
		else result.push(i);
	}
	return result
}
console.log(solution());
  1. Print the following star pattern :-

    *
    * *
    * * *
    * * * *
    * * * * *

function solution(){
	for (i = 1; i <= 5; i++){
		for (j = 1; j <= i; j++){
			process.stdout.write("* ");			
		}
		console.log();		
	}
}
solution()
  1. Write a program to take a number input from user and print multiplication table 12 times for that number.
const readline = require('readline');
const readLine = readline.createInterface({
	input:process.stdin,
	output:process.stdout
});

readLine.question("Enter a number: ",function(input){
	for (i = 1; i <= 12; i++){
		process.stdout.write(input + " * " + i + " = "+ input*i);
		console.log();		
	}

	readLine.close();
})
  1. Write a program to return a Fibonacci series : 0,1,1,2,3,5,8,13,21....
function solution(num){
	let a = 0;
	let b = 1;
	let c = 0;
	for (i = 0; i <= num; i++){
		process.stdout.write(c+", ");
		a = b;
		b = c;
		c = a+b;
	}
}
solution(10);
  1. Write a program to take an input from a user and find its Factorial.
  • Example: Factorial of 5 is 120 i.e. 5*4*3*2*1=120
const { log } = require("console");
const readline = require("readline");
const rl = readline.createInterface({
	input:process.stdin,
	output:process.stdout
})
rl.question("enter a number: ",function (num){
	let result = 1;
	for (i = 2; i <= num; i++){
		result *= i
	}
	console.log(result);

	rl.close()
} )
  1. Write a Program to take a number input from user and find if the number is Prime or not.
const { log } = require("console");
const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout
})
rl.question("enter a number: ", function (num) {

	function isPrime(num) {

		for (let i = 2; i <= num ** 0.5; i++) {
			if (num % i === 0){
				return 0
			}
		}
		return 1
	}
	if (isPrime(num)) console.log(num+" is a prime");
	else console.log(num+" is not a prime");

	rl.close()
})
  1. Write a program to take a day as an input and determine whether it is a weekday or weekend. Example: Tuesday is weekday.
  1. Given a and b, your function should return the value of ab
    Example:
    Input: power(2,3) ––> Output: 8
function power(a,b){
	return a**b
}
console.log(power(2,3));
  1. Given length of a regular hexagon, your function should return area of the hexagon.
    Example:
    Input: areaOfHexagon(10) ––> Output: 259.80
function solution(side){
	return (3 * (3**0.5)/2)*(side**2)
}
console.log(solution(10));
  1. Given a sentence, your functions should return the number of words in the sentence.
    Example:
    Input: noOfWords(We are developers) ––> Output: 3
function solution(str){
	return (str.split(' ')).length
}
console.log(solution("We are developers"));
  1. Given n numbers, your function should return the minimum of them all. The number of parameters won't be accepted from user.
    Example:
    Input: findMin(3,5) ––> Output: 3
    Input: findMin(3,5,9,1) ––> Output: 1
    (Hint: Lookup rest parameters in JavaScript)
function solution(...arr){
	minVal = arr[0]
	for (let num of arr){
		if (num < minVal)
			minVal = num
	}
	return minVal
}
console.log(solution(3,5,9,1));
  1. Given three angles of a triange, your function should return if it is a scalene, isosceles, equilateral triangle or not a triangle at all. Example:
    Input: typeOfTriangle(30, 60, 90) ––> Output: Scalene Triangle
function solution(a, b, c) {
	if (a === b && b === c) return "equilateral"
	else if (a !== b || b !== c) return "scalene"
	else return "isosceles"
}
console.log(solution(30, 60, 90));
  1. Given an array, your function should return the length of the array.
    Example:
    Input: arrayLength([1,5,3,7,8]) ––> Output: 5
function solution(arr){
	count = 0;
	for (let item in arr) 
		count += 1;
	return count;
}
console.log(solution([1,2,4,45,5,3]));
  1. Given an array and an item, your function should return the index at which the item is present. Example: Input: indexOf([1,6,3,5,8,9], 3) ––> Output: 2
function solution(arr,num) {
	for (let i = 0; i < arr.length; i++){
		if (arr[i] === num)
			return i;
	}
}
console.log(solution([1,6,3,5,8,9],3));
  1. Given an array and two numbers, your function should replace all entries of first number in an array with the second number. Example: Input: replace([1,5,3,5,6,8], 5, 10) ––> Output: [1,10,3,10,6,8]
function solution(arr,a,b){
	for (let i = 0; i < arr.length; i++){
		if (arr[i] === a)
			arr[i] = b;
	}
	return arr;
}
console.log(solution([1,5,3,5,6,8], 5, 10));
  1. Given two arrays, your function should return single merged array. Example: Input: mergeArray([1,3,5], [2,4,6]) ––> Output: [1,3,5,2,4,6]
function solution(arr1, arr2){
	for (let num in arr2)
		arr1.push(Number(num));
	return arr1;
}
console.log(solution([1,3,5], [2,4,6]));
  1. Given a string and an index, your function should return the character present at that index in the string. Example: Input: charAt("developer", 4) ––> Output: e
function solution(str,charAt){
	return str[charAt-1];
}
console.log(solution("developer",4));
  1. Given two dates, your function should return which one comes before the other. Example: Input: minDate('02/05/2021', '24/01/2021') ––> Output: 24/01/2021
function solution(a,b){
	const date1 = new Date(a);
	const date2 = new Date(b);
	if (date1.getTime() < date2.getTime())
		return a + " is earlier than " + b
	else return  a + " is later than " + b
}
console.log(solution('02/05/2021', '24/01/2021'));
  1. Write a function which generates a secret code from a given string, by shifting characters of alphabet by N places. Example: Input: encodeString("abcd", 2) ––> Output: cdef Explanation: 2 represents shifting alphabets by 2 places. a –> c, b –> d, c –> e and so on.
function solution(str,shift){
	result = ''
	for (let i = 0; i < str.length; i++){
		charNum = str.charCodeAt(i);		
		let num = (charNum-97+shift)%26+97
		result += String.fromCharCode(num);
	}
	return result;
}
console.log(solution('abcd',2));

About

A list of programming problems to improve logic building skills from beginner to intermediate with problem statements and solutions in javascript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published