diff --git a/Javascript/armstrong_no.js b/Javascript/armstrong_no.js new file mode 100644 index 0000000..8050205 --- /dev/null +++ b/Javascript/armstrong_no.js @@ -0,0 +1,24 @@ + +// Program to chech whether is a number is armstrong number or not in javascript + +function armstrong(num){ + + var temp=num; + var arm = 0; + while(temp>0) + { + var a=temp%10; + temp=parseInt(temp/10); + arm=arm+a*a*a; + } + + if(arm == num)return true; + + return false; + +} + +console.log(armstrong(370)); + +//Output +// true \ No newline at end of file diff --git a/Javascript/fibonacci.js b/Javascript/fibonacci.js index 4e0941f..81fa720 100644 --- a/Javascript/fibonacci.js +++ b/Javascript/fibonacci.js @@ -1,6 +1,16 @@ // Fibonacci sequence generator in JavaScript function fibonacci(n) { + let array = []; + + array[0] = 0; + array[1] = 1; + + for (let i = 2; i < n; i++) { + array[i] = array[i - 2] + array[i - 1]; + } + + return array; } diff --git a/Javascript/infixtopostfix.js b/Javascript/infixtopostfix.js index 8cc1002..5abafb2 100644 --- a/Javascript/infixtopostfix.js +++ b/Javascript/infixtopostfix.js @@ -1,9 +1,63 @@ // Infix to Postfix Conversion using Stack in JavaScript -function infixToPostfix(str) { - // your code here +function precedence (c) { + if (c == '^') { + return 3; + } + else if (c == '/' || c == '*') { + return 2; + } + else if (c == '+' || c == '-') { + return 1; + } + else { + return 0; + } } +function isOperand (c) { + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) { + return 1; + } + return 0; +} + +function infixToPostfix(s){ + let st = []; + let postFix = ""; + + for (let i = 0; i < s.length; i++) { + if (isOperand(s[i])) { + postFix += s[i]; + } + else if (s[i] == '(') { + st.push('('); + } + else if (s[i] == ')') { + while (st[st.length - 1] != '(') { + postFix += st[st.length - 1]; + st.pop(); + } + st.pop(); + } + else { + while (st.length != 0 && precedence(s[i]) <= precedence(st[st.length - 1])) { + postFix += st[st.length - 1]; + st.pop(); + } + st.push(s[i]); + } + } + + while (st.length != 0) { + postFix += st[st.length - 1]; + st.pop(); + } + return postFix; +} + + + console.log(infixToPostfix("a+b*(c^d-e)^(f+g*h)-i")); // Output: