JavaScript Interview Logical Programs with Solutions


1. Write a function that takes an integer minutes and converts it to seconds.
Example 1 :
function convert(minutes) {
    var seconds = minutes * 60;
    return (seconds);
}
let output = convert(5) ;
console.log(output);

2. Create a function that takes the age in years and returns the age in days.
Example :
function calcAge(age) {
    let ageInDays = (age*365)>=0 && typeof(age*365) == 'number' ? (age*365) : []
    return ageInDays
}
let age = calcAge(10);
console.log(age );

3. Write a JavaScript program to compute the sum of the two given integers. If the two values are the same, then return triple their sum.
Example:
function sumTriple (x, y) {
    if (x == y) {
        return 3 * (x + y);
    } else {
        return (x + y);
    }
}
console.log(sumTriple(10, 20));
console.log(sumTriple(10, 10));

4. Write a JavaScript program to check whether a given integer is within 20 of 100 or 400.
Example :
function testhundred(x) {
    return ((Math.abs(100 - x) <= 20) || (Math.abs(400 - x) <= 20));
}

console.log(testhundred(10));
console.log(testhundred(90));
console.log(testhundred(99));
console.log(testhundred(199));
console.log(testhundred(200));

5. Write a JavaScript program to check two given integers whether one is positive and another one is negative.
Example:
function positive_negative(x, y)
{
    if ((x < 0 && y > 0) || x > 0 && y < 0){
        return true;
    } else {
        return false;
    }
}
console.log(positive_negative(2, 2));
console.log(positive_negative(-2, 2));
console.log(positive_negative(2, -2));
console.log(positive_negative(-2, -2));

6. Write a JavaScript program to remove a character at the specified position in a given string and return the modified string.
Example:
function remove_character(str, char_pos){
    part1 = str.substring(0, char_pos);
    part2 = str.substring(char_pos + 1, str.length);
    return (part1 + part2);
}

console.log(remove_character("Python",0));
console.log(remove_character("Python",3));
console.log(remove_character("Python",5));

7. Write a JavaScript program to create another string from a given string with the first character of the given string added to the front and back.
Example:
function front_back(str)
{
    first = str.substring(0,1);
    return first + str + first;
}
console.log(front_back('a'));
console.log(front_back('ab'));
console.log(front_back('abc'));

8.Write a JavaScript program to check whether a given positive number is a multiple of 3 or 7.
Example:
function test37(x)
{
    if (x % 3 == 0 || x % 7 == 0)
    {
        return true;
    } else {
        return false;
    }
}
console.log(test37(12));
console.log(test37(14));
console.log(test37(10));
console.log(test37(11));

9. Write a JavaScript program to create a string from a given string. This is done by taking the last 3 characters and adding them at both the front and back. The string length must be 3 or more.
Example:
function front_back3(str)
{
    if (str.length>=3)
    {
        str_len = 3;
        back = str.substring(str.length-3);
        return back + str + back;
    } else {
        return false;
    }
}
console.log(front_back3("abc"));
console.log(front_back3("ab"));
console.log(front_back3("abcd"));


10.Write a JavaScript program to check whether a string "Script" appears at the 5th (index 4) position in a given string. If "Script" appears in the string, return the string without "Script" otherwise return the original one.
Example:
function check_script(str)
{
    if (str.length < 6) {
        return str;
    }
    let result_str = str;

    if (str.substring(10, 4) == 'Script'){
        result_str = str.substring(0, 4) + str.substring(10,str.length);
    }
return result_str;
}

console.log(check_script("JavaScript"));
console.log(check_script("CoffeeScript"));

Comments