Convert integers to string and then adding using recursion [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Hi I am trying to solve a problem where the input for a function digital_root(n) will add the digits. I am not sure what I am doing wrong.
function digital_root(n) {
// ...
//1. separate n into array of digits
var nString = n.toString();
//[ '1', '2', '3', '4' ]
var numbersToAdd = [];
var total = 0;
for (var i = 0; i < nString.length; i++) {
numbersToAdd.push(+nString.charAt(i));
}
// result is [ 1, 2, 3, 4 ]
//2. add digits
for (var x = 0; x < numbersToAdd.length; x++) {
total += numbersToAdd[i];
//expected outputs
// total = 0 + numbersToAdd[0]--> 0+1--> total = 1
// total = 1 + numbersToAdd[1]-->1+2--> total = 3
// total = 3 + numbersToAdd[2]-->3+3--> total = 6
// total = 6 + numbersToAdd[3]-->6+3--> total = 9
}
return total;
}
console.log(digital_root(1234));

You need to use "x" instead of "i"
So changing
total += numbersToAdd[i];
to
total += numbersToAdd[x];
will fix an issue.
Also output should be 10 instead of 9, there is calculation mistake in your question

Related

Complete and Generate number for a array in JS [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 1 year ago.
Improve this question
In javascript I'm looking to complete one numbers, you can help me generate a array of please.
The first 4 digits start by "23 29 xx xx xx", the xx remains to be completed with a range from 0 to 99. Ex. 23 29 01 02 03
let firstDigit = "2329";
let numberOfRandomDigit = "6";
let firstRange = "01";
let maxRange = "99";
let arrayOfNumbers = ["2329010203, 2329xxxxxx", ...];
I don't know to do this with a loop for complete array
as I see it's a 10 digit number, and you know 4 digits initial, so rest 6 digits you can generate randomly like this :
from random import randint
def random_num(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
rest_digit = random_num(6)
Now you can simply append these 6 digit to the 4 digits that you have.
In JavaScript:
let firstDigit = "2329";
let firstRange = "00";
let maxRange = "99";
var random_string = function(digits) {
var num = Math.floor(Math.random() * (maxRange-firstRange+1)+firstRange).toString();
while (num.length < digits)
{
num = "0" + num;
}
return num;
}
var arrayOfNumbers = [];
for (i=0;i<10;i++)
{
six_digit_string = random_string(2)+random_string(2)+random_string(2);
arrayOfNumbers.push(firstDigit+six_digit_string);
}
In Python:
import random
def random_two_digit_numbers():
return str(random.randint(0,99)).zfill(2)
generated_string = '23 29 {} {} {}'.format(random_two_digit_numbers,random_two_digit_numbers,random_two_digit_numbers)

Just a simple Javascript function to strengthen the core concept of function call [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This gives the output of 0 which means that something is wrong with how I call the factorial function.
function factorial(n) {
let answer = 1;
if (n === 0 || n === 1) {
return answer;
} else {
for (var i = n; i >= 1; i--) {
answer = answer * i;
}
return answer;
}
}
let computation = 0;
function compute(){
let a = 5;
let b = 6
let sum = a + b;
computation = sum + factorial(5);
}
console.log(`The value of this is + ${computation}`);
I don't see you calling compute (i.e. compute()), so computation is still as you initialized it as 0.

double loops stuck in forever loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to loop over my $('.fore-col') and iterate the index position of my api call variables each time incrementing upwards. Starting at 7 and going up by 8 until at the postion of 40. When i run this code, it is stuck in a "forever loop" i believe and didn't stop at the index position of 40. highest count before i pulled the plug was just over 20k.
Any help would be much appreciated!
url: forecastURL,
method: "GET"
}).then(function(res) {
console.log(res);
$(".fore-col").each(function() {
for (let i = 7; i < 40; i + 8) {
let date = res.list[i].dt_txt;
let icon =
"https://openweathermap.org/img/wn/" +
res.list[i].weather +
".png";
let temp = res.list[i].main.temp;
let humid = res.list[i].main.humidity;
console.log(date, icon, temp, humid);
}
});
```
enter code here
for (let i = 7; i < 40; i + 8) should be
for (let i = 7; i < 40; i = i + 8)

convert javascript to python : looping for in python [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 5 years ago.
Improve this question
I have the javascript code and i want to convert it to python.
var dice = [1, 3, 4, 1]
for(let i = 0; i < dice.length; i++) {
if(dice[i] == 1 ) {
dice.splice(i, 1)
break;
}
}
whats wrong?
q = [1, 2,3, 4]
for i, dice in q:
if dice[i] == 1:
dice = splice(i, 1)
break
print dice
I want if the number one appears simultaneously then the number 1 is discarded.
How the python code for the js code above?
q = [1, 2,3, 4]
for dice in q:
if dice == 1:
q.remove(dice)
break
print(q);
Try:
dice = [1, 3, 4, 1]
newval = ''
for i in range(len(dice)):
if dice[i] == 1:
newval = dice[1:i]
print(newval)

How do I make a numerical array's increase amount smaller in each step? [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
I would like to make a numeric array which increases in every step.
[1,200,400,600,800,1000, .... , 10000]
But I need to make the amount by which it increases progressively smaller in every step. For example,
[1, 200, 300, 350, 325, 312.5, ....., 10000]
If anybody knows the solution, please give me some ideas.
Thank you.
Change the increment amount as you please...
var arr = [];
var i = 1;
var incrementAmt = 2000;
for(var j = 0; j < 1000; j++) {
var num = i + incrementAmt;
arr.push(num);
i = num;
i++;
incrementAmt = incrementAmt / 2; // cause incrementer to decrease each iteration
}
console.log(arr)

Categories

Resources