Show arrays arranged in a specific format - javascript

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"]

Related

Math Challenge print the next largest number after shuffling using 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

Increment/decrement numbers and letters by 'n'

I have a function encryptString(string, shift).
If encryptString('c', 1), then c becomes d.
if the arguments are encryptString('c', -1), then c becomes b.
If shift is greater than 26, or less than 0, the letter/number wraps back around. Meaning in encryptString('a', 29), a becomes `c.
I believe this could be solved using the charCode() functions, but I started using reduce, and would like to solve it with what I have.
My attempt below can shift letters and numbers, but only if they're within the array length. If shift is greater than 26, it doesn't work.
I'd appreciate any input I can get.
const encryptString = (str, shift) => {
if(!str.match(/^[a-z0-9]+$/i)) { throw new Error }
const caps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const letters = 'abcdefghijklmnopqrstuvwxyz'.split('');
const numbers = '123456789'.split('');
const reduction = str.split('').reduce((a,b) => {
caps.includes(b) ? b = caps[caps.indexOf(b) + shift] || caps[shift] : null;
letters.includes(b) ? b = letters[letters.indexOf(b) + shift] || letters[shift] : null;
numbers.includes(b) ? b = numbers[numbers.indexOf(b) + shift] || numbers[shift] : null;
a.push(b);
return a;
}, [])
return reduction.join('');
};
Some additional cases:
encryptString('JavaScript', 3) => 'MdydVfulsw' // passes
encryptString('mqblhw', -1) => 'lpakgv' // passes
encryptString('z956qau', 29) => 'c845tdx' //code above fails
encryptString('password123', -266) => 'jummqilx567' // code above fails

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));

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)

find median values from array in javascript (8 values or 9 values) [duplicate]

This question already has answers here:
Calculating median - javascript
(17 answers)
Closed 10 months ago.
How can i find median values from array in javascript
this is my array
var data = [
{ values: 4 },
{ values: 4 },
{ values: 4 },
{ values: 5 },
{ values: 2 },
{ values: 6 },
{ values: 6 },
{ values: 5 }
];
and i have tried this code
function findMedian(m) {
var middle = m.length - 1 / 2;
if (m.length - 1 % 2 == 1) {
return m[middle];
} else {
return (m[middle - 1] + m[middle]) / 2.0;
}
}
But it's return NaN value
My calculation formula is
Find the median of the data. Place the number of data in ascending order. Then, mark the place whose value we take into account when calculating the median.
This will do what you need - at the moment you've no logic to cope with reading the .values field out of each element of the array:
function findMedian(data) {
// extract the .values field and sort the resulting array
var m = data.map(function(v) {
return v.values;
}).sort(function(a, b) {
return a - b;
});
var middle = Math.floor((m.length - 1) / 2); // NB: operator precedence
if (m.length % 2) {
return m[middle];
} else {
return (m[middle] + m[middle + 1]) / 2.0;
}
}
EDIT I've padded out the code a bit compared to my original answer for readability, and included the (surprising to me) convention that the median of an even-length set be the average of the two elements either side of the middle.
For an array of numbers, e.g. [1,6,3,9,28,...]
// calculate the median
function median(arr){
arr.sort(function(a, b){ return a - b; });
var i = arr.length / 2;
return i % 1 == 0 ? (arr[i - 1] + arr[i]) / 2 : arr[Math.floor(i)];
}
What the code is doing:
Sort the numbers so that they are in order by value.
Find out the median's index in the array. If the array's length is even, the median is the average of the two numbers on either side of the index.
For arrays of odd length, it's easy to pluck out the middle number. But for arrays of even length, it's not. So, you test to find out whether your array is odd- or even-lengthed by finding out if dividing the array's length by two returns a whole number or not. If it's a whole number, that means the array's length is even, and you have to calculate the average of the two numbers on either side of the median.
In your question, your array can be flattened like so:
var myArray = data.map(function(d){ return d.values; });
And to get the median, use the function above like so:
var myMedian = median(myArray); // 4.5
Here about two things you have to be careful.
1) Operator precedence
When you are saying
var middle = m.length - 1 / 2;
It is same as
var middle = m.length - 0.5; //Because / has much precedence than -
So you should say
var middle = (m.length - 1) / 2;
Same problem with m.length - 1 % 2
2) You are not rounding middle so it's looking for decimal indexes in array. Which I think will return undefined.
In case you were wondering how to find median without using conditionals, here you are :)
Mind ES6.
/**
* Calculate median of array of numbers
* #param {Array<Number>} arr
* #return {Number}
*/
function median(arr) {
arr = [...arr].sort((a, b) => a - b);
return (arr[arr.length - 1 >> 1] + arr[arr.length >> 1]) / 2;
}
To answer the question:
median(data.map(x => x.values));
Here's a snippet that allows you to generate an array of numbers with either even or odd length. The snippet then sorts the array and calculates the median, finally printing the sorted array and the median.
(function() {
function makeNumArray(isEven) {
var randomNumArr = [];
var limit = isEven ? 8 : 9;
for (var i = 0; i < limit; i++) {
randomNumArr.push(Math.ceil(Math.random() * 10));
}
return randomNumArr;
}
function getMedian(arrOfNums) {
var result = [];
var sortedArr = arrOfNums.sort(function(num1, num2) {
return num1 - num2;
});
result.push("sortedArr is: [" + sortedArr.toString() + "]");
var medianIndex = Math.floor(sortedArr.length / 2);
if (arrOfNums.length % 2 === 0) {
result.push((sortedArr[medianIndex-1] + sortedArr[medianIndex]) / 2);
return result;
} else {
result.push(sortedArr[medianIndex]);
return result;
}
}
function printMedian(resultArr) {
var presentDiv = document.querySelector('#presentResult');
var stringInsert = '<div id="sortedArrDiv">' + resultArr[0].toString() + '<br>' + 'the median is: ' + resultArr[1].toString() + '</div>';
if (!document.querySelector('#sortedArrDiv')) {
presentDiv.insertAdjacentHTML('afterbegin', stringInsert);
} else {
document.querySelector('#sortedArrDiv').innerHTML = resultArr[0].toString() + "<br>" + 'the median is: ' + resultArr[1].toString();
}
};
function printEven() {
printMedian(getMedian(makeNumArray(1)));
}
function printOdd() {
printMedian(getMedian(makeNumArray(0)));
}
(document.querySelector("#doEven")).addEventListener('click', printEven, false);
(document.querySelector("#doOdd")).addEventListener('click', printOdd, false);
})();
#presentResult {
width: 70%;
margin: 2% 0;
padding: 2%;
border: solid black;
}
<h4>Calculate the median of an array of numbers with even (static length of 8) or odd (static length of 9) length. </h4>
<input type="button" value="Even length array of random nums" id="doEven">
<input type="button" value="Odd length array of random nums" id="doOdd">
<div id="presentResult"></div>
function median(a){
let arrlength = a.length;
if((arrlength%2) == 0){
let c = arrlength/2;
let b = (a[c]+a[c-1])/2;
return b;
}
else{
let e=Math.floor(arrlength/2);
return a[e];
}
}
median([1, 2, 10, 100]);
Try this simple one.
Findmedian(arr) {
arr = arr.sort(function(a, b){ return a - b; });
var i = arr.length / 2;
var result = i % 1 == 0 ? parseInt((arr[i - 1] + arr[i]) / 2) + ',' +
parseInt(arr[i]) : arr[Math.floor(i)];
return result;
}
it returns for odd number of array elements.

Categories

Resources