Header Ads Widget

Responsive Advertisement

solution to questions

 7)

Input:

[5, 2, 1, 3, 2, 4, 5]

Output:

1, 2, 3, 4, 5


let num = [5, 2, 1, 3, 2, 4, 5];

let result = [...new Set(num)].sort((a,b) => a - b);

console.log(result);

8)

Input:

4

Output:

30


let n = 4;

let sum = 0;

for(let i = 1;i<=n;i++){

sum += i*i // sum = 0 + (1*1) = 1 sum = 1 + (2*2) = 5

}

console.log(sum)

9)

Input:

["apple", "banana", "cherry"]

Output:

banana


let str = ["apple", "banana", "cherry"];

let result = "";

for(let word of str){

if(word.length > result.length){

result = word;

}

}

console.log(result);

10)

Input:

4

Output:

1

2 3

4 5 6

7 8 9 10


let n = 4;

let num = 1;

for(let i = 1; i<=n;i++){

let str = "";

for(let j = 1;j<=i;j++){

str += num + " ";

num++

}

console.log(str);

}

11)

Input:

"Heeello"

Output:

e


let str = "Heeello";

let result = {};

let count = 0;

let word = "";

for(let char of str){

result[char] = result[char] + 1 || 1;

if(result[char] > count){

count = result[char];

word = char;

}

}

console.log(word);


12)

Input:

5

Output:

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5


let n = 5;

for(let i = 1; i <=n; i++){

let row = "";

for(let j = 1; j <=n - i; j++){

row += " ";

}

for(let k = 1; k <= i; k++){

row += i + " ";

}

console.log(row);

}

13)

Input:

12345

Output:

15


let n = 12345;

let result = 0;

while(n > 0){

let rem = n % 10;

result += rem;

n = Math.floor(n / 10);

}

console.log(result);


14)

Input:

12345

Output:

54321


let n = 12345;

let result = 0;

while(n > 0){

let rem = n % 10;

result = result * 10 + rem;

n = Math.floor(n / 10);

}

console.log(result);


15)

Input:

12321

Output:

true


let num = 12321;

let n = num;

let result = 0;

while(n > 0){

let rem = n % 10;

result = result * 10 + rem;

n = Math.floor(n / 10);

}

let result1 = num === result;

console.log(result1);

*/

Post a Comment

0 Comments