- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
Problem
Suppose a school organises this game on their Annual Day celebration −
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?
We are required to write a JavaScript function that takes in the number n and returns the number of open water taps.
Example
Following is the code −
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));
Output
7
- Related Articles
- Minimum Number of Taps to Open to Water a Garden in C++
- Finding smallest number using recursion in JavaScript
- Smallest number after removing n digits in JavaScript
- Finding next prime number to a given number using JavaScript
- Find the Number of Triangles After N Moves using C++
- Finding state after all collisions 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 minimum number of required operations to reach n from m in JavaScript
- Finding persistence of number in JavaScript
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Finding the largest 5 digit number within the input number using JavaScript
- Finding smallest sum after making transformations in JavaScript
- Finding one missing number in a scrambled sequence using JavaScript

Advertisements