Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for [old] Swap Nodes by beryleo
"use strict";
function swapNodes(a) {
var swapped = [];
for (var i = 0; i < a.length; i += 2) {
if (i+1 < a.length) swapped.push(a[i+1])
swapped.push(a[i]);
}
// your code here
return swapped;
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:');
console.log(swapNodes([1, 2, 3, 4]));
// These "asserts" are used for self-checking and not for an auto-testing
assert.deepEqual(swapNodes([1, 2, 3, 4]), [2, 1, 4, 3]);
assert.deepEqual(swapNodes([5, 5, 5, 5]), [5, 5, 5, 5]);
assert.deepEqual(swapNodes([1, 2, 3]), [2, 1, 3]);
assert.deepEqual(swapNodes([3]), [3]);
assert.deepEqual(swapNodes(["hello", "world"]), ["world", "hello"]);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Feb. 27, 2019
Comments: