在 JavaScript 中检查 Doubleton 数
双顿数
如果一个自然数正好包含两个不同的数字,我们将把它称为“双倍数”。例如,23、35、100、12121是双峰数,而123和9980则不是。
问题
我们需要编写一个JavaScript函数,该函数接受一个数字,如果它是一个doubleton数字则返回true,否则返回false。
示例
以下是代码-
const num = 121212;
const isDoubleTon = (num = 1) => {
const str = String(num);
const map = {};
for(let i = 0; i < str.length; i++){
const el = str[i];
if(!map.hasOwnProperty(el)){
map[el] = true;
};
};
const props = Object.keys(map).length;
return props === 2;
};
console.log(isDoubleTon(num));输出结果以下是控制台输出-
true