javaScript Program and solutions

javaScript Program and solutions

 

Questions: 1

Write a function named astroGuru that: takes 4 arguments: number of children, spouse’s name, location, job. outputs a string to the console that is of the form:

 "You will be a job in location, and married to spouse with no of children kids. 

 answer:


    //  function name astroGuru that takes 4 arguments-job, location, spouse, noOfChildren
    function astroGuru(job, location, spouse, noOfChildren){
   // "You will be a `job` in `location`, and married to `spouse` with `no of children` kids."
        let str =`you will be a ${job} in ${location} and married to ${spouse} with ${noOfChildren} kids. `;
        return str;
    }

    /*
    outputs a string to the console that is of the form:
    */
 
    let result = astroGuru("Backand Developer", "Chicago","Jenny","2");
    console.log(result);;





  // Another way to print this  

    let job = "Backand Developer job";
    let location = "London";
    let spouse = "Jenny";
    let noOfChildren = "3";

    let str = `you will be a ${job } in ${location} and married to ${spouse} with ${noOfChildren} kids.`;
    console.log(str);



 

 


Questions: 2

Write a program to check a number is prime or Not ?

answer:


    var number = 33;
    var isPrime = true;
    for(let i = 2; i <= number/2; i++){
        if(number%i === 0){
            isPrime = false;
            break;
        }
    }
    if(isPrime){
        console.log("This is a Prime number.");
    }
    else{
        console.log("This is not a Prime number.");
    }


 

 

 

Questions: 3

Find Given number is Odd or  Even ?

 
    let number = 20;
    if(number % 2 ===0){
        console.log("This is even number.");
    }
    else{
        console.log("This is Odd number.");
    }


 

Questions: 4

Create a program which gives output for children to go out in park if the temperature is between 20 degree Celsius to 25 degree Celsius and if it's not raining outside, ask them to be in play school if the temperature is between 18 degrees Celsius to 20 degrees Celsius and raining otherwise, they should not step out of the home.

answer:

 
    let temp = 20;
    let raining = true;
    // let raining = false;
    if(temp >= 20 && temp <= 25 && raining == false) {
        console.log("Students can play outside.");
    }
    else if(temp >= 18 && temp <= 20 && raining == true){
        console.log("Student can stay at play school.");
    }
    else{
        console.log("Student should not step out of the home.");
    }




Ternary Operator: Ternary operator have three Operands.

You can only write one statement in Ternary Operator.
When we make any short decision, in that case we use Ternary Operator. 

Let see an example of Ternary Operator---


    //  ternary Operator

    let raining = false;
    let goOutSide = raining == true ? "don't go out" : "go out";
    console.log(goOutSide);
 
    output: go out. 
 
 




Questions: 5

Create a program to print the result as pass if the marks obtained is greater than or equal to 40 using Ternary Operator.

 
    let marks = 70;
    let passFail = marks >= 40 ? "Passed" : "Fail";
    console.log(passFail);


    // or with Parenthesis ()  

    let marks = 70;
    let passFail = (marks >= 40) ? "Passed" : "Fail";
    console.log(passFail);
 
    output: Passed 



or

 
    let marks = 70;
    let passFail = (marks >= 40) ? (marks >= 60) ? "Excellent" : "Pass" : "Fail";
    console.log(passFail);
 
     output: Excellent 




HCF ---> Highest Common Factor

GCD----> Greatest Common Divisor


Questions: 6

Write a program to find the HCF or GCD of two integers ?

 
    let hcf = 1;
    let num = 8;
    let num1 = 12;
    for(let i =2; i <=num && i<= num1; i++){
        if(num%i===0 && num1%i===0){
            hcf = i;
        }
    }
    console.log(hcf);
 


 

Questions: 7

Create a program to find if the number is positive, negative or zero.

 
    let num = -2;
    switch(true){  
        case(num > 0):
        console.log("Positive");
        break;
        case(num < 0):
        console.log("Negative");
        break;

    default:
        console.log("Zero");
    }

    
    OUTPUT: Negative
 


Questions: 8


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

 

إرسال تعليق (0)
أحدث أقدم