Created
November 26, 2022 11:06
-
-
Save alwaisy/cddb68a1eba10338871e2c08bfa5284f to your computer and use it in GitHub Desktop.
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
/** | |
Today we're discussing Generics; be aware that not all languages support this construct, so fewer languages are enabled for this challenge. Check out the Tutorial tab for learning materials and an instructional video! | |
Task | |
Write a single generic function named printArray; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector). The locked Solution class in your editor tests your function. | |
Note: You must use generics to solve this challenge. Do not write overloaded functions. | |
Input Format | |
The locked Solution class in your editor will pass different types of arrays to your printArray function. | |
Constraints | |
You must have exactly function named printArray. | |
Output Format | |
Your printArray function should print each element of its generic array parameter on a new line. | |
*/ | |
// Solution - all tests [0 to 1] were passed | |
'use strict'; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString: string = ''; | |
let inputLines: string[] = []; | |
let currentLine: number = 0; | |
process.stdin.on('data', function(inputStdin: string): void { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function(): void { | |
inputLines = inputString.split('\n'); | |
inputString = ''; | |
new Solution().main(); | |
}); | |
function readLine(): string { | |
return inputLines[currentLine++]; | |
} | |
// Enter your code here | |
class Solution { | |
items: string[] | |
length: number | |
constructor() { | |
this.items = inputLines | |
this.length = 0; | |
} | |
printArray<T>(items: T[]) { | |
items.forEach((item) => { | |
if(this.length == 0) { | |
this.length = Number(item) | |
// console.log(this.length) don't ever run this console here. | |
} else { | |
this.length -= 1; | |
console.log(item); | |
} | |
}) | |
// using for loop | |
// for (var item of items) { | |
// // if (this.length == 0) { | |
// // this.length = Number(item); | |
// // } else { | |
// // this.length -= 1; | |
// // console.log(item); | |
// // } | |
// console.log(item) | |
// } | |
} | |
main() { | |
return this.printArray(this.items) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment