

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding number of open water taps after n chances using JavaScript
<h2>Problem</h2><p>Suppose a school organises this game on their Annual Day celebration −</p><p>There are ”n” water taps and “n” students are chosen at random. The instructor asks the first student to go to every tap and open it. Then he has the second student go to every second tap and close it. The third goes to every third tap and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth tap, and so on. After the process is completed with the "n"th student, how many taps are open?</p><p>We are required to write a JavaScript function that takes in the number n and returns the number of open water taps.</p><h2>Example</h2><p>Following is the code −</p><p><a class="demo" href="http://tpcg.io/0p4wDNg1" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notransalte">const num = 15; const openTaps = (num = 1) => { const arr = []; let index = 1; while(index ** 2 <= num){ arr.push(index++ ** 2); }; return arr.length; }; console.log(openTaps(num));</pre><h2>Output</h2><pre class="result notransalte">7</pre>
- Related Questions & Answers
- Minimum Number of Taps to Open to Water a Garden in C++
- Smallest number after removing n digits in JavaScript
- Find the Number of Triangles After N Moves using C++
- Finding smallest number using recursion in JavaScript
- Finding persistence of number in JavaScript
- Finding minimum number of required operations to reach n from m in JavaScript
- Finding state after all collisions in JavaScript
- Finding next prime number to a given number using JavaScript
- Finding product of Number digits in JavaScript
- Finding square root of a number without using library functions - JavaScript
- Finding square root of a number without using Math.sqrt() in JavaScript
- Finding smallest sum after making transformations in JavaScript
- Finding Gapful number in JavaScript
- Finding next n leap years in JavaScript
- Finding average of n top marks of each student in JavaScript
Advertisements