Loading...
simon.kan1 avatar simon.kan1 9 Точки

06. Format the Text DOM Introduction

Здравейте, тази зад ми дава 40/100 , на зеро тестовете минава, и не мога да открия от какво, моят код:

https://pastebin.com/FUCAJeEF

https://judge.softuni.bg/Contests/Compete/Index/2761#5

06.Format the Text

Create functionality that gets text from textarea, formats the given text - you need to find out how many sentences there are in the text, simply split the whole text by '.'
Also, every sentence must have at least 1 character.

Generate HTML paragraphs as a string (Use interpolation string to create paragraph element: `<p> {text} </p>`) and append it to the div with an id = "output".

When the [Format] button is clicked, get the text inside the textarea with an id="input" and format it. The formatting is done as follows:

  • Create a new paragraph element which holds no more than 3 sentences from the given input.
  • Hint: Use interpolation string to create paragraph element. (`<p> {text} </p>`)
  • If the given input contains less or 3 sentences, you need to create only 1 paragraph, fill it with these sentences and append this paragraph to the div with an id="output".

Otherwise, when you have more than 3 sentences, create enough paragraphs to get all sentences from the textarea.

Just remember to restrict the sentences in each paragraph to 3.

Example:

  • If the input textarea contains 2 sentences, create only 1 paragraph with these 2 sentences

 

  • If the input textarea contains 7 sentences, create 3 paragraphs
    - The first paragraph must contain the first 3 sentences
    - The second paragraph must contain the other three sentences of the whole text
    - The third paragraph will contain only the last sentence

Output

Input

Output

JavaScript, often abbreviated as JS, is a high-level, interpreted programming language. It is a language which is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.

<p>JavaScript, often abbreviated as JS, is a high-level, interpreted programming language. It is a language which is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.</p>

JavaScript, often abbreviated as JS, is a high-level, interpreted programming language. It is a language which is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm. Alongside HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web. JavaScript enables interactive web pages and thus is an essential part of web applications. The vast majority of websites use it, and all major web browsers have a dedicated JavaScript engine to execute it. As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative (including object-oriented and prototype-based) programming styles. It has an API for working with text, arrays, dates, regular expressions, and basic manipulation of the DOM, but the language itself does not include any I/O, such as networking, storage, or graphics facilities, relying for these upon the host environment in which it is embedded.

<p>JavaScript, often abbreviated as JS, is a high-level, interpreted programming language.It is a

language which is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.Alongside

HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web.</p>
<p>JavaScript enables

interactive web pages and thus is an essential part of web applications.The vast majority of websites use it,

and all major web browsers have a dedicated JavaScript engine to execute it.As a multi-paradigm language,

JavaScript supports event-driven, functional, and imperative (including object-oriented and prototype-based)

programming styles.</p>

<p>It has an API for working with text, arrays, dates, regular expressions, and basic

manipulation of the DOM, but the language itself does not include any I/O, such as networking, storage, or

graphics facilities, relying for these upon the host environment in which it is embedded.</p>

Тагове:
1
JavaScript Advanced
gbsho avatar gbsho 9 Точки

Отдолу са 2 решения (100/100) - с 1 (един) и с 2 (два) цикъла.

 

// от упражението - с 2 for цикъла
function solve() {
    let text = document.getElementById("input").value.split(".").filter(e=>e.length>0)
    let div = document.getElementById("output");
    for (i=0;i<text.length;i+=3){
        let output = [];
        for(j=0;j<3;j++){
            if (text[i+j]){
                output.push(text[i+j])
            }    
        }
        let res = output.join(". ")+"."
        div.innerHTML += `<p>${res}</p>`
    }
}

// мое решение - с 1 for цикъл
function solve() {
    let text = document.getElementById("input").value.split(".").filter(e=>e)
    let output = [];

    for (i=0;i<text.length;i++){
        let sentence = text[i]
        if (sentence.length>0){
            if (i%3===0 && i!=text.length-1){output.push("<p>"+sentence+".")}
            else if (i%3===1 && i!=text.length-1){output.push(sentence+".")}
            else if (i%3===2 && i!=text.length-1){output.push(sentence+"."+"</p>")}
            else{
                if ((i%3===0)&& i===text.length-1){output.push("<p>"+sentence+"."+"</p>")}
                else{output.push(sentence+"."+"</p>")}
            }
        }
    }
    document.getElementById("output").innerHTML = output.join(" ")
}

 

Успех!

0
cdimitrov avatar cdimitrov 1 Точки

На тази задача е хубаво да се гледа за проверка дали последното изречение има точка накрая. if(!arr.includes('.')) {sentence += '.'}
Ето и малко по-различен код: https://pastebin.com/u8XNXva3

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.