Last active
September 18, 2018 12:32
-
-
Save MirzaChilman/4585e60d6036f8fbddaeab300e435bfe to your computer and use it in GitHub Desktop.
ReverseChar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function reverseChar(words){ | |
// take the last element | |
let rightIndex = words.length - 1 | |
// take the first element | |
let leftIndex = 0; | |
//iterate and meet in the middle | |
while(leftIndex < rightIndex){ | |
//create a temp for the first element | |
const temp = words[leftIndex]; | |
//reassign the first el with the last el | |
words[leftIndex] = words[rightIndex] | |
//reassign the last el with the first el | |
words[rightIndex] = temp; | |
leftIndex++ | |
//basically meet in the middle | |
rightIndex-- | |
} | |
console.log(words) | |
} | |
reverseChar(['m','a','n','u','s','i','a']) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function reverseChar(words){ | |
// take the last element | |
let rightIndex = words.length - 1 | |
// take the first element | |
let leftIndex = 0; | |
//iterate and meet in the middle | |
while(leftIndex < rightIndex){ | |
//create a temp for the first element | |
const temp = words[leftIndex]; | |
//reassign the first el with the last el | |
words[leftIndex] = words[rightIndex] | |
//reassign the last el with the first el | |
words[rightIndex] = temp; | |
leftIndex++ | |
//basically meet in the middle | |
rightIndex-- | |
} | |
console.log(words) | |
} | |
reverseChar(['m','a','n','u','s','i','a'])</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseChar(words){ | |
// take the last element | |
let rightIndex = words.length - 1 | |
// take the first element | |
let leftIndex = 0; | |
//iterate and meet in the middle | |
while(leftIndex < rightIndex){ | |
//create a temp for the first element | |
const temp = words[leftIndex]; | |
//reassign the first el with the last el | |
words[leftIndex] = words[rightIndex] | |
//reassign the last el with the first el | |
words[rightIndex] = temp; | |
leftIndex++ | |
//basically meet in the middle | |
rightIndex-- | |
} | |
console.log(words) | |
} | |
reverseChar(['m','a','n','u','s','i','a']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment