Avatar of brunnock

brunnock's solution

to Octal in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert an octal number, represented as a string (e.g. '1735263'), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion).

Implement octal to decimal conversion. Given an octal input string, your program should produce a decimal output.

Note

  • Implement the conversion yourself. Do not use something else to perform the conversion for you.
  • Treat invalid input as octal 0.

About Octal (Base-8)

Decimal is a base-10 system.

A number 233 in base 10 notation can be understood as a linear combination of powers of 10:

  • The rightmost digit gets multiplied by 10^0 = 1
  • The next number gets multiplied by 10^1 = 10
  • ...
  • The nth number gets multiplied by 10^(n-1).
  • All these values are summed.

So:

   233 # decimal
 = 2*10^2 + 3*10^1 + 3*10^0
 = 2*100  + 3*10   + 3*1

Octal is similar, but uses powers of 8 rather than powers of 10.

So:

   233 # octal
 = 2*8^2 + 3*8^1 + 3*8^0
 = 2*64  + 3*8   + 3*1
 = 128   + 24    + 3
 = 155

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine octal.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

All of Computer Science http://www.wolframalpha.com/input/?i=base+8

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

octal.spec.js

var Octal = require('./octal');

describe('octal', function () {
  it('1 is decimal 1', function () {
    expect(new Octal('1').toDecimal()).toEqual(1);
  });

  xit('10 is decimal 8', function () {
    expect(new Octal('10').toDecimal()).toEqual(8);
  });

  xit('17 is decimal 15', function () {
    expect(new Octal('17').toDecimal()).toEqual(15);
  });

  xit('11 is decimal 9', function () {
    expect(new Octal('11').toDecimal()).toEqual(9);
  });

  xit('130 is decimal 88', function () {
    expect(new Octal('130').toDecimal()).toEqual(88);
  });

  xit('2047 is decimal 1063', function () {
    expect(new Octal('2047').toDecimal()).toEqual(1063);
  });

  xit('7777 is decimal 4095', function () {
    expect(new Octal('7777').toDecimal()).toEqual(4095);
  });

  xit('1234567 is decimal 342391', function () {
    expect(new Octal('1234567').toDecimal()).toEqual(342391);
  });

  xit('invalid is decimal 0', function () {
    expect(new Octal('carrot').toDecimal()).toEqual(0);
  });

  xit('considers the digit 8 as invalid', function () {
    expect(new Octal('12345678').toDecimal()).toEqual(0);
  });
});
function Octal(string){
    this.toDecimal = function() {
	return string.split('').reverse().reduce(function(decimal,x,index){
		return (x>0) ? (decimal + Math.pow(8,index) * x) : decimal ;
	    },0);
    }
}

module.exports=Octal;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?