Explore what CodeHS has to offer for districts, schools, and teachers.
Click on one of our programs below to get started coding in the sandbox!
View All
Consider the following code:
let x = 1; let y = 2; function main() { // call swap function here console.log("value of x: " + x); console.log("value of y: " + y); } main();
Which of the following swap functions can we call so that the values of x and y are successfully swapped?
swap
x
y
function swap() { y = x; x = y; return x, y; }
Pass in the variables x and y to the function:
function swap(first, second) { let temp = first; first = second; second = temp; }
function swap() { let temp = x; x = y; y = temp; }
function swap(first, second) { first, second = second, first; }