Code coverage report for lib/algorithms/2-linkedLists/addTwoLists.js

Statements: 91.3% (21 / 23)      Branches: 70% (7 / 10)      Functions: 100% (1 / 1)      Lines: 100% (21 / 21)      Ignored: none     

All files » lib/algorithms/2-linkedLists/ » addTwoLists.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35      1 3 3   3 3 3 3   3 10   10 9 9   10 10 10     10 10 10   10     3     1
//2.5a: Given two lists, representing numbers is reverse order,
//      Sum the two numbers and return the result as a similar list
 
var addTwoLists = function(left, right) {
	Iif (left === undefined) throw "Left is Bad";
	Iif (right === undefined) throw "Right is Bad";
 
	var node = {};
	var start = node;
	var remainder = 0;
	var amount = 0;
 
	while((left != null) || (right != null)) {
		amount = remainder;
 
		if (left !== null) {
			amount = amount + left.data;
			left = left.next;
		}
		Eif (right !== null) {
			amount = amount + right.data
			right = right.next;
		}
 
		node.data = amount % 10;
		node.next = {};
		node = node.next;
 
		remainder = Math.floor(amount / 10);
	}
 
	return start;
}
 
module.exports = addTwoLists;