JavaScript How do I pass a variable into a JS while loop? pass variable into bash script,pass variable into awk,pass

I want to recursively sum an integer: to split an integer into an array and then sum the individual items until I am left with a single integer array.

This is my logic:

  1. Take an integer (n) and split it into an array.
  2. Sum all the individual numbers.
  3. Is the number greater than 9?
    1. YES: Repeat steps 1 and 2
    2. NO: Return the number
function digital_root(n) {
  let digits = (""+n).split("").map(Number);
  while (digits.length > 1) {
    let result = digits.reduce((sum, int) => sum + int);
    let digits = (""+result).split("").map(Number);
  }
  return digits;
};

This is the error code my Node.js chucks at me (at line 4 in the example code above):

ReferenceError: digits is not defined
    at digital_root (repl:6:18)

I'm assuming that the variable digits is accessible inside the scope of the while loop, but obviously, I seem to be wrong? Could someone shed some insight here for me, please?


EDIT: Thank you everyone for your help! I've solved the issue. For your curiosity, this is the mathematical form underlying my algorithm: http://mathworld.wolfram.com/DigitalRoot.html

It could also have been solved in one line:

function digital_root(n) {
  return (n - 1) % 9 + 1;
}
Answer:1

The inner let for digits (line 5) should be removed

function digital_root(n) {
  let digits = (""+n).split("").map(Number);
  while (digits.length > 1) {
    let result = digits.reduce((sum, int) => sum + int);
    digits = (""+result).split("").map(Number);
  }
  return digits;
}
Answer:2

Issue is let keyword, as let has block level scope, "digits" is not defined at the start of the while loop

Removing let for digits in while loop

function digital_root(n) {
  let digits = (""+n).split("").map(Number);
  while (digits.length > 1) {
    let result = digits.reduce((sum, int) => sum + int);
    digits = (""+result).split("").map(Number);
  }
  return digits;
}

console.log(digital_root(47))
Answer:3

Below is my JS and then my HTML . I cannot figure out why my countdown clock doesn't automatically count down. You must refresh every second to see the correct amount of time left. Any ideas? BTW this ...

Below is my JS and then my HTML . I cannot figure out why my countdown clock doesn't automatically count down. You must refresh every second to see the correct amount of time left. Any ideas? BTW this ...

  1. javascript countdown clock code
  2. javascript countdown clock
  3. javascript countdown clock example
  4. javascript countdown clock explained
  5. js countdown clock
  6. styled javascript countdown clock
  7. javascript countdown flip clock
  8. html javascript countdown clock
  9. simple javascript countdown clock
  10. javascript digital countdown clock
  11. free javascript countdown clock
  12. javascript timer countdown clock
  13. javascript countdown analog clock
  14. javascript display countdown clock
  15. javascript multiple countdown clock
  16. moment js countdown clock
  17. react js countdown clock
  18. javascript countdown timer flip clock
  19. javascript countdown to date flip clock
  20. javascript countdown/count-up timer/clock/ticker for web pages

I'd like to replace all blocked ads (for users with adblock) with an image politely asking to have the site white-listed. This code wasn't written by me at all- it's taken from a site that I can't ...

I'd like to replace all blocked ads (for users with adblock) with an image politely asking to have the site white-listed. This code wasn't written by me at all- it's taken from a site that I can't ...

async function test() { const res = await fetch('https://www.easy-mock.com/mock/5c6d317e8d040716434d0a5b/reading/category/homeSmallCategory'); console.log(res) // data return res; } ...

async function test() { const res = await fetch('https://www.easy-mock.com/mock/5c6d317e8d040716434d0a5b/reading/category/homeSmallCategory'); console.log(res) // data return res; } ...

I am using the Google Custom Search in my web page and want to filter the results before displaying the results. I cannot find any callback in Google's documenation where I can access the results so ...

I am using the Google Custom Search in my web page and want to filter the results before displaying the results. I cannot find any callback in Google's documenation where I can access the results so ...

  1. access google search history
  2. access google search console
  3. access google search data
  4. access google search api
  5. access google search box
  6. access my google search history
  7. can't access google search
  8. cannot access google search engine
  9. python access google search
  10. access vba google search
  11. access google voice search
  12. how to access google search settings
  13. how to access google search trends
  14. how to access google search
  15. cbs all access - google search
  16. how to access google search history on ipad
  17. how to access google search results
  18. google access music search history
  19. give access to google search console
  20. grant access to google search console