Switch numbers per line

I have a line with unique numbers separated by spaces, as shown below:

"2 4 13 14 28 33" 

A fast and efficient way to switch pairs of them in the form is required:

 switchNumbers(2, 28) // result: "28 4 13 14 2 33" 

I could break the string and look for values, but that sounds boring. Any better idea?

+6
source share
7 answers

Try also:

 var numbers = "2 4 13 14 28 33"; function switchNum(from, to){ return numbers.replace(/\d+/g, function(num){ return num == from ? to : num == to ? from : num }) } alert(switchNum(2, 28)) //result: "28 4 13 14 2 33" 

Note Do not use switch as a function name, switch is a statement for JavaScript.

+8
source

You can use array functions instead of strings .

See comments in code:

 var str = "2 4 13 14 28 33"; // Don't use `switch` as name function switchNumbers(a, b) { var arr = str.split(' '); // Convert string to array // Get the index of both the elements var firstIndex = arr.indexOf(a.toString()); var secondIndex = arr.indexOf(b.toString()); // Change the position of both elements arr[firstIndex] = b; arr[secondIndex] = a; // Return swapped string return arr.join(' '); } alert(switchNumbers(2, 28)); 

Demo

+10
source

I can not judge whether it is boring or not, but at least it is not splitting and looping :)

 function switchNumbers(str, x, y) { var regexp = new RegExp('\\b(' + x + '|' + y + ')\\b', 'g'); // /\b(x|y)\b/g return str.replace(regexp, function(match) { return match == x ? y : x; }); } var s = "2 4 13 14 28 33"; document.write('<pre>' + switchNumbers(s, 2, 28) + '</pre>'); 
+5
source

I am sure this is not the best way, but it works.

 var swapnums = function(x,first,second) { var y = x.split(" "); var locOfFirst = y.indexOf(first.toString()); var locOfSecond = y.indexOf(second.toString()); y[locOfFirst] = second.toString(); y[locOfSecond] = first.toString(); return y.join(" "); }; 
+2
source

I think the best solution would be to use a JavaScript replacement method for strings.

W3Schools has a good low below here . It should do exactly what you want, but it can replace ALL the numbers you specified, so be sure to say something like var replacement = str.replace("2 ", "28 ");

EDIT: Indicates a good flaw. Instead, you can try:

EDIT2: Opps, had some flaws in the source code. Tested and works great! :)

  function replaceNumbers(x1, x2, str) { var strMod = " " + str + " " var x1Mod = " " + x1 + " " var x2Mod = " " + x2 + " " // Want to replace "farthest" first to ensure correct replacement. if (str.indexOf(x1Mod) > str.indexOf(x2Mod)) { strMod = strMod.replace(x1Mod, x2Mod) strMod = strMod.replace(x2Mod, x1Mod) } else { strMod = strMod.replace(x2Mod, x1Mod) strMod = strMod.replace(x1Mod, x2Mod) } return strMod.slice(1, strMod.length - 1) } var numbers = "2 4 13 14 28 33"; alert(replaceNumbers(2, 33, numbers)) 
+1
source

Something like this should work.

 var str= "2 4 13 14 28 33"; function switchNumbers(a, b) { var arr = str.split(" "); var first= arr.indexOf(a), second = arr.indexOf(b); arr[first] = arr.splice(second, 1, arr[first])[0]; return arr.join(" "); } 

Jsfiddle demo

+1
source
 var str = "2 4 13 14 28 33"; switchNumbers(2,28); // call function switchNumbers(a,b) { var split_ = str.split(" "); var index_of_1 = split_.indexOf(a+""); var index_of_2 = split_.indexOf(b+"") temp = split_[index_of_1]; split_[index_of_1] = split_[index_of_2] ; split_[index_of_2] = temp ; split_.toString(" "); // Answer } 
+1
source

Source: https://habr.com/ru/post/989413/


All Articles