5. Multi-language Advertisement

by venkatavineela

HTML

<!--

Problem 5:

Background:
India is a nation of many languages. Hindi is spoken, or at least understood, in many regions. English is spoken in many cities as well. In Bangalore, Kannada is the local language. Urdu is another language spoken in some parts of India. What is interesting about Urdu is that, unlike most languages, it is written right to left.
http://en.wikipedia.org/wiki/Urdu#Urdu_script

Problem statement:
A company released a newspaper advertisement containing text in Hindi, Urdu, and English. However it made a mistake in writing the Urdu words left to right instead of right to left. Write a program to correct the mistake and restructure the input into one line, and also to count the total words in the advertisement. Note that the words are provided in the form of a nested array. The Urdu words are in the second array.

Example:

Given an input of 
[
 ["zara", "dhyaan", "dein"],
 ["mazarat", "chahenge"], // reverse this line
 ["attention", "please"]
]
When the code executes
Then the output should be
{
   words: ["zara", "dhyaan", "dein", "chahenge", "mazarat", "attention", "please"],
   count: 7
}

-->

<textarea rows="5" cols="50" id="advertisement"></textarea><br/>
<button id="but">

Click</button>

<span id="res">

</span>

JavaScript

document.getElementById("but").onclick=advertise
function advertise() {
var answer=" ";
var p="",q="", s="",t="",k="",l="";
var a=document.getElementById("advertisement").value;
var b=a.split("\n");
for(var i=1;i<(b.length-1);i+=1)
{

p+=b[i];

}

var c=p.split(" ");
for(var i=2;i<b[2].length-2;i+=1)
{
q+=b[2][i];
}
var r=q.split(",");
var j=r.length-1;
for(var i=j;i>=0;i-=1)
{
t+=","+r[i];
}



for(var i=0;i<b[1].length-2;i+=1)
{
k+=b[1][i];
}
for(var i=2;i<b[3].length;i+=1)
{
l+=b[3][i];
}
answer+=k+"\t"+t+",\t"+l;
var d=answer.split(",");
document.getElementById("res").innerHTML="words:"+d+"\ncount:"+d.length;

}