This file contains 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 addNumbers(numbers) { | |
const n = numbers.length; | |
let sum = 0; | |
for (let i = 0; i < n; i++) sum += numbers[i]; | |
return sum; | |
} |
This file contains 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 addToLimit(limit) { | |
let sum = 0; | |
for (let number = 1; number <= limit; number++) sum += number; | |
return sum; | |
} |
This file contains 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 binarySearch(list, target) { | |
let start = 0 | |
let end = list.length - 1 | |
while (start <= end) { | |
const middle = Math.floor((start + end) / 2) | |
if (list[middle] === target) return true | |
else if (list[middle] < target) start = middle + 1 | |
else end = middle - 1 | |
} | |
return false |
This file contains 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 countOperations(n) { | |
let operations = 0 | |
for (let i = 0; i < n; i++) operations++ | |
for (let i = 0; i < n; i++) operations++ | |
return operations | |
} |
This file contains 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 sort(list) { | |
const n = list.length | |
for (let i = 0; i < n; i++) | |
for (let ii = 0; ii < n; ii++) | |
if (list[ii] > list[ii + 1]) { | |
const temp = list[ii] | |
list[ii] = list[ii + 1] | |
list[ii + 1] = temp | |
} | |
return list |