Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

eternalincantation avatar eternalincantation 2 Точки

03. Memory game - kak se reshava?

03. Memory game - molya za pomosht kak se reshava?

Write a program, which receives a sequence of elements. Each element in the sequence will have a twin. Until the player receives "end" from the console, he will receive strings with two integers separated by space, which represent the indexes of elements in the sequence.  

If the player tries to cheat and enters two equal indexes or indexes which are out of bounds of the sequence you should add two matching elements in the following format "-{number of moves until now}a" at the middle of the sequence and print this message on the console:

"Invalid input! Adding additional elements to the board"

Input

  1. On the first line you will receive sequence of elements.

Output

  1. Every time the player hit two matching elements you should remove them from the sequence and print on the console the following message:

"Congrats! You have found matching elements - ${element}!"

  1. If the player hit two different elements, you should print on the console the following message:

"Try again!"

  1. If the player hit all matching elements before he receives "end" from the console, you should print on the console the following message:

"You have won in {number of moves until now} turns!"

  1. If the player receives "end" before he hits all matching elements, you should print on the console the following message:

"Sorry you lose :(

              {the current sequence's state}"

Constraints

  1. All elements in the sequence will always have a matching element.

 

Examples

Input

Output

1 1 2 2 3 3 4 4 5 5

1 0

-1 0

1 0

1 0

1 0

end

Congrats! You have found matching elements - 1!

Invalid input! Adding additional elements to the board

Congrats! You have found matching elements - 2!

Congrats! You have found matching elements - 3!

Congrats! You have found matching elements - -2a!

Sorry you lose :(

4 4 5 5

Тагове:
0
JavaScript Fundamentals
Axiomatik avatar Axiomatik 2422 Точки

Best,

 

function solve(input) {

const numbers = input

.shift()

.trim()

.split(' ');

 

let moves = 0;

 

while (numbers.length > 1 && input[0].toLowerCase() !== 'end') {

let [index1, index2] = input

.shift()

.split(' ');

index1 = Number(index1);

index2 = Number(index2);

 

moves++;

 

if (index1 < 0

|| index1 >= numbers.length

|| index1 === index2

|| index2 < 0

|| index2 >= numbers.length) {

const index = Math.trunc(numbers.length / 2);

const symbol = '-' + moves + 'a';

numbers.splice(index, 0, symbol, symbol);

console.log('Invalid input! Adding additional elements to the board')

}

else {

const num1 = numbers[index1];

const num2 = numbers[index2];

if (num1 === num2) {

numbers.splice(index1, 1);

index2 = numbers.indexOf(num2);

numbers.splice(index2, 1);

console.log(`Congrats! You have found matching elements - ${num1}!`);

} else if (num1 !== num2) {

console.log('Try again!');

}

}

}

 

if (numbers.length === 0 || numbers.length === 1) {

console.log(`You have won in ${moves} turns!`);

} else {

console.log(`Sorry you lose :(`);

console.log(`${numbers.join(' ')}`);

}

}

0
donamtf avatar donamtf 9 Точки

 I was into this issue and tired to   tinker arou vidmate nd to check if its possible but couldnt get it done. Now that i have seen the way you did it, thanks guys
with
regards

-1
RadoKolev99 avatar RadoKolev99 1 Точки

Very nice solution. I was struggling for hours. Thank you mate!

1
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.