
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Converting numbers to base-7 representation in JavaScript
Like the base−2 representation (binary), where we repeatedly divide the base 10 (decimal) numbers by 2, in the base 7 system we will repeatedly divide the number by 7 to find the binary representation.
We are required to write a JavaScript function that takes in any number and finds its base 7 representation.
For example −
base7(100) = 202
Example
The code for this will be −
const num = 100; const base7 = (num = 0) => { let sign = num < 0 && '−' || ''; num = num * (sign + 1); let result = ''; while (num) { result = num % 7 + result; num = num / 7 ^ 0; }; return sign + result || "0"; }; console.log(base7(num));
Output
And the output in the console will be −
202
- Related Articles
- JavaScript algorithm for converting Roman numbers to decimal numbers
- Converting numbers to Indian currency using JavaScript
- Converting strings to numbers with vanilla JavaScript
- JavaScript algorithm for converting integers to roman numbers
- Converting array of Numbers to cumulative sum array in JavaScript
- Calculating 1s in binary representation of numbers in JavaScript
- Converting numbers into corresponding alphabets and characters using JavaScript
- Converting Strings to Numbers in C/C++
- Converting array to set in JavaScript
- Converting ASCII to hexadecimal in JavaScript
- Converting degree to radian in JavaScript
- Convert a number into negative base representation in C++
- Converting string to MORSE code in JavaScript
- Converting strings to snake case in JavaScript
- Converting string to an array in JavaScript

Advertisements