Last active
January 18, 2022 07:45
-
-
Save evgeniyworkbel/50bf1db39d361af729ad9a62e8782d25 to your computer and use it in GitHub Desktop.
Курс "Введение в программирование", Hexlet.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const length = (str) => str.length; | |
const reverseInt = (num) => { | |
const str = String(num); | |
const firstChar = str[0]; | |
let newNum = ''; | |
for (let i = 0; i < length(str); i += 1) { | |
newNum = `${str[i]}${newNum}`; | |
} | |
//используем инструкцию if | |
if (firstChar === '-') { | |
newNum = `${firstChar}${newNum.slice(0, length(newNum) - 1)}`; | |
newNum = Number(newNum); | |
return newNum; | |
} | |
return Number(newNum); | |
}; | |
export default reverseInt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const length = (str) => str.length; | |
const reverseInt = (num) => { | |
const str = String(num); | |
const firstChar = str[0]; | |
const hasPrefix = (firstChar === '-'); //используем предикат | |
let newNum = ''; | |
for (let i = 0; i < length(str); i += 1) { | |
newNum = `${str[i]}${newNum}`; | |
} | |
newNum = hasPrefix ? `${firstChar}${newNum.slice(0, length(newNum) - 1)}` : newNum; | |
newNum = Number(newNum); | |
return newNum; | |
}; | |
export default reverseInt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reverseInt = (num) => { | |
const numAsStr = String(Math.abs(num)); | |
let reversedStr = ''; | |
for (let i = numAsStr.length - 1; i >= 0; i -= 1) { | |
reversedStr += numAsStr[i]; | |
} | |
const reversedModule = Number(reversedStr); | |
return num < 0 ? -reversedModule : reversedModule; | |
}; | |
export default reverseInt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Задание: | |
Реализуйте и экспортируйте по умолчанию функцию, которая переворачивает цифры в переданном числе и возвращает новое число. | |
Примеры: | |
reverseInt(13); // 31 | |
reverseInt(-123); // -321 | |
reverseInt(8900); // 98 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const length = (str) => str.length; | |
const reverseInt = (num) => { | |
const str = String(num); | |
const hasPrefix = (str[0] === '-'); //используем предикат | |
let newNum = ''; | |
for (let i = 0; i < length(str); i += 1) { | |
newNum = `${str[i]}${newNum}`; | |
} | |
newNum = hasPrefix ? `-${newNum.slice(0, length(newNum) - 1)}` : newNum; | |
newNum = Number(newNum); | |
return newNum; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment