Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/mukhailo.hutsalenko/IClassName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IClassName {
className: string
}
3 changes: 3 additions & 0 deletions src/mukhailo.hutsalenko/ISalary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ISalary {
Copy link
Owner

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

[key: string]: number;
}
78 changes: 78 additions & 0 deletions src/mukhailo.hutsalenko/homework.spec.ts
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';
Copy link
Owner

Choose a reason for hiding this comment

The 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);
});
});
55 changes: 55 additions & 0 deletions src/mukhailo.hutsalenko/homework.ts
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){
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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) {
Copy link
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no console by tslint rules