
- 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
Finding confusing number within an array in JavaScript
Confusing Numbers:
A number in an array is confusing if it becomes another number which is also present in the array after we rotate the number by 180 degrees vertically and horizontally. For instance, if we rotate 6 by 180 degrees vertically and horizontally it becomes 9 and vice-versa.
We have to keep in mind that only rotations of 0, 1, 6, 8, 9 yield a valid number.
We are required to write a JavaScript function that takes in a natural number, num, as the first and the only argument. The function should first construct an array of all natural numbers upto num, including num.
For example, for num = 5, the array should be −
[1, 2, 3, 4, 5]
Then the function should count how many confusing numbers are present in the array and finally return that count.
For example −
If the input is −
const num = 10;
Then the output should be −
const output = 5;
because the array will be: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and confusing numbers are −
1, 6, 8, 9, 10
Example
The code for this will be −
const num = 10; const countConfusing = (num = 1) => { let count = 0; const valid = '01689'; const rotateMap = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}; const prepareRotation = num => { let res = ''; const numArr = String(num).split(''); if(numArr.some(el => !valid.includes(el))){ return false; }; numArr.map(el => { res = rotateMap[el] + res; }); return +res; }; for(let i = 1; i <= num; i++){ const rotated = prepareRotation(i); if(rotated && rotated > 0 && rotated <= num){ count++; }; }; return count; }; console.log(countConfusing(num));
Output
And the output in the console will be −
5
- Related Articles
- Finding the third maximum number within an array in JavaScript
- Finding unlike number in an array - JavaScript
- Number of vowels within an array in JavaScript
- JavaScript Finding the third maximum number in an array
- Finding the nth missing number from an array JavaScript
- Finding the largest non-repeating number in an array in JavaScript
- Finding the first non-consecutive number in an array in JavaScript
- Finding the largest 5 digit number within the input number using JavaScript
- Finding a number and its nth multiple in an array in JavaScript
- Confusing Number II in C++
- Counting possible APs within an array in JavaScript
- Find average of each array within an array in JavaScript
- Finding unique string in an array in JavaScript
- Finding two closest elements to a specific number in an array using JavaScript
- Find average of each array within an array JavaScript
