Reputation: 11
(Javascript)
functionName(“2 plus 3 is = 5!”);
would produce following message in console:
235 plus is =
I am unable to bring the numbers to the front, I am a little stumped
function frontNum(str) {
let emptyArr = [];
let rev = str.split("");
let expression = /[\d]/g;
for(let i = 0; i < rev.length; i++) {
if (rev[i].match(expression)) {
emptyArr += rev.pop(rev[i]);
}
}
console.log(emptyArr + rev.join(''));
}
frontNum("2 plus 3 is = 5!");
Upvotes: 0
Views: 57
Reputation: 22334
a way todo that
'use strict'
function frontNum(str)
{
let s = ''
, n = ''
for (let x of str)
{
if (/[0-9]/.test(x)) n += x
else s += x
}
console.log( n + s.replace(/ +/g,' ') )
}
frontNum('2 plus 3 is = 5!')
Upvotes: 0
Reputation: 10221
Since this is your home work I won't give you the correct version, but give you some pointers instead:
emptyArr
is an array, but you are adding data to it as if it was a string.pop
causing problemsexpression
to capture all the digits and remove them from the string without need converting the string into array and loop through it (it can be done with 2 lines of code)Upvotes: 3