0% found this document useful (0 votes)
9 views

1

The document contains 8 code examples in JavaScript, including programs to display subject information, generate all combinations of a string, reverse a number, check for palindromes, determine data types, display dictionary definitions in an object, check driving age with a ternary operator, and count letter occurrences in a string. The examples cover basic JavaScript concepts like functions, operators, strings, numbers, objects, and arrays.

Uploaded by

Anisha Choudhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

1

The document contains 8 code examples in JavaScript, including programs to display subject information, generate all combinations of a string, reverse a number, check for palindromes, determine data types, display dictionary definitions in an object, check driving age with a ternary operator, and count letter occurrences in a string. The examples cover basic JavaScript concepts like functions, operators, strings, numbers, objects, and arrays.

Uploaded by

Anisha Choudhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

:

1. Write a program in JavaScript to display your subject name and subject instructor name of
current academic semester.

```javascript
console.log("Subject Name: Web Technology Workshop-2");
console.log("Subject Instructor Name: ANIKET(Asst. prof. CSE,ITER)");
```

2. Write a program in JavaScript using function that generates all combination of a string.
(Example string: ‘WTW’ Expected output: W,WT,WTW,T,TW)

```javascript
function allCombinations(str) {
let result = [];
let len = str.length;
let power = Math.pow(2, len);
for (let i = 0; i < power; i++) {
let temp = "";
for (let j = 0; j < len; j++) {
if (i & Math.pow(2, j)) {
temp += str[j];
}
}
if (temp !== "") {
result.push(temp);
}
}
return result.join(",");
}
console.log(allCombinations("WTW"));
```

3. Write a JavaScript function that reverse a number.

```javascript
function reverseNumber(num) {
return parseInt(num.toString().split("").reverse().join(""));
}
console.log(reverseNumber(12345));
```

4. Write a JavaScript function that checks whether a passed string is palindrome or not?

```javascript
function isPalindrome(str) {
return (
str ===
str
.split("")
.reverse()
.join("")
);
}
console.log(isPalindrome("racecar"));
```

5. Create a variable of type string and try to add a number to it. Write a program in JavaScript
using typeof operator to find the data type of the string for this question.

```javascript
let str = "Hello";
str += 123;
console.log(typeof str);
```

6. Write a program in JavaScript using objects to display 5 dictionary words and maps its meaning.

```javascript
let dictionary = {
"apple": "a round fruit with red or green skin and firm white flesh",
"banana": "a long curved fruit which grows in clusters",
"cherry": "a small round fruit with a red or black skin and a single hard stone",
"grape": "a small green or purple fruit that grows in bunches",
"orange": "a large round juicy citrus fruit with a tough bright reddish-yellow rind"
};
for (let word in dictionary) {
console.log(word + ": " + dictionary[word]);
}
```

7. Write a program in JavaScript by using ternary operator , to check the valid age (valid age is
equal or greater than 18) of driving in INDIA.

```javascript
let age = prompt("Enter your age:");
let validAge = age >= 18 ? true : false;
console.log(validAge);
```

8. Write a function in JavaScript to get the number of occurrences of each letter in specified string.
(take specified string as your name).

```javascript
function countLetters(str) {
let result = {};
for (let i = 0; i < str.length; i++) {
if (result[str[i]]) {
result[str[i]]++;
} else {
result[str[i]] = 1;
}
}
return result;
}
console.log(countLetters("Aniket"));
```

You might also like