JSFiddle - React, Tailwind, and code Playground

by dvjc

HTML

<div>This is an inline multiplier.</div>
<div>Usage: <a href="http://projecteuler.net/problem=16" id="problemLink" target="_blank">Project Euler (16)</a></div>
<div id="divResult"></div>
<hr/>
<div id="divValue"></div>

CSS

div {
  font-size = 16
}

JavaScript

// array holding each digit of the answer
var result = [];
result[0] = 1;

var a = 1000;

repeatMe(a);

var message = "The sum of the digits of 2^" + a + " = " + getDigitSum();
document.getElementById('divResult').innerText = message;

message = "2^" + a + " happens to be " + getDigitDisplay();
document.getElementById('divValue').innerText = message;

// applies the multiplier once upon the result array
function mult(base){
  if( !base ){ base = 2; }
  var max = result.length;
  for( var i = (max-1); i >=0; i-- ) {
    result[i] = base * result[i];
    handleOverflow();
  }
}

// from GSB to LSB, ensures each cell in the array holds a single digit
function handleOverflow(){
  var max = result.length;
  for( var i = 0; i < max; i++ ) {
    if( result[i]>=10 ){
      result[i] -= 10;
      if( result.length < (i+2) ){ result.push(0); }
      result[i+1] += 1;
    }
  }
}

// appends the individual digits, GSB to LSB, as a single value
function getDigitDisplay(){
  var display = "";
  var max = result.length;
  for( var k = max-1; k >= 0; k-- ){
    display += result[k] + "";
  }
  return display;
}

// adds the individual digits, GSB to LSB, as a single value
function getDigitSum(){
  var sum = 0;
  var max = result.length;
  for( var k = max-1; k >= 0; k-- ){
    sum += result[k];
  }    
  return sum;
}

// applies the multiplier n times
// be is the base
function repeatMe(n, b){
  for( var j=0; j<n; j++){
    mult(b);
  }
}