Home > Software engineering >  How can I generate an error which says "Javascript For/in loop not working - Object property is
How can I generate an error which says "Javascript For/in loop not working - Object property is

Time:08-28

I tried this code to raise an error but it works fine.

 <!DOCTYPE html>
<html>
<body>

<h2>JavaScript For In Loop</h2>
<p>The for in statement loops through the properties of an object:</p>

<p id="demo"></p>

<script>
const basket= {
  cup: 1,
  plate: 2,
  mug: 100
}

function checkBasket(basket, lookingFor) {
  for(item in basket) {
    console.log(item);
     if(item === lookingFor) {
      return `${lookingFor} is in your basket`;
    }     
   }
   return `${lookingFor} is not in your basket`;
}

</body>
</html>

I was given a task to raise an error ,but I am unable to do it .Please any One help me

CodePudding user response:

Use a try-and-catch block to handle errors appropriately and use the throw keyword to throw errors as shown below.

You can refer to this guide to understand Throw and Error

function checkBasket(basket, lookingFor) {
  try{
      if (Object.hasOwn(basket,lookingFor)){
        return `${lookingFor} is in your basket`;
      }else{
        throw new Error("Javascript For/in loop not working - Object property is not defined")
      }
  }catch(err){
      console.error(err)
      return err;
  }
   
}

CodePudding user response:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For In Loop</h2>
<p>The for in statement loops through the properties of an object:</p>

<p id="demo"></p>

<script>
const basket= {
  cup: 1,
  plate: 2,
  mug: 100
}

function checkBasket(basket, lookingFor) {
  for(item in basket) {
    console.log(item);
     if(item === lookingFor) {
      return `${lookingFor} is in your basket`;
    }     
   }
   return `${lookingFor} is not in your basket`;
}

console.log( checkBasket(basket, 'cup') ) ;

</script>
</body>
</html>

CodePudding user response:

call your function with parameter

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For In Loop</h2>
<p>The for in statement loops through the properties of an object:</p>

<p id="demo"></p>

<script>

const basket = {
    cup: 1,
    plate: 2,
    mug: 100
}

function checkBasket(basket, lookingFor) {
    for (item in basket) {
        console.log(item);
        if (item === lookingFor) {
            return `${lookingFor} is in your basket`;
        }
    }
    return `${lookingFor} is not in your basket`;
}

//call your function
checkBasket(basket, 'cup');

</script>

</body>
</html>

Just call your function with parameter

const basket= {
  cup: 1,
  plate: 2,
  mug: 100
}

//call your function with parameter after the basket object
checkBasket(basket, 'cup')

function checkBasket(basket, lookingFor) {
  for(item in basket) {
    console.log(item);
     if(item === lookingFor) {
      return `${lookingFor} is in your basket`;
    }     
   }
   return `${lookingFor} is not in your basket`;
}
 <!DOCTYPE html>
<html>
<body>

<h2>JavaScript For In Loop</h2>
<p>The for in statement loops through the properties of an object:</p>

<p id="demo"></p>

</body>
</html>

  • Related