Skip to content
Menu
TechFacts007.in
  • Information Zone
  • Reviews
    • Apps Reviews
    • PC’s
  • Gadgets
  • Hosting
    • Domain and Hosting
      • cPanel
    • WordPress
  • More
    • Tech Facts
    • Tips & Tricks
TechFacts007.in
November 26, 2021September 28, 2022

Write a function to take a positive integer and return the largest power of two less than or equal to this integer – JavaScript Program

Write a function to take a positive integer and return the largest power of two less than or equal to this integer – JavaScript Program.

 

A function that takes a positive integer and returns the largest power of two less than or equal to that integer.


Input: 5

Output: 4

 

Input: 16

Output: 16

 

Input: 10

Output: 8


 

Code:

function nearest_power_of_two(n) {
    let num = 1
    for (let i = 1; i < 100; i++) {
        if (num * 2 > number){
            break
        }
        else{
            num = num * 2
        }
    }
    return num
}

console.log("Answer is: " + nearest_power_of_two(5));

 

Output:

Answer is: 4

 

This code can be written as: In ES6  Syntax

const nearest_power_of_two = n => {
    let num = 1
    for (let i = 1; i < 100; i++) {
        if (num * 2 > number) break
        else num = num * 2
    }
    return num
}

console.log("Answer is: " + nearest_power_of_two(9));

 

The logic behind this code:

Declare a variable num with the value of 1. A loop will execute 99 times and calculate the power.

When the power is less than or equal to the given number, the variable num will overwrite by num * 2 and, if power is greater than the number then the loop will stop executing.

Finally, the function will return the variable num which stores the last value of power 2.

Post Views: 1,381

1 thought on “Write a function to take a positive integer and return the largest power of two less than or equal to this integer – JavaScript Program”

  1. kashish tomar says:
    September 17, 2022 at 2:38 PM

    i want this program solution in c++ and python 3

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Trending

  • Tech Facts
    52.376552,5.198303(Dead Body Found) Google Map
  • Information Zone
    Create Sitemap for React.Js Web App – Sitemap.xml for react app
  • Information Zone, Programming Languages, Tutorials
    React Hook To Check If User Has Changed/Minimize the Browser Tab
  • How to, Information Zone, Tips & Tricks
    Download any paid Software. Get any software free for lifetime
  • Codes, Programming Languages
    Find out if you can reach the last tile – Program Code
  • Codes, Programming Languages
    Write a function to take a positive integer and return the largest power of two less than or equal to this integer – JavaScript Program
  • How to, Information Zone, Tips & Tricks
    Encrypt Javascript and CSS Code – Hide Original Code from Users
  • PC's, Tips & Tricks
    Mount and Unmount Virtual Hard Disk (CMD Command and batch File Method)

Recent Posts

  • Worker Threads in JavaScript: Enhancing Performance with Parallelism July 31, 2023
  • Create Sitemap for React.Js Web App – Sitemap.xml for react app May 27, 2023
  • The Importance of Sitemap.xml: Enhancing Website Visibility and SEO May 27, 2023
  • Web Page Generation – Difference between SSR, CSR and SSG May 26, 2023
  • React.js vs. Next.js – Choosing the Right Framework for Your Web Development Needs May 26, 2023
  • NGINX vs. Apache: Exploring the Differences and Choosing the Right Web Server May 21, 2023
  • Setting Up NGINX on Ubuntu Server and Activating a Domain on NGINX Server May 21, 2023
  • Functional Component vs Class Component in React.Js January 18, 2023
  • React.Js Lifecycle Methods January 18, 2023
  • What is componentDidMount and useEffect with Example January 18, 2023

Categories

  • Apps Reviews (4)
  • Gadgets (4)
  • Hack With Us (2)
  • Hosting (12)
    • Domain and Hosting (7)
      • cPanel (5)
    • Wordpress (5)
  • How to (11)
  • Information Zone (65)
  • PC's (7)
  • Product Reviews (11)
  • Programming Languages (11)
    • Codes (5)
    • Tutorials (6)
  • Smart Phones (8)
  • Tech Facts (13)
  • Tips & Tricks (13)

Browse By Date

December 2023
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031
« Jul    

More

Contact Us
Privacy Policy
About Us
About Admin

Advertisement

Other Sites

ShanuTheWebDev.in
QrCode.ShanuTheWebDev.in
©2023 TechFacts007.in | Powered by WordPress and Superb Themes!