Javescript create list of numbers 0 to n [closed] - javascript

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 12 months ago.
Improve this question
Sorry, I dont know anything about programming, so bare with me.
I need an expression for a textlayer in adobe after effects which uses javascript. The expression needs to create a customizable list of numbers using 3 variables -
example:
x = 0 //starting point (the first number in the list)
y= 20 //increments (the size of the steps)
z= 5 //the number of steps
the output needs to be a list with each entry in a new line, in this case:
0
20
40
60
80
Hope somebody can help. Thanks

x = 0 //starting point (the first number in the list)
y= 20 //increments (the size of the steps)
z= 5 //the number of steps
let output=[]
for(let i=x;i<z;i++){
console.log(i*y)
output.push(i*y)
}
console.log(output)

let arr = [];
let x = 0;
let y = 20;
let z = 5;
for (let i = 0; i < z * y; i+= y) {
arr.push(i);
}
console.log(arr);

Related

Make a recursive function 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 1 year ago.
Improve this question
I am trying to make a recursive function for this parameters. Function should determine the nth element of the row
a_0 = 1
a_k = k*a_(k-1) + 1/k
k = 1,2,3...
I am trying really hard to do this but i cant get a result. Please help me to do this
I came up with this but it is not a recursive function. I can not do this as a recursive function
let a = 1
let k = 1
let n = 3
for (let i = 1; i<=n; i++){
a = i*a + 1/i
}
console.log(a)
Here's the recursive function you're looking for, it has two conditions:
k == 0 => 1
k != 0 => k * fn(k - 1) + 1/k
function fn(k) {
if(k <= 0) return 1;
return k * fn(k - 1) + 1/k;
}
console.log(fn(1));
console.log(fn(2));
console.log(fn(3));
console.log(fn(4));
Note: I changed the condition of k == 0 to k <= 0 in the actual function so it won't stuck in an infinite loop if you pass a negative k

How to calculate total perimeter of shapes in JavaScript exercise and write proper function [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 3 years ago.
Improve this question
I was at internship interview, where I couldn't solve one logical exercise. It was too complicate for me. Could you help me and explain how to solve it? (I didn't pass, but I really want to know how to solve it). Exercise is:
"Given an array arr of strings complete the function landPerimeter by calculating the total perimeter of all the islands. Each piece of land will be marked with 'X' while the water fields are represented as 'O'. Consider each tile being a perfect 1 x 1 piece of land. Some examples for better visualization:
['XOOXO',
'XOOXO',
'OOOXO',
'XXOXO',
'OXOOO']
see image here : https://i.ibb.co/pbDwWSs/pic1.jpg
should return: "Total land perimeter: 24",
while
['XOOO',
'XOXO',
'XOXO',
'OOXX',
'OOOO']
see image here: https://i.ibb.co/Pcdy8Ct/pic2.jpg
should return: "Total land perimeter: 18"
Kindly ask you to show code example how could I solve this exercise.
Thank you very much!
please see this link find-perimeter-shapes-formed-1s-binary-matrix. need some small modifications in your case . see below
<script>
function numOfNeighbour( mat, i, j,rows,cols)
{
var count = 0;
// UP
if (i > 0 && mat[i - 1][j]==='X')
count++;
// LEFT
if (j > 0 && mat[i][j - 1]==='X')
count++;
// DOWN
if (i < rows-1 && mat[i + 1][j]==='X')
count++;
// RIGHT
if (j < cols-1 && mat[i][j + 1]==='X')
count++;
return count;
}
function findPerimeter( mat)
{
var perimeter = 0;
var rows=mat.length;
var cols=mat[0].length;
// Traversing the matrix and finding ones to
// calculate their contribution.
for (var i = 0; i < rows; i++)
for (var j = 0; j < cols; j++)
if (mat[i][j] && mat[i][j]==='X')
perimeter += (4 - numOfNeighbour(mat, i ,j,rows,cols));
return perimeter;
}
console.log("perimeter of ['XOOXO', 'XOOXO', 'OOOXO', 'XXOXO', 'OXOOO'] is ",findPerimeter(['XOOXO', 'XOOXO', 'OOOXO', 'XXOXO', 'OXOOO'])); //24
console.log("perimeter of ['XOOO', 'XOXO', 'XOXO', 'OOXX', 'OOOO'] is " ,findPerimeter(['XOOO', 'XOXO', 'XOXO', 'OOXX', 'OOOO'])); //18
</script>

Create an array of 20 random numbers [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 4 years ago.
Improve this question
I need to create an array of 20 random numbers in the range from -10 to 10 inclusive and Check whether all numbers match the parameters (from -10 to 10).
here is my code:
let array = [];
for (let i = 0; i < array.length; i++) {
array.push(Math.round((Math.random() * 21) - 10));
}
console.log(array);
It should be more like this
let array = [];
for(let i=0; i<20; i++){
array.push(Math.round((Math.random() * 21) - 10));
}
console.log(array);
This will do the trick,
var array = Array(20).fill().map(() => Math.round(Math.random() * 20) - 10);
console.log(array)

Find extra character between two strings [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 6 years ago.
Improve this question
How can I find an extra character between two strings in an optimal way.
Ex1: S1 - 'abcd', S2 - 'abcxd', output - 'x'
Ex2: S1 - '100001', S2 - '1000011', output - '1'
We can do this by traversing linearly and comparing each character in O(n). I want this to be done in much more optimal way, say in O(logn)
Baseline method (O(n)): Just comparing chars and narrowing in on both sides each cycle.
function findDiffChar(base, baseExtraChar) {
let extraLastIndex = base.length;
let lastIndex = extraLastIndex - 1;
for (let i = 0; i < extraLastIndex / 2; i++) {
console.log(`Loop: ${i}`);
if (base[i] !== baseExtraChar[i])
return baseExtraChar[i];
if (base[lastIndex - i] !== baseExtraChar[extraLastIndex - i])
return baseExtraChar[extraLastIndex - i];
}
return false;
}
console.log(findDiffChar('FOOOOOAR', 'FOOOOOBAR')); // B
Improved method using binary search (O(log n)): Compare halves until you've narrowed it down to one character.
function findDiffChar(base, baseExtraChar) {
if (baseExtraChar.length === 1) return baseExtraChar.charAt(0);
let halfBaseLen = Number.parseInt(base.length / 2) || 1;
let halfBase = base.substring(0,halfBaseLen);
let halfBaseExtra = baseExtraChar.substring(0,halfBaseLen);
return (halfBase !== halfBaseExtra)
? findDiffChar(halfBase, halfBaseExtra)
: findDiffChar(base.substring(halfBaseLen),baseExtraChar.substring(halfBaseLen));
}
console.log(findDiffChar('FOOOOAR', 'FOOOOBAR')); // B
console.log(findDiffChar('---------', '--------X')); // X
console.log(findDiffChar('-----------', '-----X-----')); // X
console.log(findDiffChar('------------', '---X--------')); // X
console.log(findDiffChar('----------', '-X--------')); // X
console.log(findDiffChar('----------', 'X---------')); // X

JavaScript code to include Maximum and minimum limit for a number Z [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 8 years ago.
Improve this question
function add(id,value)
{
var x = document. get Element By Id(id). value ;
x = x.replace('$','');
x = x.replace(',','');
var y = value;
var z = +x + +y;
document.get Element By Id(id). value =z;
}
To set a minimum value for z as 0 and maximium value as 999999999
If your question is how to make sure z is never less than 0 or greater than 999999999, there are two common ways:
Using if:
if (z < 0) {
z = 0;
}
else if (z > 999999999) {
z = 999999999;
}
Using Math:
z = Math.max(0, Math.min(z, 999999999));
Math.min(z, 999999999) will pick the smallest of the values you give it, and so won't return a value greater than 999999999. Similarly, Math.max(0, ...) will return the largest of the two values you give it, and so won't return a value less than 0.
An obvious solution to this would be
if (z > 999999999) {
z = 999999999;
}else if (z < 0) {
z = 0;
};
Insert this between var z = +x + +y; and document.get Element By Id(id). value =z;

Categories