Skip to content

Instantly share code, notes, and snippets.

@MirzaChilman
Last active September 18, 2018 12:32
Show Gist options
  • Save MirzaChilman/4585e60d6036f8fbddaeab300e435bfe to your computer and use it in GitHub Desktop.
Save MirzaChilman/4585e60d6036f8fbddaeab300e435bfe to your computer and use it in GitHub Desktop.
ReverseChar
<!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>
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