var getYears = function(g1,g2,s) {
var mas = []
var h1=0
var f=g1
while (f<=g2){
f=String(f)
for (i=0; i<4; i++){
h1=h1+parseInt(f[i], 10)
}
f=parseInt(f, 10)
if (h1==s){mas.push(f)}
f++
h1=0
}
return mas
}
код свой форматируйте и не плодите новые топики, все до вас написали уже.
еще бы переменные нормально называли, кому этот пост нужен, если неинтересно даже читать абстрактные x1, x2, x3, x4, etc.
var getYears = function(yearStart, yearEnd, summ){
var resMass = [];
var mass = [];
for( var i = yearStart; i<= yearEnd ; i++){ mass.push(String(i));}
console.log(mass)
for( var i = 0; i <= mass.length-1; i++){
for( var j = 0; j < mass[i].length; j++){ mass[i][j] = Number(mass[i][j]);}
;} console.log(mass[0][0])
;}
Не преобразовывается тип элемента, не пойму почему…
Вот мой вариант решения, был приятно удивлен, что заработал с первого раза!) Решение через while.
let getYears = function(yearStart, yearEnd, digitSum) {
let trueYears = [];
while(yearStart <= yearEnd) {
let digitSumYear = 0;
let yearStr = String(yearStart);
for (let i = 0; i <= yearStr.length -1 ; i++) {
digitSumYear += Number(yearStr[i]);
}
if (digitSumYear === digitSum) {
trueYears.push(yearStart);
}
yearStart++;
}
return trueYears;
}
let olympYears = [];
let getYears = function(startYear, stopYear, number) {
let years = [];
let year;
while (startYear <= stopYear) {
startYear += 1;
year = startYear - 1;
years.push(year);
}
for (let i = 0; i < years.length; i++) {
let numStr = String(years[i]);
let sum = Number(numStr[0]) + Number(numStr[1]) + Number(numStr[2]) + Number(numStr[3]);
if(sum === number) {
olympYears.push(Number(numStr));
}
}
return olympYears;
}
вот мое решение:
let olympicYears = [];
let getYears = function(firstYear, lastYear, sum) {
for (let year=firstYear; year<=lastYear; year++){
let n = Number(String(year)[0]) + Number(String(year)[1])+Number(String(year)[2])+Number(String(year)[3]);
if(n===sum) {
olympicYears.push(year);
}
}
return olympicYears;
}
let getYears = function (yearStart, yearEnd, number) {
let years = [];
for (let i = yearStart; i <= yearEnd; i++) {
let total = 0;
let currentYear = i;
for (let j = 0; j < 4; j++) {
total += Number(String(i)[j]);
}
if (total === number) {
years.push(currentYear);
}
}
return years;
}
Вот мой вариант (использован только пройденный материал):
let getYears = function (startYear, endYear, sum) {
let result = [];
for (let i = startYear; i<= endYear; i++) {
let summ = 0;
let sup = String(i);
for (let j = 0; j <= sup.length-1; j++) {
summ += Number(sup[j]);
}
if (summ == sum) {result.push(i)}
}
return result
}
Предлагаю оптимизировать код убрав " let currentYear = i; " а в " years.push(currentYear); " вместо “currentYear” поставить " i ".
Решение ниже в сводке.
Сводка
const reducer = (acc, value) => Number(acc) + Number(value);
const culcSumYear = year => {
const numbers = String(year).split('');
return numbers.reduce(reducer);
}
let getYears = function (yearStart, endYear, sum) {
const years = [];
for (let i = yearStart; i <= endYear; i++) {
const sumYear = culcSumYear(i);
if (sumYear === sum) {
years.push(i);
}
}
return years;
}
let getYears = function (StartPoint, EndPoint, SumOfNumbers) {
let massive = [];
while (StartPoint <= EndPoint) {
let SumYear = 0;
let StartPointt = String(StartPoint);
for (let i = 0; i <= StartPointt.length - 1; i++) {
SumYear += Number(StartPointt[i]);
}
if (SumYear === SumOfNumbers) {
massive.push(StartPoint);
}
StartPoint++;
}
return massive;
}