Created
September 19, 2021 03:04
-
-
Save SackeyDavid/d24315dd844d51b3e70af062fdcf5941 to your computer and use it in GitHub Desktop.
Turing Coding Challenge Practise Test - Problem 2
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
// console.log("Hello, World!"); | |
var countElements = function(arr) { | |
var result = 0; | |
// output = 0; | |
// count elements | |
for (var i = 0; i < arr.length; i++) { | |
if(arr.includes(arr[i] + 1)) { | |
result+= 1 | |
} | |
} | |
return result; | |
} | |
var arr = '1 1 2'.split(" ").map(num => parseInt(num)); | |
console.log(countElements(arr)); |
Much thanks bro, I'm glad to learn!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of such long logic, we can also use Set like
var countElements = function(arr) {
return new Set(arr).size;
}
var arr = '1 1 2 2 9 8'.split(" ").map(num => parseInt(num));
console.log(countElements(arr));