Skip to content
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 README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# JavaScript-Algorithms

### Some Javascript Algorithms and Solutions
25 changes: 25 additions & 0 deletions Translation/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
From CodeForces -
The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, it's easy to make a mistake during the «translation». Vasya translated word s from Berlandish into Birlandish as t. Help him: find out if he translated the word correctly.

Input
The first line contains word s, the second line contains word t. The words consist of lowercase Latin letters. The input data do not consist unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols.

Output
If the word t is a word s, written reversely, print YES, otherwise print NO.
*/

function Translation(t,s) {
let letters = [];
for(var i = t.length - 1; i >= 0; i--) {
letters.push(t[i]);
}

let reverse_word_withComma = letters.join();
let reverse_word = reverse_word_withComma.replaceAll(',','');
if(reverse_word == s) {
return "YES";
} else {
return "NO";
}
}
20 changes: 20 additions & 0 deletions Way Too Long Words/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
From CodeForces -

Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.

Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.

This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.

Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".

You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
*/

function wayTooLongWords(word){
let wFirst = word[0], wLast = word[word.length - 1];
let len = word.length - 2;
var res = wFirst + len.toString() + wLast;
return res;
}
33 changes: 33 additions & 0 deletions Word/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
From CodeForces - Problem

Vasya is very upset that many people on the Net mix uppercase and lowercase letters in one word.

That's why he decided to invent an extension for his favorite browser that would change the letters' register in every word so that it either only consisted of lowercase letters or,

vice versa, only of uppercase ones. At that as little as possible letters should be changed in the word.

For example, the word HoUse must be replaced with house, and the word ViP — with VIP. If a word contains an equal number of uppercase and lowercase letters,

you should replace all the letters with lowercase ones. For example, maTRIx should be replaced by matrix. Your task is to use the given method on one given word.
*/

function Word(word) {
var lwrLetter = [], uprLetter = [];
let letter = word.split('');
let upperCaseLetters = word.toUpperCase().split('');
for(var i = 0; i < letter.length; i++) {
if(letter[i] === upperCaseLetters[i]) {
uprLetter.push(letter[i]);
} else {
lwrLetter.push(letter[i]);
}
}
if (lwrLetter.length > uprLetter.length) {
return word.toLowerCase();
} else if (lwrLetter.length < uprLetter.length) {
return word.toUpperCase();
} else {
return word.toLowerCase();
}
}
24 changes: 24 additions & 0 deletions Wrong Subtraction/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
From Codeforces -
Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits.
Tanya subtracts one from a number by the following algorithm:

if the last digit of the number is non-zero, she decreases the number by one;
if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit).

You are given an integer number n. Tanya will subtract one from it k times. Your task is to print the result after all k subtractions.

It is guaranteed that the result will be positive integer number.
*/

function WrongSubtraction(n, k) {
for (var i = 0; i < k; i++) {
if(n%10 != 0) {
n = n - 1;
} else {
n = n/10;
}
}

return n;
}