Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function equalSumsEvenOddPosition(input) {
- let num1 = Number(input[0]);
- let num2 = Number(input[1]);
- let output = "";
- for (let i = num1; i <= num2; i++) {
- let odd = 0;
- let even = 0;
- let digit = i;
- for (let x = 0; x < 6; x++) {
- if (x % 2 === 0) {
- even += digit % 10;
- } else {
- odd += digit % 10;
- }
- digit = Math.floor(digit / 10);
- }
- if (odd === even) {
- output += i + " ";
- }
- }
- console.log(output)
- }
- РЕШЕНИЕ С toString():
- function equalSumsEvenOddPosition(input) {
- let num1 = Number(input[0]);
- let num2 = Number(input[1]);
- let output = "";
- for (let i = num1; i <= num2; i++) {
- let odd = 0;
- let even = 0;
- let current = i.toString();
- for (let x = 0; x < current.length; x++) {
- let digit = Number(current[x].toString());
- if (x % 2 === 0) {
- even += digit;
- } else {
- odd += digit;
- }
- }
- if (odd === even) {
- output += i + " ";
- }
- }
- console.log(output)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement