Home / Past papers / MAD2 Quiz 2 / this paper
Consider the following JavaScript code. What will be logged on to the console?```javascript
function promiseBuilder(t) {
return new Promise((res, rej) => {
setTimeout(() => {
const count = Math.floor(t / 1000)
if (count > 3) {
res(count)
} else {
rej(count * 2)
}
}, t)
})
}
async function getData() {
let data1 = null,
data2 = null
try {
data1 = await promiseBuilder(5000)
} catch (e) {
data1 = 30
}
try {
data2 = await promiseBuilder(2000)
} catch (e) {
data2 = 40
}
return data1 + data2
}
getData().then(
(res) => {
console.log(8 * res)
},
(err) => {
console.log(6 * err)
}
)
```
Consider the Vue application with markup index.html and JavaScript app.js index.html
```html
<body>
<div id="app"></div>
</body>
```
app.js
```javascript
const NavBar = {
template: `<ul>
<li> Home </li>
<slot><li>Bowling Average</li></slot>
</ul>`,
}
const Batsman = {
template: `<NavBar><li>Batting Average</li><li>Strike Rate</li></NavBar>`,
components: {
NavBar,
},
}
new Vue({
el: '#app',
template: `<Batsman />`,
components: {
Batsman,
},
})
```
Suppose the application is running on "http://localhost:8080". What will be rendered by the browser?
Consider the following Vue app with markup index.html and JavaScript app.js. index.html
```html
<body>
<div id="app"></div>
</body>
```
app.js
```javascript
const Dashboard = {
template: `<div> This is dashboard for {{ user.name }} </div>`,
props: ['id'],
data() {
return {
users: [
{ id: 1, name: 'User1' },
{ id: 2, name: 'User2' },
{ id: 3, name: 'User3' },
],
}
},
computed: {
user() {
const user = this.users.find((user) => user.id == this.id)
if (user) {
return user
} else {
return this.users[2]
}
},
},
}
const router = new VueRouter({
routes: [{ path: '/dashboard/:id', component: Dashboard, props: true }],
})
new Vue({
el: '#app',
template: `<router-view />`,
router,
})
```
Suppose the application is running on "http://localhost:8080". What will be rendered inside the router-view component of root component for the URL "http://127.0.0.1:8080/#/dashboard/8" ?
Consider the following Vue application with JavaScript app.js and markup index.html
Consider the following Vue application with JavaScript app.js and markup index.html ```html
index.html
<body>
<div id="app"></div>
</body>
app.js
const players = [
{ name: 'Rohit', role: 'Batsman', runs: 397 },
{ name: 'Jaspreet', role: 'Bowler', wickets: 9 },
{ name: 'Virat', role: 'Batsman', runs: 355 },
{ name: 'Kuldeep', role: 'Bowler', wickets: 10 },
]
const Batsman = {
template: `<div><span v-for="batsman in batsmans">{{batsman.name}}:
{{batsman.runs}}</span></div>`,
computed: {
batsmans() {
return players.filter((player) => player.role == 'Bowler')
},
},
}
const Bowler = {
template: `<div><span v-for="bowler in bowlers">{{bowler.name}}:
{{bowler.wickets}}</span></div>`,
computed: {
bowlers() {
return players.filter((player) => player.role == 'Batsman')
},
},
}
```
```javascript
const Players = {
template: `<div>
Ranking
<router-view />
</div>`,
};
const router = new VueRouter({
routes: [
{
path: '/players',
component: Players,
children: [
{ path: '', component: Batsman, name: 'batsman' },
{ path: '*', component: Bowler, name: 'bowler' },
],
},
],
});
new Vue({
el: '#app',
template: `<router-view />`,
router,
});
```
Suppose the application is running on "http://localhost:8080". What will be rendered inside the router-view component of Players component by the browser for the URL "http://localhost:8080/#/players/allrounder"?
Consider the following Vue application with markup “index.html” and javascript file “app.js”.
Consider the following Vue application with markup “index.html” and javascript file “app.js”. If you open the 'index.html' file in a browser, what content will be displayed on the browser screen after the automatic reloading of the browser tab stops?index.html:
```html
<style>
.heading {
color: yellow;
}
.raw-text {
color: blue;
}
</style>
<body>
<div id="app">
<p :class="[iteration ? 'heading' : 'raw-text']">{{displayText}}</p>
</div>
</body>
<script src="./script.js"></script>
```
app.js:
```javascript
const app = new Vue({
el: '#app',
data: {
iteration: 1,
},
computed: {
displayText: function() {
let result = "";
for (let i = 0; i <= this.iteration; i++)
result += sessionStorage.getItem("data");
return result;
}
},
mounted() {
const iteration = sessionStorage.getItem("iteration") ?
parseInt(sessionStorage.getItem("iteration")) + 2 : 1;
if (iteration >= 1 && iteration < 7) {
const data = sessionStorage.getItem("data");
console.log(data);
if (!data) sessionStorage.data = "JS";
else
sessionStorage.data += data;
sessionStorage.iteration = iteration + 2;
window.location.reload();
}
}
});
```
Consider the below javascript program, and predict the output assuming the variable “random_num” always has a value greater than 8. Note: `Math.random()` method returns a random number from 0 (inclusive) up to but not including 1 (exclusive)
```javascript
const num = "9";
new Promise((rej, res) => {
const random_num = Math.floor(Math.random() * 10);
if (random_num == num) rej(random_num)
else res(num + num);
}).then(
(msg) => console.log("Some Data Came"),
(msg) => console.log("Some Data Lost")
).then(data => {
console.log("Data Sent");
return data;
}).then(num => console.log(num));
```
Consider the below javascript program, and predict the output if executed. ```javascript
let x = 5;
const obj1 = {
x : 10,
func : function () {
console.log(x * this.x)
}
}
const obj2 = {
x : 20,
func : () => {
console.log(this.x);
obj1.func.call(obj2);
}
}
obj2.func();
```
Consider the Vue app with markup index.html and script app.js index.html
```html
<body>
<div id="app"></div>
</body>
```
app.js
```javascript
Vue.component('NavBar', {
template: '<div> This is navbar </div>',
})
const Model = {
template: '<div> This is model </div>',
}
new Vue({
el: '#app',
template: '<div><NavBar /><Model/></div>',
})
```
Suppose the application is running on "http://localhost:8080". What will be rendered by the browser?
Consider the following Vue app with markup index.html and JavaScript app.js. index.html
```html
<body>
<div id="app"></div>
</body>
```
app.js
```javascript
const Dashboard = {
template: `<div> This is dashboard for {{user.name}} </div>`,
props: ['id'],
data() {
return {
users: [
{ id: 1, name: 'User1' },
{ id: 2, name: 'User2' },
{ id: 3, name: 'User3' },
],
}
},
computed: {
user() {
const user = this.users.find((user) => user.id == this.id)
if (user) {
return user
} else {
return this.users[2]
}
},
},
}
const router = new VueRouter({
routes: [{ path: '/dashboard/:id', component: Dashboard, props: true }],
})
new Vue({
el: '#app',
template: `<router-view />`,
router,
})
```
Suppose the application is running on "http://localhost:8080". What will be rendered inside the router-view component of root component for the URL "http://localhost:8080/dashboard/1" ?
Consider the following Vue application with JavaScript app.js and markup index.html
Consider the following Vue application with JavaScript app.js and markup index.html ```html
index.html
<body>
<div id="app"></div>
</body>
app.js
const players = [
{ name: 'Rohit', role: 'Batsman', runs: 397 },
{ name: 'Jaspreet', role: 'Bowler', wickets: 9 },
{ name: 'Virat', role: 'Batsman', runs: 355 },
{ name: 'Kuldeep', role: 'Bowler', wickets: 10 },
]
const Batsman = {
template: `<div><span v-for="batsman in batsmans">{{batsman.name}}:
{{batsman.runs}}</span></div>`,
computed: {
batsmans() {
return players.filter((player) => player.role == 'Bowler')
},
},
}
const Bowler = {
template: `<div><span v-for="bowler in bowlers">{{bowler.name}}:
{{bowler.wickets}}</span></div>`,
computed: {
bowlers() {
return players.filter((player) => player.role == 'Batsman')
},
},
}
```
```javascript
const Players = {
template: `<div>
Ranking
<router-view />
</div>`,
};
const router = new VueRouter({
routes: [
{
path: '/players',
component: Players,
children: [
{ path: '', component: Batsman, name: 'batsman' },
{ path: '*', component: Bowler, name: 'bowler' },
],
},
],
});
new Vue({
el: '#app',
template: `<router-view />`,
router,
});
```
Suppose the application is running on "http://localhost:8080". What will be rendered inside the router-view component of Players component by the browser for the URL "http://localhost:8080/#/players" ?
Consider the following Vue application with markup “index.html” and javascript file “app.js”.
Consider the following Vue application with markup “index.html” and javascript file “app.js”. Suppose you open the file “index.html” in a browser, what will be shown on the browser screen post which browser tab will not be reloaded programmatically? If you open the 'index.html' file in a browser, what content will be displayed on the browser screen after the automatic reloading of the browser tab stops?index.html:
```html
<style>
.heading {
color: yellow;
}
.raw-text {
color: blue;
}
</style>
<body>
<div id="app">
<p :class="[iteration ? 'heading' : 'raw-text']">{{displayText}}</p>
</div>
</body>
<script src="./script.js"></script>
```
app.js:
```javascript
const app = new Vue({
el: '#app',
data: {
iteration: 1,
},
computed: {
displayText: function() {
let result = "";
for (let i = 0; i <= this.iteration; i++)
result += sessionStorage.getItem("data");
return result;
}
},
mounted() {
const iteration = sessionStorage.getItem("iteration") ?
parseInt(sessionStorage.getItem("iteration")) + 2 : 1;
if (iteration >= 1 && iteration < 7) {
const data = sessionStorage.getItem("data");
console.log(data);
if (!data) sessionStorage.data = "JS";
else
sessionStorage.data += data;
sessionStorage.iteration = iteration + 2;
window.location.reload();
}
}
});
```

Suppose you are organizing a ceremony to award the startup companies who have progressed considerably well in the past few years. You have received a lot of registrations. The committee has decided to shortlist the companies based on the following 2 criteria:
1. The company must have a website
2. The company must have a capital of more than 40000
Fill in the code1 & code2, which can be used in Vuex Store to update the "awardees" state variable with the objects of those companies who satisfy the above-mentioned criteria. The objects to be inserted in the "awardees" state variable must only have the company name and a random token generated in the "send_task" action function.
```javascript
const store = new Vuex.Store({
state: {
companies: [
{
name: 'sample1',
website: 'sample1.com',
capital: 50000
},
{
name: 'sample2',
website: null,
capital: 75000
},
{
name: 'sample3',
website: 'sample3.com',
capital: 42000
},
{
name: 'sample4',
website: 'sample4.com',
capital: 38000
}
],
awardees: []
},
mutations: {
update_final(state, payload) {
for (company of state.companies)
code2
}
},
actions: {
send_task: function (context) {
const token = get_token(); // generates a random token
code1
}
}
})
```
```
code1: this.$store.commit("update_final", {"min": 40000, "token": token});
code2: if (company.website != null && company.capital > payload) {
const obj = {};
obj.name = company.name;
obj.token = paylad.token
state.awardees.push(company)
}
```
```
code1: context.commit("update_final", 40000, token);
code2: if (company.website != null && capital > payload)
awardees.push(company)
```
```
code1: this.$store.commit("update_final", 40000, token);
code2: if (company.website != null && capital > payload.min)
awardees.push(company)
```
```javascript
code1: context.commit("update_final", {"min": 40000, "token": token});
code2: if (company.website != null && company.capital > payload.min) {
const obj = {};
obj.name = company.name;
obj.token = paylad.token
state.awardees.push(company)
}
```