https://github.com/timursaurus/dar-intern-task
Dar internship submission
https://github.com/timursaurus/dar-intern-task
Last synced: 8 months ago
JSON representation
Dar internship submission
- Host: GitHub
- URL: https://github.com/timursaurus/dar-intern-task
- Owner: timursaurus
- Created: 2022-02-08T13:55:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-02-08T14:20:16.000Z (over 4 years ago)
- Last Synced: 2025-10-06T23:43:58.275Z (9 months ago)
- Language: TypeScript
- Homepage:
- Size: 1000 Bytes
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dar internship task
---
## First
```ts
function colorPatternTimes(colors: string[]): number {
const res = [colors[0]]
return colors.forEach(color => res[res.length - 1] !== color && res.push(color)), res.length - 1 + colors.length * 2
}
```
```ts
console.log(colorPatternTimes(["Blue"])) //=> 2
console.log(colorPatternTimes(["Red", "Yellow", "Green", "Blue"])) //=> 11
console.log(colorPatternTimes(["Blue", "Blue", "Blue", "Red", "Red", "Red"])) //=> 13
```
---
## Second
```ts
function plant(seed: string, water: number, fert: number, temp: number): string {
const stem = '-'.repeat(water)
const flower = seed.repeat(fert)
return temp < 20 || temp > 30 ? stem.repeat(water) + seed : (stem + flower).repeat(water)
}
```
```ts
console.log(plant("@", 3, 3, 25)) //=> ---@@@---@@@---@@@
console.log(plant("#", 1, 5, 30)) //=> -#####
console.log(plant("§", 3, 3, 15)) //=> ---------§
console.log(plant("&", 5, 1, 20)) //=> -----&-----&-----&-----&-----&
```
---
### More readable version of the first solution
```ts
function colorPatternTimes(colors: string[]): number {
const res = [colors[0]]
colors.forEach(color => {
if (res[res.length - 1] !== color) {
res.push(color)
}
})
return res.length - 1 + colors.length * 2
}
```