0% found this document useful (0 votes)
4 views4 pages

Js Programs 1

Uploaded by

sivangsantonino
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Js Programs 1

Uploaded by

sivangsantonino
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Factorial

var n = 5;
function factorial(n) {
if (n === 0) {
return 1;
}
else {
return n * factorial( n - 1 );
}
}

[Link](factorial(n));

Check Armstrong Number of n Digits


// program to check an Armstrong number of n digits

// take an input
const number = prompt("Enter a positive integer");
const numberOfDigits = [Link];
var sum = 0;

// create a temporary variable


var temp = number;

while (temp > 0) {

var remainder = temp % 10;

sum += remainder ** numberOfDigits;

// removing last digit from the number


temp = parseInt(temp / 10); // convert float into integer
}

if (sum == number) {
[Link](`${number} is an Armstrong number`);
}
else {
[Link](`${number} is not an Armstrong number.`);
}

Write a function to convert Celsius to Fahrenheit

Function `calFahrenheit()` returns the converted Celsius value to Fahrenheit value based on the
formula `(Celsius × 9/5) + 32 = Fahrenheit`

function calFahrenheit(cel) {
return (cel*9/5)+32; //Conversion formula
}

[Link](calFahrenheit(0)) //32
[Link](calFahrenheit(20)) //68
[Link](calFahrenheit(40)) //104

9. Write a function to check if an input string is a palindrome

Function `checkPalindrome()` return a boolean value based on whether the input string is
palindrome or not.

function checkPalindrome(str) {
for(var i=0;i<[Link];i++){
if([Link](i)!=[Link]([Link]-i-1)){ // Comparison fail
return false;
}
}
return true;
}

[Link](findPalindrome("bannana")) //false
[Link](findPalindrome("racecar")) //true
[Link](findPalindrome("madam")) //true

Write a function to check if a number is Prime

Function `checkPrime()` return Boolean value based on whether the number is Prime or not.

function checkPrime(num, div=2) {


// Base case
if (num <= 2){
return (num == 2) ? true : false;
}

// No Divisor case
if (div * div > num){
return true;
}

// Number is not prime if divisible


if (num % div == 0){
return false;
}
// Recursive call with next divisor
return checkPrime(num, div+1);
}

[Link](checkPrime(27)) // false
[Link](checkPrime(19)) // true

JS code to print a pattern using for loop

Function printPattern() is used to print a pattern for a given range using nested for loop.
function printPattern(range) {
for(var i=1;i<=range;i++){
var str="";
for(var j=1;j<=i;j++){
str += j+" ";
}
[Link](str);
}
}
printPatter(8);

/* 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 */

Write a JS code to print the Fibonacci series for a given value of N

The function fibonacci() prints the Fibonacci series for the given range N using While loop.

function fibonacci(n) {
var n1=0;
var n2=1;
var count=2;
var n3;
[Link](n1,n2);
while(count<n){
n3=n1+n2;
[Link](n3); //print current element
n1=n2;
n2=n3;
count++;
}
}

fibonacci(8) //0 1 1 2 3 5 8 13

Sum of digits
function sumOfDigit(num) {
var numStr = [Link]();
var sum = 0;

for (var digit of numStr) {


sum += parseInt(digit);
}
return sum;
}

[Link](sumOfDigit(738));

Write a function to find the area and perimeter of a Circle

Function `circleValues()` returns the perimeter and area of the circle provided the radius as an
argument for the function call.

function circleValues(rad) {
return `Perimeter: ${2*[Link]*rad}, Area: ${[Link]*rad*rad}`;
}

[Link](circleValues(10)) //"Perimeter: 62.83, Area: 314.15"


[Link](circleValues(15)) //"Perimeter: 94.24, Area: 706.85"
[Link](circleValues(25)) //"Perimeter: 157.07, Area: 1963.49"

Write a function to reverse a number

Function `reverseNum()` returns the reversed number for the given argument number value.

function reverseNum(num) {
var reverse = 0;
while(num != 0)
{
reverse = reverse * 10;
reverse = reverse + num%10;
num = [Link](num/10); // Remove decimal places
}
return reverse;
}

[Link](reverseNum(123)) //321
[Link](reverseNum(5872)) //2785

JS code to find the no of digits in a number

Function digitCount() to returns the number of digits in a given number using while loop.

function digitCount(num) {
var count=0; //return 1 for pow=0
while(num!=0){
num=[Link](num/10);
count++;
}
return count;
}
[Link](digitCount(8367)); //4
[Link](digitCount(563349)); //6

You might also like