Answer to Question #231951 in HTML/JavaScript Web Application for raj

Question #231951

Gardening

We have a task to do Gardening.

Given two boolean values

  1. isGrassTrimmerFound and isWaterHosePipeFound as inputs, create three JS promises using async/await and try/catch blocks. For cutting the grass,
  • resolve with "Grass Trimmed" text, if the isGrassTrimmerFound is true
  • reject with "Grass Trimmer Not Found" text, if the isGrassTrimmerFound is false
  1. For cleaning the garden,
  • resolve with "Garden Cleaned" text
  1. For watering the plants,
  • resolve with "Watered Plants" text, if the isWaterHosePipeFound is true
  • reject with "Water Hose Pipe Not Found" text, if the isWaterHosePipeFound is false
1
Expert's answer
2021-09-01T23:56:28-0400
function grassTrimmer(param){
      return new Promise((resolve,reject)=>{
            if(param === true){
                  resolve("Grass Trimmed");
            }else{
                  reject("Grass Trimmer not Found");
            }
      })
}


function watering(param){
      return new Promise((resolve,reject)=>{
            if(param === true){
                  resolve("Watered Plants");
            }else{
                  reject("Water Hose pipe not found");
            }
      })
}


function gardenCleaning(){
      if(isGrassTrimmerFound === true && isWaterHosePipe === true){
            return new Promise((resolve,reject)=>{
                  resolve("Garden Cleaned");
            })
      }else{
            return new Promise((resolve,reject)=>{
                  reject("Garden not Cleaned")
            })
      }
}
async function checkCleanliness(isGrassTrimmerFound,isWaterHosePipe){
      try {
           let trimmerCheck = await grassTrimmer(isGrassTrimmerFound).then((res)=>console.log(res)).catch((res)=>console.log(res));
           
           let waterCheck = await watering(isWaterHosePipe).then((res)=>console.log(res)).catch((res)=>console.log(res));


           let gardenCheck = await gardenCleaning().then((res)=>console.log(res)).catch((res)=>console.log(res));
      } catch (error) {
            console.log(error);
      }
      
}
//inputs
let isGrassTrimmerFound = true;
let isWaterHosePipe = true;


checkCleanliness(isGrassTrimmerFound,isWaterHosePipe);


//Output
/* 
 Grass Trimmed
 Watered Plants
 Garden Cleaned
*/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS