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

homework lesson 2-3 #20

Open
wants to merge 3 commits 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
21 changes: 21 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = (config) => {
config.set({
frameworks: ["jasmine", "karma-typescript"],
files: [
{pattern: './lesson-2/DarinaAnna/**/*.ts'}
],
preprocessors: {
"**/*.ts": ["karma-typescript"]
},
reporters: ["progress", "karma-typescript"],
browsers: ["ChromeHeadless"],
singleRun: true,
karmaTypescriptConfig: {
tsconfig: "./lesson-2/DarinaAnna/tsconfig.json",
reports: {
"html": "coverage",
"text": ""
}
}
})
};
39 changes: 39 additions & 0 deletions lesson-2/DarinaAnna/homework.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getUnique, isInArray, summator, toMatrix } from './homework';

describe('Test isInArray', () => {
it('should return true', () => {
expect(isInArray([1, 2, '4', true], true, '4', 1, 2))
.toBeTruthy();
});
it('should return false', () => {
expect(isInArray([1, 2, '4', true], 5, true, '4', 1, 2))
.toBeFalsy();
});
});

describe('Test summator', () => {
it('Should return 2', () => {
expect(summator(1, 1)).toBe(2);
});
it('Should return 11', () => {
expect(summator(1, '10')).toBe(11);
});
});

describe('Test getUnique', () => {
it('Should return unique primitives', () => {
expect(getUnique([4798543, 34, 34, 4798543, '404', '404', undefined, null, null, undefined, false, false]))
.toEqual([4798543, 34, '404', undefined, null, false]);
});
});

describe('Test toMatrix', () => {
it('Should return matrix 2x3', () => {
expect(toMatrix([1, true, '2', false, false, 34], 3))
.toEqual([[1, true, '2'], [false, false, 34]]);
});
it('Should return matrix with 1 row', () => {
expect(toMatrix([1, 2, 3, 4], 5))
.toEqual([[1, 2, 3, 4]]);
});
});
62 changes: 62 additions & 0 deletions lesson-2/DarinaAnna/homework.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
type stringOrNumber = string | number;
type primitive = stringOrNumber | boolean | null | undefined;

// 1)
// Написать функцию isInArray(), которая начиная со второго принимает переменное количество аргументов.
// Возвращает true, если все аргументы, кроме первого входят в первый.
// Первым всегда должен быть массив.
function isInArray(array: primitive[], ...arg: primitive[]): boolean {
let result: boolean = true;
for (let i: number = 0; i < arg.length; i++) {
if (array.indexOf(arg[i]) < 0) {
result = false;
}
}

return result;
}

// console.log(isInArray([1, 2, 'one'], 1, 'one'));

// 2)
// Написать функцию summator(), которая суммирует переданые ей аргументы.
// Аргументы могут быть либо строкового либо числового типа. Количество их не ограничено
function summator(...args: stringOrNumber[]): number {
return args.reduce((previousValue: number, currentValue: stringOrNumber) => {
// console.log('prev ' + prev + ' current ' + current);
if (typeof currentValue === 'string') {
return Number(currentValue) + previousValue;
} else {
return previousValue + currentValue;
}
}, 0);
}

// console.log(summator('10', 2, 12));

// 3)
// Написать функцию getUnique(arr), которая принимает аргументом неограниченое число аргументов,
// и возвращает массив уникальных элементов. Аргумент не должен изменяться.
// Порядок элементов результирующего массива должен совпадать с порядком,
// в котором они встречаются в оригинальной структуре.
function getUnique(array: primitive[]): primitive[] {
return array.filter(((value: primitive, index: number): primitive => array.indexOf(value) === index));
}

// console.log(getUnique(1, 2, 3, 1, 2, 22, '2', '3', 12, '13', '45', 12));

// 4)
// Дописать функцию toMatrix(data, rowSize), которая принимает аргументом массив и число,
// возвращает новый массив. Число показывает количество элементов в подмассивах,
// элементы подмассивов беруться из массива data.
// Оригинальный массив не должен быть изменен.
function toMatrix(data: primitive[], rowSize: number): primitive[][] {
const newArray: primitive[][] = [];
for (let index: number = 0; index < data.length; index += rowSize) {
newArray.push(data.slice(index, index + rowSize));
}
return newArray;
}

// console.log(toMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3));
export {isInArray, summator, toMatrix, getUnique};
21 changes: 21 additions & 0 deletions lesson-2/DarinaAnna/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = (config) => {
config.set({
frameworks: ["jasmine", "karma-typescript"],
files: [
{ pattern: './**/*.ts' }
],
preprocessors: {
"**/*.ts": ["karma-typescript"]
},
reporters: ["progress", "karma-typescript"],
browsers: ["ChromeHeadless"],
singleRun: true,
karmaTypescriptConfig: {
tsconfig: "./tsconfig.json",
reports: {
"html": "coverage",
"text": ""
}
}
})
};
7 changes: 7 additions & 0 deletions lesson-2/DarinaAnna/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"include": [
"./homework.ts",
"./homework.spec.ts"
]
}
Loading