-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mykhailo Hutsalenko Homework #1 #61
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IClassName { | ||
className: string | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface ISalary { | ||
[key: string]: number; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { calculateSalaries, getToptPaidEmployee, multiplyNumeric, countBySequence, addClass } from './homework'; | ||
import { ISalary } from './ISalary'; | ||
|
||
describe('Test calculateSalaries method', () => { | ||
const salaries: ISalary = { | ||
'Вася': 100, | ||
'Петя': 300, | ||
'Даша': 250 | ||
}; | ||
|
||
test('calculate slaries of an object with data', () => { | ||
expect(calculateSalaries(salaries)).toBe(650); | ||
}); | ||
|
||
test('calculate slaries of an empty object', () => { | ||
expect(calculateSalaries({})).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('Test getToptPaidEmployee method', () => { | ||
const salaries: ISalary = { | ||
'Вася': 100, | ||
'Петя': 300, | ||
'Даша': 250 | ||
}; | ||
|
||
test('get top paid employee from of an object with data', () => { | ||
expect(getToptPaidEmployee(salaries)).toBe('Петя'); | ||
}); | ||
|
||
test('get top paid employee from of an empty object', () => { | ||
expect(getToptPaidEmployee({})).toBe('нет сотрудников'); | ||
}); | ||
}); | ||
|
||
describe('Test multiplyNumeric method', () => { | ||
const menu = { | ||
width: 200, | ||
height: 300, | ||
title: "My menu" | ||
}; | ||
|
||
test('multiply all numeric properties of an object', () => { | ||
multiplyNumeric(menu); | ||
expect(menu.width).toBe(400); | ||
expect(menu.height).toBe(600); | ||
expect(menu.title).toBe('My menu'); | ||
}); | ||
}); | ||
|
||
describe('Test countBySequence method', () => { | ||
const sequence1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
const sequense2 = [2, 4, 6, 8, 10]; | ||
|
||
test('count by for sequence1', () => { | ||
expect(countBySequence(1, 10)).toEqual(sequence1); | ||
}); | ||
|
||
test('count by for sequence2', () => { | ||
expect(countBySequence(2, 5)).toEqual(sequense2); | ||
}); | ||
}); | ||
|
||
describe('Test addClass method', () => { | ||
test('add several class names', () => { | ||
var obj = { | ||
className: 'open menu' | ||
} | ||
|
||
var expectedClassName: string = 'open menu new me'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use let or const |
||
|
||
addClass(obj, 'new'); // obj.className='open menu new' | ||
addClass(obj, 'open'); // без изменений (класс уже существует) | ||
addClass(obj, 'me'); // obj.className='open menu new me' | ||
|
||
expect(obj.className).toEqual(expectedClassName); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ISalary } from "./ISalary"; | ||
import { IClassName } from "./IClassName"; | ||
|
||
export function calculateSalaries(salaries: ISalary): number { | ||
let totalSalary: number = 0; | ||
Object.values(salaries).forEach(key => totalSalary += key); | ||
return totalSalary; | ||
} | ||
|
||
export function getToptPaidEmployee(salaries: ISalary): string { | ||
let topPaidEmployee = Object.keys(salaries).reduce((function(prevEmp, nextEmp){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. have you set up tslint plugin to your editor? because I have tslint warnings, pls fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, of course. I just did not have time to refactor it all in a best practice way |
||
let prevEmpSalary = salaries[prevEmp]; | ||
let nextEmpSalary = salaries[nextEmp]; | ||
|
||
return prevEmpSalary > nextEmpSalary ? prevEmp : nextEmp | ||
}), 'нет сотрудников'); | ||
|
||
return topPaidEmployee; | ||
} | ||
|
||
export function multiplyNumeric(object : any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no any and type for function return value fix everywhere |
||
for (const property in object) { | ||
if (isNumeric(object[property])) { | ||
object[property] *= 2; | ||
} | ||
} | ||
} | ||
|
||
function isNumeric(n: any) { | ||
return !isNaN(parseFloat(n)) && isFinite(n) | ||
} | ||
|
||
export function countBySequence(initialNumber: number, length: number) { | ||
let array: number[] = []; | ||
for(let counter = 1; counter < length + 1; counter++) { | ||
array.push(counter * initialNumber); | ||
} | ||
return array; | ||
} | ||
|
||
export function addClass(obj: IClassName, cls: string) { | ||
if( obj.className.split(' ').indexOf(cls) < 0) { | ||
obj.className += ' ' + cls.trim(); | ||
} | ||
} | ||
|
||
var obj = { | ||
className: 'open menu' | ||
} | ||
|
||
addClass(obj, 'new'); // obj.className='open menu new' | ||
addClass(obj, 'open'); // без изменений (класс уже существует) | ||
addClass(obj, 'me'); // obj.className='open menu new me' | ||
console.log(obj.className); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no console by tslint rules |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
best practice and by conventions - filename should be salary.interface.ts
the same for another file