Skip to content
Advertisement

how to print a single output in for loop?

How can I print only one output in for loop? If the number is in array then it will print "Present" but if the number is not in the array it will print "Nope". And I need to search the number the user inputed.

JavaScript

Input:

JavaScript

output:

JavaScript

Expected Output: Present

Advertisement

Answer

You need to maintain a variable outside the loop and print outside the loop using that variable as below:

JavaScript

Added Java 8 Solution:

If you are using Java 8 or above you can use Stream also to replace above for loop. As below :

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement