Skip to content

Commit

Permalink
Merge pull request #42 from namantiwari2002/main
Browse files Browse the repository at this point in the history
Added several JavaScript programs
  • Loading branch information
nishchaysinha authored Oct 8, 2022
2 parents 99d6e1a + 80a28fa commit 9b4decf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Javascript/armstrong_no.js
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions Javascript/fibonacci.js
Original file line number Diff line number Diff line change
@@ -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;

}

Expand Down
58 changes: 56 additions & 2 deletions Javascript/infixtopostfix.js
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 9b4decf

Please sign in to comment.