How to find any number that includes a certain digit - like 1323 includes the digit "2"
I'm learning JS and new to Stackoverflow, sorry if I'm wrong.
As you show on your comments, the first digit of userInput is important.
var userInput = 24;
var firstDigit = userInput.toString().substr(0,1);
var arr = [];
for (i = 0; i <= userInput; i++) {
arr.push(i);
if(firstDigit==='3') {
if (arr[i].toString().includes('3')) {
arr.splice(i,1,"I'm sorry, Dave. I'm afraid I can't do that.");
}
}
if(firstDigit==='2') {
if (arr[i].toString().includes('2')){
arr.splice(i,1,"Boob!");
}
}
if(firstDigit==='1') {
if (arr[i].toString().includes('1')){
arr.splice(i,1,"Beep!")
}
}
};
console.log(arr);
//[0, 1, "Boob!", 3, 4, 5, 6, 7, 8, 9, 10, 11, "Boob!", 13, 14, 15, 16, 17, 18, 19, "Boob!", "Boob!", "Boob!", "Boob!", "Boob!"]
I really hope that this is what you are looking for. Good luck!
edit:
You could do this too!
var userInput = 24;
var firstDigit = userInput.toString().substr(0,1);
var arr = [];
for (i = 0; i <= userInput; i++) {
arr.push(i);
};
newArr = arr.map(e => e.toString().includes(firstDigit) ? 'problem' : e);
console.log(newArr);
//[0, 1, "problem", 3, 4, 5, 6, 7, 8, 9, 10, 11, "problem", 13, 14, 15, 16, 17, 18, 19, "problem", "problem", "problem", "problem", "problem"]
Best way to check if a number contains another number
Convert to string and use indexOf
(752+'').indexOf('5') > -1
console.log((752+'').indexOf('5') > -1);console.log((752+'').indexOf('9') > -1);
Regular Expression: Consecutive Repetitions with a Letter In Between
If I understand correctly that w
is a wildcard (because I am not sure from what you have written - more by the examples you give) then this regex should work...
([1-9])(\1|w){2,}|w([1-9])(\3|w)+|ww([1-9])\5*
This is maybe not the most elegant solution, but should work, and breaks into parts like this...
# matches that don't start with wildcards
([1-9]) # get a digit, and capture for future reference
(\1|w){2,} # the captured digit, or the letter w, 2 or more times
| # or, matches that start with a single w
w # match a w
([1-9]) # get a digit, and capture for future reference
(\3|w)+ # get one or more of the captured digit, or letter w
| # or, matches starting with two w
ww # get two w
([1-9]) # get a digit, and capture for future reference
\5* # get all the successive captured digits
This should also work...
([1-9])(\1|w){2,}|w([0-9](\3|w)|w[0-9])
Create array of all N digit numbers whose sum of digit equals S
I might make the recursion a bit more explicit. To my mind there are a number of different base-cases, for various low values of n
and s
:
- if
s < 0
, then there are no results - if
s == 0
, then the only result is a string ofn
zeroes - if
n == 1
, then- if
s < 10
, then the only result is the digits
- otherwise, there are no results
- if
The recursive case involves taking each digit as a potential first digit, then joining it with each of the results involved in recursing, taking that amount from the total and using a digit count one smaller.
Here's an implementation of that:
const subsetSum = (n, s) => s < 0 ? [] : s == 0 ? ['0' .repeat (n)] : n == 1 ? s < 10 ? [String(s)] : [] : // else [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] .flatMap ( k => subsetSum (n - 1, s - k) .map (p => k + p) )
console .log ( subsetSum (3, 17) ) //~> ["089", "098", "179", "188", "197", "269", "278", ..., "971", "980"] (63 entries)
.as-console-wrapper {min-height: 100% !important; top: 0}
Find the dividend when remainder is given
The translation table doesn't show any duplicate, so no problem here:
n +7 %10
--------------
0 => 7 => 7
1 => 8 => 8
2 => 9 => 9
3 => 10 => 0
4 => 11 => 1
5 => 12 => 2
6 => 13 => 3
7 => 14 => 4
8 => 15 => 5
9 => 16 => 6
So to decrypt:
- unshuffle the digits
- subtract 7 on each digit and if the result is negative add 10 (or use a translation table)
How can I turn a large integer into an array of successive two-digit integers?
You could use the modulus here:
long j = 357642466853L;
long[] digArray = new long[(int)(Math.log10(j) / 2) + 1];
for (int i=digArray.length - 1; i >= 0; --i) {
digArray[i] = j % 100L;
j /= 100;
}
System.out.println(Arrays.toString(digArray));
This prints:
[35, 76, 42, 46, 68, 53]
Creating an array of all unique 4 digit numbers that can be made from the numbers 1-9 in JavaScript
You could take a recursive approach by iterating the items and check if the item has been chosen before. If not then take that item and check the length of the part array.
If that has the wanted length, puth the part array to the result.
If not, iterate the given array and hand over the part array.
function arrayCreate(array, size) { var result = []; array.forEach(function iter(parts) { return function (v) { var temp = parts.concat(v); if (parts.includes(v)) { return; } if (temp.length === size) { result.push(temp); return; } array.forEach(iter(temp)); } }([])); return result;}
console.log(arrayCreate([1, 2, 3, 4, 5, 6, 7, 8, 9], 4).map(a => a.join('')));console.log(arrayCreate([1, 2, 3, 4, 5, 6, 7, 8, 9], 5).map(a => a.join('')));console.log(arrayCreate([1, 2, 3, 4, 5, 6, 7, 8, 9], 6).map(a => a.join('')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related Topics
Input Date Format With Dd/Mm/Yyyy in the Input Field
React Router - Stay At the Same Page After Refresh
Creating a Json Dynamically With Each Input Value Using Jquery
Javascript - Auto Click on a Button on Page Load
How to Get Date Value from One Date Input Field and Put It to Other Date Field Using JavaScript
Check If a String Is a Date Value
How to Get a Div Element from an External Webpage in HTML File
How to Allow Only English Letters in Input Fields
How to Get Only First Character Match With Regex
Regular Expression to Accept Only Positive Numbers and Decimals
How to Reload a Page With React-Router
How to Convert Jquery Code to JavaScript
Break the Loop of an Array Looping Function (Map, Foreach, etc.)
How to Have Multiple Buttons of Same Id Value and When Click on Any Button the Pop-Up Should Come
How to Submit an HTML Form Without Redirection
Split a String Only the At the First N Occurrences of a Delimiter