Math Challenge print the next largest number after shuffling using javascript - javascript

Have the function nextLargest(num) take the num parameter being passed and return the next number greater than num using the same digits. For example: if num is 123 return 132, if it's 12453 return 12534. If a number has no greater permutations, return -1 (ie. 999).
Examples
Input: 11121
Output: 11211
Input: 41352
Output: 41523
var permute = (function () {
return permute;
function permute(list) {
return list.length ?
list.reduce(permutate, []) :
[[]];
}
function permutate(permutations, item, index, list) {
return permutations.concat(permute(
list.slice(0, index).concat(
list.slice(index + 1)))
.map(concat ,[item]));
}
function concat(list) {
return this.concat(list);
}
}());
console.log(JSON.stringify(permute([1,2,3,4])));
my output : [[1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[1,4,3,2],[2,1,3,4],[2,1,4,3],[2,3,1,4],[2,3,4,1],[2,4,1,3],[2,4,3,1],[3,1,2,4],[3,1,4,2],[3,2,1,4],[3,2,4,1],[3,4,1,2],[3,4,2,1],[4,1,2,3],[4,1,3,2],[4,2,1,3],[4,2,3,1],[4,3,1,2],[4,3,2,1]]
expected output :
Input: 11121
Output: 11211
Input: 41352
Output: 41523

Assuming the permute function as given that you shared in your question, the following straightforward approach should solve the question.
It is not very efficient, however. Probably, a better strategy could be found working with a cleverly chosen sequence of transpositions only.
function nextLargest(input) {
let x = input.toString();
let p = permute([...x]);
let result = Infinity;
for (let n of p) {
let y = n.join('')*1;
if (y > input && y < result) result = y;
}
return result < Infinity ? result : -1;
}
console.log(nextLargest(1234)); // <<< gives 1243

Related

How could i write out each individual number as well as the asterisk symbol when calculating the factorial of a number?

I'm stuck on a problem that requires me to display the full workings of a factorial function, for example, if the user wanted to workout 6!, i would need to display: 6 * 5 * 4 * 3 * 2 * 1 = 720. Would i need to use an array for such?
This is what i have so far in order to workout the factorized value of any user given number, although this only outputs the final value, and not the fully expanded working out as i have shown above:
(the variable number contains the user input);
var f = [];
function factorizeFunction(number) { //this is the function that does the factorization calculations
if (number == 0 || number == 1)
return 1;
if (f[number] > 0)
return f[number];
return f[number] = factorizeFunction(number-1) * number;
}
document.getElementById("factorialTest").innerHTML = factorizeFunction(number);
any help on this would be appreciated!
One option is, on each iteration, push to an array which is passed down through the recursive call (or created on the initial call). At the end, return the array, joined by *, and also the sum of the array:
function factorizeFunction(number, arr = []) { //this is the function that does the factorization calculations
if (number == 0 || number == 1) arr.push(number);
else {
arr.push(number);
factorizeFunction(number - 1, arr);
}
return arr.join(' * ') + ' = ' + arr.reduce((a, b) => a * b, 1);
}
document.getElementById("factorialTest").innerHTML = factorizeFunction(5);
<div id="factorialTest"></div>
Use map and join methods.
const factorString = num => {
const nums = new Array(num).fill(0).map((_, i) => num - i);
let res = 1;
nums.forEach(x => res *= x);
return `${nums.join(' * ')} = ${res}`;
}
console.log(factorString(6))
You could change the return signature of the function and expect an array of an array with the factors and the product.
function factorize(number) {
if (number === 0 || number === 1) return [[1], 1];
var [factors, product] = factorize(number - 1);
return [[...factors, number], product * number];
}
console.log(factorize(5));

Adding two numbers JS

I want to add two numbers from range 10-99,for example:
Input:16
Output:1+6=7
Input:99
Output:18
function digital_root(n) {
var z = n.toString().length;
if (z == 2) {
var x = z[0] + z[1]
return x;
}
}
console.log( digital_root(16) );
Output from this code is NaN.What should I correct?
You can try this:
function digital_root(n) {
var z = n.toString();
//use length here
if (z.length == 2) {
//convert to int
var x = parseInt(z[0]) + parseInt(z[1]);
return x;
} else {
return "not possible!";
}
}
console.log( digital_root(16) );
console.log( digital_root(99) );
console.log( digital_root(999) );
Use split to split the string in half and add the two using parseInt to convert to a number.
const sum = (s) => (''+s).split('').reduce((a,b) => parseInt(a)+parseInt(b))
↑ ↑ ↑ ↑
our coerce split sum
function to string in two both
Here a test :
const sum = (s) => (''+s).split('').reduce((a,b) => parseInt(a)+parseInt(b))
console.log(sum(12))
There are several approaches to sum digits of a number. You can convert it to a string but IDK if thats neccesary at all. You can do it with numerical operations.
var input = 2568,
sum = 0;
while (input) {
sum += input % 10;
input = Math.floor(input / 10);
}
console.log(sum);
Here's a fun short way to do it:
const number = 99
const temp = number.toString().split('')
const res = temp.reduce((a, c) => a + parseInt(c), 0) // 18
1.) Convert number to string
2.) Separate into individual numbers
3.) Use reduce to sum the numbers.
Your way would be the iterational way to solve this problem, but you can also use a recursive way.
Iterative solution (Imperative)
n.toString() Create String from number.
.split("") split string into chars.
.reduce(callback, startValue) reduces an array to a single value by applying the callback function to every element and updating the startValue.
(s, d) => s + parseInt(d) callback function which parses the element to an integer and adds it to s (the startValue).
0 startValue.
Recursive solution (Functional)
condition?then:else short-hand if notation.
n<10 only one digit => just return it.
n%10 the last digit of the current number (1234%10 = 4).
digital_root_recurse(...) call the function recursivly.
Math.floor(n / 10) Divide by 10 => shift dcimal point to left (1234 => 123)
... + ... add the last digit and the return value (digital root) of n/10 (1234 => 4 + root(123)).
function digital_root_string(n) {
return n.toString().split("").reduce((s, d) => s + parseInt(d), 0);
}
function digital_root_recurse(n) {
return n < 10 ? n : n % 10 + digital_root_recurse(Math.floor(n / 10));
}
console.log(digital_root_string(16));
console.log(digital_root_string(99));
console.log(digital_root_recurse(16));
console.log(digital_root_recurse(99));
The issue in your code is that you stored the length of n into z. The length is an integer, so both z[0] and [1] are undefined. The solution is to store the string into another variable and use that instead of z.
function digital_root(n) {
n = n.toString();
var l = n.length;
if (l === 2) {
return parseInt(n[0], 10) + parseInt(n[1], 10);
}
}
console.log( digital_root(16) );
Simply use var x = parseInt(n/10) + (n%10); and it will work for you.
function digital_root(n) {
var z = n.toString().length;
if (z == 2) {
var x = parseInt(n/10) + (n%10);
return x;
}
}
console.log( digital_root(16) );
console.log( digital_root(99) );
console.log( digital_root(62) );
Convert input to string, split it, convert each item back to number and sum them all:
function digital_root(n) {
return String(n).split('').map(Number).reduce((a,b) => a + b)
}
const result = digital_root(99);
console.log(result);

Clearing a counter after each function call: JavaScript Recursive function

I have the folllowing solution to a problem relating to multiplicative persistence. However, I need to wipe the counter after each function call.
I have tried different return statements, counters and arrays.
I don't seem to be able to clear the counter after each function call AND
get the correct answer. It is adding all the answers from multiple function calls.
function persistence(num, counter = 0) {
if (num.toString().length != 1) {
num = num.toString().split("").filter(Number).reduce((a, b) => a * b);
persistence(num, ++counter);
}
return counter;
}
persistence(999) // Answer should be 4.
persistence(25)// Answer should be 2 not 6 or 1.
The tests here:
describe('Initial Tests', function () {
Test.assertEquals(persistence(39),3);
Test.assertEquals(persistence(4),0);
Test.assertEquals(persistence(25),2);
Test.assertEquals(persistence(999),4);
});
You need to return the result of each recursive call and handle the else case.
Try this:
function persistence(num, counter = 0) {
if (num.toString().length != 1) {
num = num.toString().split("").filter(Number).reduce((a, b) => a * b);
return persistence(num, ++counter);
} else {
return counter;
}
}
Here are the results from console:
> persistence(25)
< 2
> persistence(999)
< 4
I'm assuming you're trying to compute multiplicative digital root but that does not remove zeroes from the computation as you're doing with .filter(Number) above. Below, we write multiplicativeRoot which returns an array of the steps it takes to reduce a number to a single digit
Finally, the multiplicative persistence can be computed by simply counting the number of steps in the return value from multiplicativeRoot and subtracting 1 (the first value in the result is always the input value)
The result is an implementation of multiplicativePersistence that is made up of several functions, each with their distinct and clear purpose
const digits = n =>
n < 10
? [ n ]
: digits (n / 10 >> 0) .concat ([ n % 10 ])
const mult = (x,y) =>
x * y
const product = xs =>
xs.reduce (mult, 1)
const multiplicativeRoot = x =>
x < 10
? [ x ]
: [ x ] .concat (multiplicativeRoot (product (digits (x))))
const multiplicativePersistence = x =>
multiplicativeRoot (x) .length - 1
console.log (multiplicativeRoot (999)) // [ 999, 729, 126, 12, 2 ]
console.log (multiplicativePersistence (999)) // 4
console.log (multiplicativeRoot (25)) // [ 25, 10, 0 ]
console.log (multiplicativePersistence (25)) // 2

JavaScript: How to reverse a number?

Below is my source code to reverse (as in a mirror) the given number.
I need to reverse the number using the reverse method of arrays.
<script>
var a = prompt("Enter a value");
var b, sum = 0;
var z = a;
while(a > 0)
{
b = a % 10;
sum = sum * 10 + b;
a = parseInt(a / 10);
}
alert(sum);
</script>
Low-level integer numbers reversing:
function flipInt(n){
var digit, result = 0
while( n ){
digit = n % 10 // Get right-most digit. Ex. 123/10 → 12.3 → 3
result = (result * 10) + digit // Ex. 123 → 1230 + 4 → 1234
n = n/10|0 // Remove right-most digit. Ex. 123 → 12.3 → 12
}
return result
}
// Usage:
alert(
"Reversed number: " + flipInt( +prompt("Enter a value") )
)
The above code uses bitwise operators for quick math
This method is MUCH FASTER than other methods which convert the number to an Array and then reverse it and join it again. This is a low-level blazing-fast solution.
Illustration table:
const delay = (ms = 1000) => new Promise(res => setTimeout(res, ms))
const table = document.querySelector('tbody')
async function printLine(s1, s2, op){
table.innerHTML += `<tr>
<td>${s1}</td>
<td>${s2||''}</td>
</tr>`
}
async function steps(){
printLine(123)
await delay()
printLine('12.3 →')
await delay()
printLine(12, 3)
await delay()
printLine('1.2', '3 × 10')
await delay()
printLine('1.2 →', 30)
await delay()
printLine(1, 32)
await delay()
printLine(1, '32 × 10')
await delay()
printLine('1 →', 320)
await delay()
printLine('', 321)
await delay()
}
steps()
table{ width: 200px; }
td {
border: 1px dotted #999;
}
<table>
<thead>
<tr>
<th>Current</th>
<th>Output</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Assuming #DominicTobias is correct, you can use this:
console.log(
+prompt("Enter a value").split("").reverse().join("")
)
I was recently asked how to solve this problem and this was my initial solution:
The desired output: 123 => 321, -15 => -51, 500 => 5
function revInt(num) {
// Use toString() to convert it into a String
// Use the split() method to return a new array: -123 => ['-', '1','2','3']
// Use the reverse() method to reverse the new created array: ['-', '1','2','3'] => ['3','2','1','-'];
// Use the join() method to join all elements of the array into a string
let val = num.toString().split('').reverse().join('');
// If the entered number was negative, then that '-' would be the last character in
// our newly created String, but we don't want that, instead what we want is
// for it to be the first one. So, this was the solution from the top of my head.
// The endsWith() method determines whether a string ends with the characters of a specified string
if (val.endsWith('-')) {
val = '-' + val;
return parseInt(val);
}
return parseInt(val);
}
console.log(revInt(-123));
A way better solution:
After I gave it some more thought, I came up with the following:
// Here we're converting the result of the same functions used in the above example to
// an Integer and multiplying it by the value returned from the Math.sign() function.
// NOTE: The Math.sign() function returns either a positive or negative +/- 1,
// indicating the sign of a number passed into the argument.
function reverseInt(n) {
return parseInt(n.toString().split('').reverse().join('')) * Math.sign(n)
}
console.log(reverseInt(-123));
NOTE: The 2nd solution is much more straightforward, IMHO
This is my solution, pure JS without predefined functions.
function reverseNum(number) {
var result = 0,
counter = 0;
for (i = number; i >= 1 - Number.EPSILON; i = i / 10 - (i % 10) * 0.1) {
counter = i % 10;
result = result * 10 + counter;
}
return result;
}
console.log(reverseNum(547793));
Firstly, I don't think you are using an array to store the number. You are using a java script variable.
Try out this code and see if it works.
var a = prompt("Enter a value");
var z = a;
var reverse = 0;
while(z > 0)
{
var digit = z % 10;
reverse = (reverse * 10) + digit;
z = parseInt(z / 10);
}
alert("reverse = " + reverse);
Or, as a one-liner ( x contains the integer number to be inversed):
revX=x.toFixed(0).split('').reverse().join('')-0;
The number will be separated into its individual digits, reversed and then reassembled again into a string. The -0 then converts it into a number again.
Explanation
Using the JavaScript reverse() array method you can reverse the order of the array elements.
Code
var a = prompt("Enter a value");
var arr = [];
for (var i = 0; i < a.length; i++) {
arr[i] = a.charAt(i);
}
arr.reverse();
alert(arr);
Assuming you may want to reverse it as a true number and not a string try the following:
function reverseNumber(num){
num = num + '';
let reversedText = num.split('').reverse().join('');
let reversedNumber = parseInt(reversedText, 10);
console.log("reversed number: ", reversedNumber);
return reversedNumber;
}
Using JavaScript reverse() and Math.sign() you can reverse a number both positive and negative numbers.
var enteredNum = prompt("Enter integer");
function reverseInteger(enteredNum) {
const reveredNumber = enteredNum.toString().split('').reverse().join('');
return parseInt(reveredNumber)*Math.sign(enteredNum);
}
alert(reverseInteger(enteredNum));
function add( num:number){ //159
let d : number;
let a : number =0;
while(num > 0){ //159 15 1
d = num % 10;
a = a * 10 + d; //9 95 951
num = Math.floor(num/10); // 15 1 0
}
return a; //951
}
console.log(add(159));
Reversing a number without converting it into the string using the recursive approach.
const num = 4578;
const by10 = (num) => {
return Math.floor(num / 10);
};
const remBy10 = (num) => {
return Math.floor(num % 10);
};
const reverseNum = (num, str = "") => {
if (num.toString().length == 1) return (str += num);
return reverseNum(by10(num), (str += remBy10(num)));
};
console.log(reverseNum(num, ""));
The simplest solution is to reverse any integer in js. Doesn't work with float.
const i2a = number.toString().split("");
const a2i = parseInt(i2a.reverse().join(""));
console.log(a2i);
Apply logic of reversing number in paper and try, and you have to care about dividing because it gives float values. That's why we have to use parseInt().
function palindrome()
{
var a = document.getElementById('str').value;
var r=0 ,t=0;
while(a>0){
r=a%10;
t=t*10+r;
a=parseInt(a/10);
}
document.write(t);
}
<form>
<input type="text" id="str"/>
<input type="submit" onClick="palindrome()" />
<form>
var reverse = function(x) {
if (x > 2147483647 || x < -2147483648 || x === 0) {
return 0;
}
let isNegative = false;
if(x < 0){
isNegative = true;
x = -x;
}
const length = parseInt(Math.log10(x));
let final = 0;
let digit = x;
let mul = 0;
for(let i = length ; i >= 0; i--){
digit = parseInt(x / (10**i));
mul = 10**(length-i);
final = final + digit * mul;
x = parseInt(x % 10**i);
}
if (final > 2147483647 || final < -2147483648 ) {
return 0;
}
if(isNegative){
return -final;
}
else{
return final;
}
};
console.log(reverse(1534236469));
console.log(reverse(-123));
console.log(reverse(120));
console.log(reverse(0));
console.log(reverse(2,147,483,648));
function reverseInt(n) {
let x = n.toString();
let y = '';
for(let i of x) {
y = i + y
}
return parseInt(y) * Math.sign(n);
}
Sweet and simple:
function reverseNumber(num){
return parseInt(num.toString().split("").reverse().join(""));
}
The above code will not work for negative numbers. Instead, use the following:
/**
* #param {number} x
* #return {boolean}
*/
var isPalindrome = function(x) {
return ((x>=0) ? ((x==(x = parseInt(x.toString().split("").reverse().join("")))) ? true:false) : false);
};
The simplest way is to
Covert it into a string and apply the reverse() method
Change it back to number
Check for the value provided if negative or positive with Math.sign()
Below is my solution to that.
function reverseInt(n) {
const reversed =
n.toString().split('').reverse().join('');
return parseInt(reversed) * Math.sign(n);
}
console.log(reverseInt(12345));
My solution to reverse a string:
var text = ""
var i = 0
var array = ["1", "2", "3"]
var number = array.length
var arrayFinal = []
for (i = 0; i < array.length; i++) {
text = array[number - 1]
arrayFinal.push(text)
text = ""
number = number - 1
}
console.log(arrayFinal)

Show arrays arranged in a specific format

I have the following array in JavaScript:
myArray = ["lu9","lu10","lu11","ma9","ma10","ma11","mi9","mi10","mi11"];
Then, I need to display the values ​​(for example in an alert) but must be arranged as follows:
"lu9,ma9,mi9,lu10,ma10,mi10,lu11,ma11,mi11"
How I can do this?
Each item in your list has two parts: The leading mishmash of characters (mi, ma, lu) and a numerical suffix. To properly sort, we have to take both into account.
array.sort(function sorter (a, b) {
var re = /^(\D+)(\d+)$/,
left = re.exec(a),
right = re.exec(b);
if (left[1] === right[1]) {
return Number(left[2]) - Number(right[2]);
}
return left[1] < right[1] ? -1 : 1;
});
Let's say a = lu9 and b = lu10:
1. left = ['lu9', 'lu', '9']
2. right = ['lu10', 'lu', '10']
3. left[1] === right[1]
1. Number(left[2]) = 9
2. Number(right[2]) = 10
3. return 9 - 10 (negative number, a before b)
Now if our input is a = lu9 and b = mi4:
1. left = ['lu9', 'lu', '9']
2. right = ['mi4', 'mi', '4']
3. left[1] !== right[1]
1. left[1] < right[1] = true
2. return -1
var myArray = ["lu9", "lu10", "lu11", "ma9", "ma10", "ma11", "mi9", "mi10", "mi11"];
function myResult(myArray) {
myArray = myArray.slice().sort(function (a, b) {
var reg = /\d+/ //A regex to extract the numerical part
var num = 2 * (+a.match(reg) - +b.match(reg)) //Put a weight of 2 on the numerical value
var str = a > b ? 1 : a < b ? -1 : 0 //The strings value with a single weight
return num + str //add them and we have a positive or negative value with a correct weight on the numerical part
})
return "" + myArray
}
console.log (myResult(myArray)) //"lu9,ma9,mi9,lu10,ma10,mi10,lu11,ma11,mi11"
Heres a Fiddle
var myArray = ["lu9","lu10","lu11","ma9","ma10","ma11","mi9","mi10","mi11"];
var derp = function(a, b) {
a = a.replace(/[^0-9]+/g, '', a);
b = b.replace(/[^0-9]+/g, '', b);
return a < b;
}
myArray.sort(derp);
We need to sort first by number and then by letters.
no need for regex here.
We will use padding:
so ma11 will be 0011ma
and mi11 will be 0011mi
and ma11 will be 0011ma
(and mi9 will be 0009mi , the padding helps 11 to be bigger then 2 as string)
so sorting it now - will yield the right result.
var a = ["ma9", "ma10", "ma11", "mi9", "mi10", "mi11", "lu9", "lu10", "lu11"]
a.sort(function (a, b)
{
return calc(a) > calc(b);
});
function calc(x)
{
return ("0000" + x.slice(2)).slice(-4) + x.slice(0,2);
}
result :
["ma9", "ma10", "ma11", "mi9", "mi10", "mi11", "lu9", "lu10", "lu11"]

Categories

Resources