Covert integer in pattern (1>=5, 6>=10, 11>=15 ...) - javascript

I have no idea to covert int like 1>=5, 6>=10, 11>=15 ...
I don't know how this call, but I think this example will explain my question...
1 = 5
2 = 5
3 = 5
4 = 5
5 = 5
6 = 10
7 = 10
8 = 10
9 = 10
10 = 10
11 = 15
//and more..
So JS
var x = 1;
var result = (???) // 5

This should do the trick:
Math.floor((x-1)/5 + 1) * 5
Simpler version (#RichardTowers):
Math.ceil(x/5) * 5

You can do this way:
var x=1;
var change= numberChange(x);
function numberChange(x){
while(x%5!=0){
x=x+1;
}
return x;
}
alert(change);
Demo Fiddle

Related

How to create doubled start pattern using javascript for loops

How to achieve following pattern using for loops without relying on external memory outside the loop:
I want to return 3 zeros, 6 ones and 12 tows and so on..
// the number on the left represents the current index
// the number on the right represents the value that needs to be returned
/*
0 = 0
1 = 0
2 = 0
3 = 1
4 = 1
5 = 1
6 = 1
7 = 1
8 = 1
9 = 2
10 = 2
11 = 2
12 = 2
13 = 2
14 = 2
15 = 2
16 = 2
17 = 2
18 = 2
19 = 2
20 = 2
*/
basically this will help me achieve the following star pattern:
***
******
************
************************
************************************************
I can use the following following but it's not what I really need.
let x = 3;
while (x < 30) {
console.log(x)
x = x * 2
}
In my case, I have a function that exposes an index prop that increments from 0 to a certain value x:
animation.start(({ index }) => {
// is it really possible to achieve the above patten (3, 6, 12..) using only the index prop.
})

Efficient way to solve "CountFactors" codility question

I have been running through Codility questions and and one of the questions was to count all the possible factors of a number. I looped through the whole number got the answer but it wasn't efficient of course.
I searched for the answer and got this
function solution(N) {
var i;
var NumFactors = 0;
for(i = 1;i*i < N; i++) {
if(N%i == 0) NumFactors += 2;
}
if(i*i == N) NumFactors++;
return NumFactors
}
for anyone who hasn't tried the challenge if you run solution(24) it should return 8 as number of factors which are (1, 2, 3, 4, 6, 8,12, 24)
Since the person who wrote the code didn't leave any explanation, can someone who get what's happening kindly explain to me the i*i and the reason of incrementing NumFactors by 2.
The i*i is for checking until squareRoot(N). Because if you have a divisor for a number N then you actually have two divisor. Because the division result is another divisor. For example, in case of 24,
If you take divisor 2, you will find another divisor which is 12. Because 2 X 12 = 24. If you loop through N i.e. 1 to 24 you will get the divisors like this,
2 X 12 = 24
3 X 8 = 24
4 X 6 = 24
6 X 4 = 24
8 X 3 = 24
12 X 2 = 24
24 X 1 = 24
You see we have got redundant values after squareRoot(N). That is why for optimization we are going from 1 to squareRoot(N).
Now about increase factors by 2 is already described above. For the special case when N is a perfect square number like 36 or 49 you will face a case where 6 X 6 = 36 and 7 X 7 = 49 that is why in that case we are increasing the factor by one. Because there is actually on divisor namely 6 and 7 in our case.

jquery array into organized (by original array index) chunks

Okay, so this is the idea.
1.I got an array with 12 numbers (a=[1,2,3,4,5,6,7,8,9,10,11,12]
2.I want to split it into 4 chunks so i did this...
a=[1,2,3,4,5,6,7,8,9,10,11,12];
var b = [];
while(a.length) {
b.push(a.splice(0,3));
}
This gave me an array with 4 elements with 3 values inside each element
i.e. [1,2,3,4] = [ 1 2 3 , 4 5 6 , 7 8 9 , 10 11 12 ]
3.Now my problem is that i would like it to be organized in a way that the first value goes into the first element, the second into the second, the third into the third, and the fourth into the fourth and it repeats the process until i got something like this:
i.e. [1,2,3,4] = [ 1 5 9 , 2 6 10 , 3 7 11 , 4 8 12 ]
This should do it;
var a = [1,2,3,4,5,6,7,8,9,10,11,12];
var chunksize = 3;
var numOfChunks = Math.ceil(a.length/ chunksize);
var b = new Array(numOfChunks);
for(var i=0; i<a.length; i++) {
var pos = i%numOfChunks;
if(!b[pos]) b[pos] = [];
b[pos].push(a[i]);
}

while loop prints 1 up and to 10, but gives back 11

I am again working in CodeAcademy and I have continued along and now working with while loops. However, I am working a little bit in the scratchpad, and I've noticed something weird to me.
This code right below this text:
var counter = 1;
while(counter <= 10){
console.log(counter);
counter = counter + 1;
}
Gives this as a result.
Why does 11 pop up at the bottom. It shouldn't be there. Is it counting 0. Or is there some more bitter explanation to this. Would be glad to get some help, thanks :P
Result:
1
2
3
4
5
6
7
8
9
10
==> 11
This is the behavior of console. It will return the result of the last expression in some cases
var counter = 1, t="loop";
while(counter <= 10){
console.log(counter);
counter = counter + 1;
t = "loop end";
}
will give you
1
2
3
4
5
6
7
8
9
10
"loop end"
I am testing in Firefox and it is logging what the OP says.. and this is my take on it.
var counter = 1;
1 is it <= 10 yes, print add 1
2 is it <= 10 yes, print add 1
3 is it <= 10 yes, print add 1
4 is it <= 10 yes, print add 1
5 is it <= 10 yes, print add 1
6 is it <= 10 yes, print add 1
7 is it <= 10 yes, print add 1
8 is it <= 10 yes, print add 1
9 is it <= 10 yes, print add 1
10 is it <= 10 yes, print add 1
11 <-- prints it.
The while loop knows of the "counter" as it is passed in, not as it is declared 'after' or within the loop. It doesn't back reference. So it still has to go thru again.
before: 1
after: 2
before: 2
after: 3
before: 3
after: 4
before: 4
after: 5
before: 5
after: 6
before: 6
after: 7
before: 7
after: 8
before: 8
after: 9
before: 9
after: 10
before: 10
after: 11
You should do this.
var counter = 0;
while(counter < 10){
console.log(counter);
counter = counter + 1;
}
while(counter <= 10) says while counter is less than or equals to 10 it will do the loop.That's why number 11 is also printed.
The condition 'counter <= 10' allows flow to enter body of the loop when count is equal to 10. You're incrementing the count in the body so the final count will be 11.
Change the condition to 'counter < 10' and the result will be 10.
This will result in 1 - 10 and leave count as 10:
var counter = 0;
while(counter < 10){
counter = counter + 1;
console.log(counter);
}

How to strip 25 to just 5 using javascript?

if i have a number like 25 for example, i would like to use javascript to "knock off" the 2 and keep the 5.
Same for any number - i want the 9 from the 89.
But i also want it to not do this if the number is single like an 8 for example
Thanks in advance,
Reece
Use the modulo operator, it returns the "rest" after a division:
lastDigit = number % 10;
You could use modulus:
var number = 89;
var reminder = number % 10; // 9
You can use the modulus operator (%):
var num = 89 % 10; // 9
var number = 25;
var singledigit = number%10;

Categories

Resources