Split string every 3 characters from back using JavaScript - javascript

How do we split a string every 3 characters from the back using JavaScript?
Say, I have this:
str = 9139328238
after the desired function, it would become:
parts = ['9','139','328','238']
How do we do this elegantly?

var myString = String( 9139328238 );
console.log( myString.split( /(?=(?:...)*$)/ ) );
// ["9", "139", "328", "238"]
I can't make any performance guarantees. For smallish strings it should be fine.
Here's a loop implementation:
function funkyStringSplit( s )
{
var i = s.length % 3;
var parts = i ? [ s.substr( 0, i ) ] : [];
for( ; i < s.length ; i += 3 )
{
parts.push( s.substr( i, 3 ) );
}
return parts;
}

I know this is an old question, but I would like to provide my own one-line version to solve the problem :)
"12345678".split('').reverse().join('').match(/.{1,3}/g).map(function(x){
return x.split('').reverse().join('')
}).reverse()
This basically reverses the string, captures the groups of 3 elements, reverses each group and then reverses the whole string.
The steps are:
"12345678" -> [1, 2, 3, 4, 5, 6, 7, 8] //.split('')
[1, 2, 3, 4, 5, 6, 7, 8] -> [8, 7, 6, 5, 4, 3, 2, 1] //.reverse()
[8, 7, 6, 5, 4, 3, 2, 1] -> "87654321" //.join('')
"87654321" -> [876, 543, 21] //.match(...)
[876, 543, 21] -> [678, 345, 12] //.map(function(x){...})
[678, 345, 12] -> [12, 345, 678] //.reverse()
You can then join the array with a character (e.g. the ',' for thousands separator)
[12, 345, 678].join(',') -> "12,345,678"

There are a lot of complicated answers here.
function group(value) {
return value.match(/\d{1,3}(?=(\d{3})*$)/g);
}
console.log(group('1'));
console.log(group('123'));
console.log(group('1234'));
console.log(group('12345'));
console.log(group('123456'));
console.log(group('1234567'));
console.log(group('12345678'));
console.log(group('123456789'));

Not as elegant, but shows you a while loop
function commaSeparateNumber (val) {
val = val.toString();
while (/(\d+)(\d{3})/.test(val)){
val = val.replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
var str = "9139328238";
var splitStr = commaSeparateNumber(str).split(",");
console.log(splitStr);

Try this:
var str = 9139328238 + ''; //convert int to string
var reqArr = []; // required array
var len = str.length; //maintaining length
while (len > 0) {
len -= 3;
reqArr.unshift(str.slice(len)); //inserting value to required array
str = str.slice(0, len); //updating string
}
Hope it helps..

Since regex operations are not liked by everyone for various reasons: here is a regular function using a regular loop to split any regular string every X characters from back. Nothing fancy but it works:
function splitStringFromEnd(customString, every) {
var result = [], counter = every;
// loop that captures substring chungs of "every" length e.g.: 1000.00 -> ["000", ".00"]
for (var i = counter; counter <= customString.length; counter += every) {
result.unshift(customString.substr(customString.length - counter, every))
}
// check if there is a remainder and grabs it.
// Using our 1000.00 example: diff = 9 - 7; remainder = 3 - 2; -> ["1", "000", ".00"]
var diff = counter - customString.length;
var remainder = every - diff;
if(remainder > 0) { result.unshift(customString.substr(0, remainder)) }
return result;
}
for your example it would be:
splitStringFromEnd("9139328238", 3);
// :returns => ["9", "139", "328", "238"]
Enjoy :)

const price_format = (price) => {
let result = [];
let new_str = [...price].reverse().join("");
let rightSplit = new_str.match(/.{1,3}/g).reverse();
for (let item of rightSplit) {
result.push([...item].reverse().join(""));
}
return result.join(",");
}
let price = "2560000000";
console.log(price_format(price));
// output : 2,560,000,000

"12345678".split('').reverse().reduce((a, s) => (a[0].length<3?a[0]=s+a[0]:a.unshift(s),a), ['']);

Finally it seems good. This is what I have got till now without using any loops
function breakAt3(x)
{
if(x.length < 3){ var parts = [x]; return parts; }
var startPos = (x.length % 3);
var newStr = x.substr(startPos);
var remainingStr = x.substr(0,startPos);
var parts = newStr.match(/.{1,3}/g);
if(remainingStr != ''){ var length = parts.unshift(remainingStr); }
return parts;
}
var str = '92183213081';
var result = breakAt3(str); // 92,183,213,081

Related

JS get max first n values from a string

I need to capture maximum 5 elements of a string. However if there are less than 5, then I just need how many there are there.
var y = '1,2,3,4,5,6,7,8,9,10'
//desired result:
var out = '1,2,3,4,5' // please note there is no trailing comma
var y = '1,2,3'
//desired result:
var out = '1,2,3'
My code:
for (var i = 0; i < 5; i++) {
x += y;
x = x + ",";
}
Write(x);
A simple method will do. The below splits the string by , then takes either n elements or the total length if it is less than n and then rejoins the values with a comma
const getNValues = (str, n) => {
const values = str.split(",");
const res = values.slice(0, Math.min(values.length,n))
return res.join(",");
}
console.log(getNValues("1,2,3,4,5,6,7,8,9",5));
console.log(getNValues("1,2,3",5));
Try this simple function to do that
function getMaxLen(str) {
if(typeof str != 'string') return null;
str = str.split(',');
return str.slice(0, 5).join(',');
}
console.log(getMaxLen('1,2,3,4,5,6,7,8,9,10'))
console.log(getMaxLen('1,2,3'))
var string = '1, 2, 3, 4, 5, 6, 7, 8, 9, 10';
var out = (string.match(/^([0-9],? ?){0,5}/)[0] || '').replace(/, ?$/, '');
console.log(out)
[EDIT] Explanation
.match(^([0-9],? ?){0,5}/g):
start at the begging ^
match numbers [0-9]
then a comma if any and a space if any ,? ?.
match this expression 0 to 5 times {0, 5}

How do I write a function that returns the first 5 positive even numbers?

var myArr = [1,2,3,4,5,6,7,8,9,10];
function even(num) {
var newArr = [];
for (var i=1; i<num.length; i++) {
if (num[i] % 2 === 0) {
newArr.push(num[i]);
}
}
return newArr;
}
console.log(even(myArr));
My function throws an exception when called. How can I rewrite or refactor the above code to return the first 5 positive numbers?
You can create it this way.
var myArr = [1,2,0,3,4,-6,5,-3,88,21,-6,5,6,7,8,9,10];
let evens = myArr.filter(x => x > 0 && x % 2 == 0).slice(0, 5);
console.log(evens)
First off, your code appears to work. Can you give an example of the error that is occurring? Second off, if you want a function that returns an array of the first n positive, even integers, you can write something like this.
function firstEven(count) {
var response = []; // Create the response list
for(var i=0;i<count;i++) { // Loop for number of even numbers you want
response.push((i + 1) * 2); // *2 skips every two numbers, and +1 shifts the number to even
}
return response
}
However, if you want to just filter out all odd numbers from an array, you can do the following.
var myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var myEvens = myArr.filter(function(myNum) { // Filter runs the function for every value in the array, and (if the function returns false) it removed that value
return (myNum % 2) == 0;
});
Feel free to ask if you have any questions!
2 others differnts ways
const myArr = [1,2,3,4,-6,5,-3,88,21,-6,5,6,7,8,9,10];
const even_1 = arr => arr.filter(x=>(x>=0 && !(x&1))).slice(0,5)
const even_2 = arr =>
{
let r = []
for(x of arr)
if (x>=0 && !(x&1)) // test2 = boolean AND on bit zero
{
r.push(x);
if (r.length >= 5) break;
}
return r
}
console.log('even 1:', even_1(myArr).join(','))
console.log('even 2:', even_2(myArr).join(','))
One suggestion:
var myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
function even(numbersArray) {
var first5EvenNums = [];
for (const num of numbersArray) {
if (first5EvenNums.length >= 5) break;
if (num % 2 === 0) {
first5EvenNums.push(num);
}
}
return first5EvenNums;
}
console.log(even(myArr));
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div id="test"></div>
<script>
var myArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
function even(num){
var newArr = [];
for (var i=1; i<num.length; i++){
if (num[i] % 2 === 0)
{
newArr.push(num[i]);
if(newArr.length == 5){
return newArr;
}
}
}
return newArr;
};
$("#test").text(even(myArr));
</script>
This return first 5 positive number in array.

Count number of values in array between two input values

As the title suggests, I want to create a function the counts the number of values in my array between two values that have been entered by the user. So for example, if the array was [1, 4, 6, 7, 8, 6] and the user entered 5 as their first value and 7 as their second value, they would be greeted with an alert that said
"total number of values = 3".
You can create an extremely clean solution to this problem by utilizing the second property of Array#filter (which sets the this binding given to your callback of choice):
var array = [1, 4, 6, 7, 8, 6]
function inRange (x) {
return this[0] <= x && x <= this[1]
}
var result = array.filter(inRange, [5, 7]).length
console.log('Total number of values:', result)
All you need is a simple for loop.
var total = 0;
var num1 = 5;
var num2 = 7;
var array = [1,4,6,7,8,6];
for(var a = 0; a < array.length; a++) {
if(array[a] >= num1 && array[a] <= num2) {
total++;
}
}
alert("Total numbers of values = " + total);
This will loop through the array, detect nums within the range, tally up a value, and output it in a alert.
You can use Array.prototype.filter(), RegExp.prototype.test() with RegExp constructor with class from-to, where from is 5, to is 7, get .length of resulting array
var from = 5;
var to = 7;
var len = arr.filter(RegExp.prototype.test.bind(new RegExp(`[${from}-${to}]`))).length;
You can alternatively use .toString(), .match()
var arr = [1,4,6,7,8,6];
var from = 5;
var to = 7;
var res = arr.toString().match(new RegExp(`[${from}-${to}]`, "g"));
var len = res.length;
console.log(res.length);
You may do as follows;
var arr = [1,4,6,7,8,6],
input = [5,7],
result = arr.reduce((r,n) => n >= input[0] && n <= input[1] ? ++r : r, 0);
console.log(result);
var array = [1, 4, 6, 7, 8, 6];
function countNumber(arr,a,b){
let count = 0;
for (const number of arr){
if (number >= a && number <= b){
count ++;
}
}
return count;
}
console.log(countNumber(array, 5, 7));

How can I reverse an array in JavaScript without using libraries?

I am saving some data in order using arrays, and I want to add a function that the user can reverse the list. I can't think of any possible method, so if anybody knows how, please help.
Javascript has a reverse() method that you can call in an array
var a = [3,5,7,8];
a.reverse(); // 8 7 5 3
Not sure if that's what you mean by 'libraries you can't use', I'm guessing something to do with practice. If that's the case, you can implement your own version of .reverse()
function reverseArr(input) {
var ret = new Array;
for(var i = input.length-1; i >= 0; i--) {
ret.push(input[i]);
}
return ret;
}
var a = [3,5,7,8]
var b = reverseArr(a);
Do note that the built-in .reverse() method operates on the original array, thus you don't need to reassign a.
Array.prototype.reverse() is all you need to do this work. See compatibility table.
var myArray = [20, 40, 80, 100];
var revMyArr = [].concat(myArray).reverse();
console.log(revMyArr);
// [100, 80, 40, 20]
Heres a functional way to do it.
const array = [1,2,3,4,5,6,"taco"];
function reverse(array){
return array.map((item,idx) => array[array.length-1-idx])
}
20 bytes
let reverse=a=>[...a].map(a.pop,a)
const original = [1, 2, 3, 4];
const reversed = [...original].reverse(); // 4 3 2 1
Concise and leaves the original unchanged.
reveresed = [...array].reverse()
The shortest reverse method I've seen is this one:
let reverse = a=>a.sort(a=>1)
**
Shortest reverse array method without using reverse method:
**
var a = [0, 1, 4, 1, 3, 9, 3, 7, 8544, 4, 2, 1, 2, 3];
a.map(a.pop,[...a]);
// returns [3, 2, 1, 2, 4, 8544, 7, 3, 9, 3, 1, 4, 1, 0]
a.pop method takes an last element off and puts upfront with spread operator ()
MDN links for reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
two ways:
counter loop
function reverseArray(a) {
var rA = []
for (var i = a.length; i > 0; i--) {
rA.push(a[i - 1])
}
return rA;
}
Using .reverse()
function reverseArray(a) {
return a.reverse()
}
This is what you want:
array.reverse();
DEMO
Here is a version which does not require temp array.
function inplaceReverse(arr) {
var i = 0;
while (i < arr.length - 1) {
arr.splice(i, 0, arr.pop());
i++;
}
return arr;
}
// Useage:
var arr = [1, 2, 3];
console.log(inplaceReverse(arr)); // [3, 2, 1]
I've made some test of solutions that not only reverse array but also makes its copy. Here is test code. The reverse2 method is the fastest one in Chrome but in Firefox the reverse method is the fastest.
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var reverse1 = function() {
var reversed = array.slice().reverse();
};
var reverse2 = function() {
var reversed = [];
for (var i = array.length - 1; i >= 0; i--) {
reversed.push(array[i]);
}
};
var reverse3 = function() {
var reversed = [];
array.forEach(function(v) {
reversed.unshift(v);
});
};
console.time('reverse1');
for (var x = 0; x < 1000000; x++) {
reverse1();
}
console.timeEnd('reverse1'); // Around 184ms on my computer in Chrome
console.time('reverse2');
for (var x = 0; x < 1000000; x++) {
reverse2();
}
console.timeEnd('reverse2'); // Around 78ms on my computer in Chrome
console.time('reverse3');
for (var x = 0; x < 1000000; x++) {
reverse3();
}
console.timeEnd('reverse3'); // Around 1114ms on my computer in Chrome
53 bytes
function reverse(a){
for(i=0,j=a.length-1;i<j;)a[i]=a[j]+(a[j--]=a[i++],0)
}
Just for fun, here's an alternative implementation that is faster than the native .reverse method.
You can do
var yourArray = ["first", "second", "third", "...", "etc"]
var reverseArray = yourArray.slice().reverse()
console.log(reverseArray)
You will get
["etc", "...", "third", "second", "first"]
> var arr = [1,2,3,4,5,6];
> arr.reverse();
[6, 5, 4, 3, 2, 1]
array.reverse()
Above will reverse your array but modifying the original.
If you don't want to modify the original array then you can do this:
var arrayOne = [1,2,3,4,5];
var reverse = function(array){
var arrayOne = array
var array2 = [];
for (var i = arrayOne.length-1; i >= 0; i--){
array2.push(arrayOne[i])
}
return array2
}
reverse(arrayOne)
function reverseArray(arr) {
let reversed = [];
for (i = 0; i < arr.length; i++) {
reversed.push((arr[arr.length-1-i]))
}
return reversed;
}
Using .pop() method and while loop.
var original = [1,2,3,4];
var reverse = [];
while(original.length){
reverse.push(original.pop());
}
Output: [4,3,2,1]
I'm not sure what is meant by libraries, but here are the best ways I can think of:
// return a new array with .map()
const ReverseArray1 = (array) => {
let len = array.length - 1;
return array.map(() => array[len--]);
}
console.log(ReverseArray1([1,2,3,4,5])) //[5,4,3,2,1]
// initialize and return a new array
const ReverseArray2 = (array) => {
const newArray = [];
let len = array.length;
while (len--) {
newArray.push(array[len]);
}
return newArray;
}
console.log(ReverseArray2([1,2,3,4,5]))//[5,4,3,2,1]
// use swapping and return original array
const ReverseArray3 = (array) => {
let i = 0;
let j = array.length - 1;
while (i < j) {
const swap = array[i];
array[i++] = array[j];
array[j--] = swap;
}
return array;
}
console.log(ReverseArray3([1,2,3,4,5]))//[5,4,3,2,1]
// use .pop() and .length
const ReverseArray4 = (array) => {
const newArray = [];
while (array.length) {
newArray.push(array.pop());
}
return newArray;
}
console.log(ReverseArray4([1,2,3,4,5]))//[5,4,3,2,1]
As others mentioned, you can use .reverse() on the array object.
However if you care about preserving the original object, you may use reduce instead:
const original = ['a', 'b', 'c'];
const reversed = original.reduce( (a, b) => [b].concat(a) );
// ^
// |
// +-- prepend b to previous accumulation
// original: ['a', 'b', 'c'];
// reversed: ['c', 'b', 'a'];
Pure functions to reverse an array using functional programming:
var a = [3,5,7,8];
// ES2015
function immutableReverse(arr) {
return [ ...a ].reverse();
}
// ES5
function immutableReverse(arr) {
return a.concat().reverse()
}
It can also be achieved using map method.
[1, 2, 3].map((value, index, arr) => arr[arr.length - index - 1])); // [3, 2, 1]
Or using reduce (little longer approach)
[1, 2, 3].reduce((acc, curr, index, arr) => {
acc[arr.length - index - 1] = curr;
return acc;
}, []);
reverse in place with variable swapping (mutative)
const myArr = ["a", "b", "c", "d"];
for (let i = 0; i < (myArr.length - 1) / 2; i++) {
const lastIndex = myArr.length - 1 - i;
[myArr[i], myArr[lastIndex]] = [myArr[lastIndex], myArr[i]]
}
Reverse by using the sort method
This is a much more succinct method.
const resultN = document.querySelector('.resultN');
const resultL = document.querySelector('.resultL');
const dataNum = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const dataLetters = ['a', 'b', 'c', 'd', 'e'];
const revBySort = (array) => array.sort((a, b) => a < b);
resultN.innerHTML = revBySort(dataNum);
resultL.innerHTML = revBySort(dataLetters);
<div class="resultN"></div>
<div class="resultL"></div>
Using ES6 rest operator and arrow function.
const reverse = ([x, ...s]) => x ? [...reverse(s), x] : [];
reverse([1,2,3,4,5]) //[5, 4, 3, 2, 1]
Use swapping and return the original array.
const reverseString = (s) => {
let start = 0, end = s.length - 1;
while (start < end) {
[s[start], s[end]] = [s[end], s[start]]; // swap
start++, end--;
}
return s;
};
console.log(reverseString(["s", "t", "r", "e", "s", "s", "e", "d"]));
Infact the reverse() may not work in some cases, so you have to make an affectation first as the following
let a = [1, 2, 3, 4];
console.log(a); // [1,2,3,4]
a = a.reverse();
console.log(a); // [4,3,2,1]
or use concat
let a = [1, 2, 3, 4];
console.log(a, a.concat([]).reverse()); // [1,2,3,4], [4,3,2,1]
What about without using push() !
Solution using XOR !
var myARray = [1,2,3,4,5,6,7,8];
function rver(x){
var l = x.length;
for(var i=0; i<Math.floor(l/2); i++){
var a = x[i];
var b = x[l-1-i];
a = a^b;
b = b^a;
a = a^b;
x[i] = a;
x[l-1-i] = b;
}
return x;
}
console.log(rver(myARray));
JavaScript already has reverse() method on Array, so you don't need to do that much!
Imagine you have the array below:
var arr = [1, 2, 3, 4, 5];
Now simply just do this:
arr.reverse();
and you get this as the result:
[5, 4, 3, 2, 1];
But this basically change the original array, you can write a function and use it to return a new array instead, something like this:
function reverse(arr) {
var i = arr.length, reversed = [];
while(i) {
i--;
reversed.push(arr[i]);
}
return reversed;
}
Or simply chaning JavaScript built-in methods for Array like this:
function reverse(arr) {
return arr.slice().reverse();
}
and you can call it like this:
reverse(arr); //return [5, 4, 3, 2, 1];
Just as mentioned, the main difference is in the second way, you don't touch the original array...
How about this?:
function reverse(arr) {
function doReverse(a, left, right) {
if (left >= right) {
return a;
}
const temp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;
return doReverse(a, left, right);
}
return doReverse(arr, 0, arr.length - 1);
}
console.log(reverse([1,2,3,4]));
https://jsfiddle.net/ygpnt593/8/

split an array into two arrays based on odd/even position

I have an array Arr1 = [1,1,2,2,3,8,4,6].
How can I split it into two arrays based on the odd/even-ness of element positions?
subArr1 = [1,2,3,4]
subArr2 = [1,2,8,6]
odd = arr.filter (v) -> v % 2
even = arr.filter (v) -> !(v % 2)
Or in more idiomatic CoffeeScript:
odd = (v for v in arr by 2)
even = (v for v in arr[1..] by 2)
You could try:
var Arr1 = [1,1,2,2,3,8,4,6],
Arr2 = [],
Arr3 = [];
for (var i=0;i<Arr1.length;i++){
if ((i+2)%2==0) {
Arr3.push(Arr1[i]);
}
else {
Arr2.push(Arr1[i]);
}
}
console.log(Arr2);
JS Fiddle demo.
It would be easier using nested arrays:
result = [ [], [] ]
for (var i = 0; i < yourArray.length; i++)
result[i & 1].push(yourArray[i])
if you're targeting modern browsers, you can replace the loop with forEach:
yourArray.forEach(function(val, i) {
result[i & 1].push(val)
})
A functional approach using underscore:
xs = [1, 1, 2, 2, 3, 8, 4, 6]
partition = _(xs).groupBy((x, idx) -> idx % 2 == 0)
[xs1, xs2] = [partition[true], partition[false]]
[edit] Now there is _.partition:
[xs1, xs2] = _(xs).partition((x, idx) -> idx % 2 == 0)
var Arr1 = [1, 1, 2, 2, 3, 8, 4, 6]
var evenArr=[];
var oddArr = []
var i;
for (i = 0; i <= Arr1.length; i = i + 2) {
if (Arr1[i] !== undefined) {
evenArr.push(Arr1[i]);
oddArr.push(Arr1[i + 1]);
}
}
console.log(evenArr, oddArr)
I guess you can make 2 for loops that increment by 2 and in the first loop start with 0 and in the second loop start with 1
A method without modulo operator:
var subArr1 = [];
var subArr2 = [];
var subArrayIndex = 0;
var i;
for (i = 1; i < Arr1.length; i = i+2){
//for even index
subArr1[subArrayIndex] = Arr1[i];
//for odd index
subArr2[subArrayIndex] = Arr1[i-1];
subArrayIndex++;
}
//For the last remaining number if there was an odd length:
if((i-1) < Arr1.length){
subArr2[subArrayIndex] = Arr1[i-1];
}
Just for fun, in two lines, given that it's been tagged coffeescript :
Arr1 = [1,1,2,2,3,8,4,6]
[even, odd] = [a, b] = [[], []]
([b,a]=[a,b])[0].push v for v in Arr1
console.log even, odd
# [ 1, 2, 3, 4 ] [ 1, 2, 8, 6 ]
As a one-liner improvement to tokland's solution using underscore chaining function:
xs = [1, 1, 2, 2, 3, 8, 4, 6]
_(xs).chain().groupBy((x, i) -> i % 2 == 0).values().value()
filters is a non-static & non-built-in Array method , which accepts literal object of filters functions & returns a literal object of arrays where the input & the output are mapped by object keys.
Array.prototype.filters = function (filters) {
let results = {};
Object.keys(filters).forEach((key)=>{
results[key] = this.filter(filters[key])
});
return results;
}
//---- then :
console.log(
[12,2,11,7,92,14,5,5,3,0].filters({
odd: (e) => (e%2),
even: (e) => !(e%2)
})
)

Categories

Resources