本文主要是介绍0045【Edabit ★☆☆☆☆☆】【字符数转整型】Return a String as an Integer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0045【Edabit ★☆☆☆☆☆】【字符数转整型】Return a String as an Integer
language_fundamentals
numbers
strings
Instructions
Create a function that takes a string and returns it as an integer.
Examples
stringInt("6") // 6
stringInt("1000") // 1000
stringInt("12") // 12
Notes
- All numbers will be whole.
- All numbers will be positive.
Solutions
function stringInt(str) {return parseInt(str);
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();
Test.assertEquals(stringInt("6"), 6)
Test.assertEquals(stringInt("2"), 2)
Test.assertEquals(stringInt("10"), 10)
Test.assertEquals(stringInt("666"), 666)
这篇关于0045【Edabit ★☆☆☆☆☆】【字符数转整型】Return a String as an Integer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!