From 7f6a179a87eb6ab7e4856dab017d7a875fab148e Mon Sep 17 00:00:00 2001 From: ericdouglas Date: Wed, 3 Jun 2015 12:12:35 -0300 Subject: [PATCH] Returns true or false if x is within epsilon of y --- .../src/03-lecture.js | 63 ++++++++++++++----- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/computer-science/01-introduction-to-cs-and-programming-mit/src/03-lecture.js b/computer-science/01-introduction-to-cs-and-programming-mit/src/03-lecture.js index 2783dd5..68d153e 100644 --- a/computer-science/01-introduction-to-cs-and-programming-mit/src/03-lecture.js +++ b/computer-science/01-introduction-to-cs-and-programming-mit/src/03-lecture.js @@ -90,31 +90,60 @@ var prompt = require( 'prompt' ); // console.log( ans.toString() + ' is close to square root of ' + x.toString()); // } -// Find closest number to be a square root of another number - bisection method -var x = 12345; -var epsilon = 0.01; -var numGuesses = 0; -var low = 0; -var high = x; -var ans = ( high + low ) / 2; +// // Find closest number to be a square root of another number - bisection method +// var x = 12345; +// var epsilon = 0.01; +// var numGuesses = 0; +// var low = 0; +// var high = x; +// var ans = ( high + low ) / 2; -while ( Math.abs( Math.pow( ans, 2 ) - x ) >= epsilon && ans <= x ) { +// while ( Math.abs( Math.pow( ans, 2 ) - x ) >= epsilon && ans <= x ) { - numGuesses += 1; +// numGuesses += 1; - if ( Math.pow( ans, 2 ) < x ) { +// if ( Math.pow( ans, 2 ) < x ) { - low = ans; +// low = ans; - } else { +// } else { - high = ans; +// high = ans; - } +// } - ans = ( high + low ) / 2; +// ans = ( high + low ) / 2; + +// } + +// console.log( 'numGuesses:', numGuesses ); +// console.log( ans, 'is close to square root of', x ); + +// Returns true or false if x is within epsilon of y +function withinEpsilon( x, y, epsilon ) { + + console.log( 'Returns true if x is within epsilon of y' ); + + return Math.abs( x - y ) <= epsilon; } -console.log( 'numGuesses:', numGuesses ); -console.log( ans, 'is close to square root of', x ); \ No newline at end of file +if ( withinEpsilon( 25, 26, 1 )) { + + console.log( 'Yes' ); + +} else { + + console.log( 'No' ); + +} + +if ( withinEpsilon( 25, 26, 0.9 )) { + + console.log( 'Yes' ); + +} else { + + console.log( 'No' ); + +} \ No newline at end of file