javascript next number on multiples of three [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a number for example 4, i want to get next number on multiples of 3
Multiples of three: [3,6,9,12,15,18,21,24,27,30,...]
the result must be 6
i'm looking for a javascript function
something like this:
function (myNum) { //myNum = 4;
var multiples = [3,6,9,12,15,18,21,24,27,30];
var result;
// do something!!
return result; // returns 6
}
thanks

I suggest another solution:
function getNext(num, dep){
return (((num % dep) ? dep:0) - num % dep) + num;
}
document.write(getNext(4, 3));//6
//document.write(getNext(200, 7));//203
Updated: You can use this method for finding next number on multiples of any number

There are a lot of ways you can achieve this. Here is an easy one. Increment the number until you get a multiple of three.
function multipleOfThree(num){
while(num % 3 != 0)
num++;
return num;
}

You must try before asking a question. If you stuck at somewhere then it is good to ask questions with problem. By the way here what you can try:
function multiple(number) {
return number % 3 === 0 ? ((number/3) * 3) : parseInt((number/3) + 1) * 3;
}

Related

How could i write this random number generating button/div syntax in another way? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have the following code:
// Reference to the <div> which displays the random number:
var rndDiv = document.getElementById('rndNum')
// Reference to the <button> which generates the random number:
var rndBtn = document.getElementById('rnd')
// Generating the random number through 'click' eventlistener:
rndBtn.addEventListener('click', function intRnd() {
var n = Math.floor((Math.random() * 10) + 1);
console.log(n)
rndDiv.innerHTML = n
})
how could/should i write this code differently, how would you write it? Would you use, for example, arrow functions? let instead of var? I'm just curious. Also i'm the total opposite of a 'pro'-coder, just a beginner, and would like to read your code to this solution.
Thanks for taking your time and reading my post!
Here you go ... !
IIFE
Arrow Function
Let
(function() {
let rndBtn = document.getElementById('rnd');
rndBtn.addEventListener('click', () => {
let rndDiv = document.getElementById('rndNum');
rndDiv.innerHTML = Math.floor((Math.random() * 10) + 1);
});
})();
<button id="rnd">Click</button>
<div id="rndNum"></div>
Here is another way
const randomNumGenerator = () => Math.floor((Math.random() * 10) + 1);
const randomNumDiv = document.getElementById('rndNum');
document.getElementById('rnd').addEventListener('click', () => {
randomNumDiv.innerHTML = randomNumGenerator();
});

How can I do this using Reduce function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
var y= '110001'.split("").reverse();
var sum = 0;
for (var i = 0; i < y.length; i++) {
sum += (y[i] * Math.pow(2, i));
}
console.log(sum);
It would be simplest to do
console.log(Array.from('110001').reduce((prev, cur) => prev << 1 | cur));
<< is the left-bitshift operator, which here essentially multiplies by two.
Array.from (if available) is preferable to split. In this case it doesn't matter, but split will fail with surrogate pair characters such as 🍺, while Array.from will handle them correctly. This could also be written as [...'110001'], which ends up being the same thing.
Of course, you could also just say
parseInt('110001', 2)
check this snippet
var binary = '110001'.split("").reverse();
var sum = binary.reduce(function(previous, current, index) {
previous = previous + (current * Math.pow(2, index));
return previous;
}, 0);
console.log(sum);
Hope it helps

Generate random value at interval of 5 Sec in JavaScript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can anyone tell me how to generate random value between 0 and 100 in an interval of 5 seconds.
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
var randomNum = 0;
setInterval(function () {
randomNum = randomIntFromInterval(0, 100);
}, 5000)
With help from
Math.random(),
setInterval()
function setRandom() {
document.getElementById('out').innerHTML = Math.random() * 101 | 0;
}
setRandom();
setInterval(setRandom, 5000);
<div id="out"></div>
Well, Thanks for the repliy #nina-scholz #ilian6806 .
var random = 0;
random = randomizator(0,100);
function randomizator(a,b)
{
return Math.floor(Math.random()*b) + a;
}
I used another function to handle my Interval.

Can someone pls help me solve this issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Create a function fizzBuzz to return 'Fizz', 'Buzz', 'FizzBuzz', or the argument it receives, all depending on the argument of the function, a number that is divisible by, 3, 5, or both 3 and 5, respectively.
When the number is not divisible by 3 or 5, the number itself should be returned
There are several ways to solve this exercise. One possible fizzBuzz function:
function fizzBuzz(number){
return number % 15 == 0 ? "FizzBuzz" : number % 5 == 0 ? "Buzz" :
number % 3 == 0 ? "Fizz" : number;
};
This is how to test it:
alert(fizzBuzz(10));
alert(fizzBuzz(60));
alert(fizzBuzz(6));
alert(fizzBuzz(7));
I recommend trying the W3Schools JavaScript tutorial, it is easy to understand for beginners.
for (var i=1; i <= 20; i++) {
if (i % 15 == 0)
console.log("FizzBuzz");
else if (i % 3 == 0)
console.log("Fizz");
else if (i % 5 == 0)
console.log("Buzz");
else
console.log(i);
}
from here: https://gist.github.com/jaysonrowe/1592432

Make all possible combos in a string of numbers with split in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an string of numbers "123456" i want to split them in all possible ways.
So
1 23456
1 2 3456
1 23 45 6
1234 5 6
and so on
What i have tried...
looping over len-1, and splitting on every index, but logically it misses a lot of possible scenarios.
You could try a recursive function like below...
<script lang="javascript">
// Split string into all combinations possible
function splitAllWays(result, left, right){
// Push current left + right to the result list
result.push(left.concat(right));
//document.write(left.concat(right) + '<br />');
// If we still have chars to work with in the right side then keep splitting
if (right.length > 1){
// For each combination left/right split call splitAllWays()
for(var i = 1; i < right.length; i++){
splitAllWays(result, left.concat(right.substring(0, i)), right.substring(i));
}
}
// Return result
return result;
};
var str = "123456";
var ans = splitAllWays([], [], str);
</script>
Results
123456
1,23456
1,2,3456
1,2,3,456
1,2,3,4,56
1,2,3,4,5,6
1,2,3,45,6
1,2,34,56
1,2,34,5,6
1,2,345,6
1,23,456
1,23,4,56
1,23,4,5,6
1,23,45,6
1,234,56
1,234,5,6
1,2345,6
12,3456
12,3,456
12,3,4,56
12,3,4,5,6
12,3,45,6
12,34,56
12,34,5,6
12,345,6
123,456
123,4,56
123,4,5,6
123,45,6
1234,56
1234,5,6
12345,6
I think that is the right results (32 combinations). Can someone confirm?

Categories