admin管理员组文章数量:1435859
Had a coding challenge to do.
The question was to write a function which returns an arbitrary higher number than N which ends in 0.
My solution was:
function solution(N) {
for (let n = N +1; n <= N + 10; n++) {
if (n % 10 === 0) return n;
}
}
Had a coding challenge to do.
The question was to write a function which returns an arbitrary higher number than N which ends in 0.
My solution was:
function solution(N) {
for (let n = N +1; n <= N + 10; n++) {
if (n % 10 === 0) return n;
}
}
Share
Improve this question
edited Jul 9, 2019 at 12:26
Sharon S
asked Jul 9, 2019 at 6:55
Sharon SSharon S
3652 gold badges3 silver badges10 bronze badges
3
- 1 please add the upper border. what means could? – Nina Scholz Commented Jul 9, 2019 at 6:59
- 1 I think your solution is still not too bad, it will have a max of 10 iterations, so it has a constant time plexity of O(1). – Nick Parsons Commented Jul 9, 2019 at 7:13
- Your solution doesn't work for N=2.5 – Matt Timmermans Commented Jul 9, 2019 at 12:13
8 Answers
Reset to default 4You could just divide and use Math.ceil
:
const solution = (n) => Math.ceil((n+1)/10) * 10
console.log(solution(49))
console.log(solution(40))
console.log(solution(41))
console.log(solution(0))
console.log(solution(-1))
console.log(solution(-11))
I think this is the fastest:
function solution(N) {
return 1000000000;
}
Unless there was a rule against that, it seems to be a trick question!
Just for fun:
There is only one way to settle this! Running all n
from 1 to 999999999 for each functions. I got this result:
@François Huppé solution: 2.884sec
@Mark Meyer solution: 3.25sec
@assoron solution: 3.79sec
@Sharon S solution: 8.824sec
var t = Date.now();
for(var n = 0; n < 1000000000; n++){
var res = solution1(n);
}
document.write('@François Huppé solution\'s: ' + ((Date.now() - t)/1000) + 'sec <br>');
var t = Date.now();
for(var n = 0; n < 1000000000; n++){
var res = solution2(n);
}
document.write('@Mark Meyer solution\'s: ' + ((Date.now() - t)/1000) + 'sec <br>');
var t = Date.now();
for(var n = 0; n < 1000000000; n++){
var res = solution3(n);
}
document.write('@assoron solution\'s: ' + ((Date.now() - t)/1000) + 'sec <br>');
var t = Date.now();
for(var n = 0; n < 1000000000; n++){
var res = solution4(n);
}
document.write('@Sharon S solution\'s: ' + ((Date.now() - t)/1000) + 'sec <br>');
function solution1(n) {
return 1000000000;
}
function solution2(n) {
return Math.ceil((n+1)/10) * 10;
}
function solution3(n) {
return n + (10 - n % 10);
}
function solution4(N) {
for (let n = N +1; n <= N + 10; n++) {
if (n % 10 === 0) return n;
}
}
If you need arbitrary higher number multiple of 10. the easiest approach is to multiply it by 10.
const solution = n => n * 10
I'd just concatenate a '0'
onto the end, and cast to a Number:
const solution = n => Number(n + '0')
You could also just ignore the input and return the largest possible number (the largest possible input is .9999e9, so 1e9 will work for all inputs)
const solution = n => 1e9;
Without Math, using mod.
const solution = n => n + 10 - n % 10
console.log(solution(57))
console.log(solution(23))
console.log(solution(1))
console.log(solution(221))
console.log(solution(9000))
For N being smaller than 10^9 you can always add an if to catch those numbers.
Since noone have considered the problem of precision, then it is a reasonable assumption that the result must be inside the safe integers, so we can simply return the maximal safe enter:
const safe = Number.MAX_SAFE_INTEGER-1
const solution = n => safe
console.log(solution(2))
Note that the result is preputed, so we only need to load a constant from memory and return it.
Not sure if you need to work with negative numbers, but you can use this trick I thought of which will give you the next number higher than n which ends in 0:
function solution(number) {
var remainder = number % 10;
if(remainder === 0) {
return number + 10;
} else {
return number + (10 - remainder);
}
}
console.log(solution(42));
console.log(solution(50));
console.log(solution(105));
Edit: solution by assoron uses the same logic, but is a one liner.
function solution(n)
{
return n % 10 ? n + (10 - n % 10) : n + 10;
}
本文标签: algorithmWhat would be a more performant way of solving this problem with JavaScriptStack Overflow
版权声明:本文标题:algorithm - What would be a more performant way of solving this problem with JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745657256a2668781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论