From 07e1167278f1fd5bda582f6d194959a604462109 Mon Sep 17 00:00:00 2001 From: bruna Date: Mon, 13 Oct 2025 17:49:02 +0200 Subject: [PATCH] LabSolved --- index.js | 68 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index aa0a0fd..82ce900 100644 --- a/index.js +++ b/index.js @@ -5,21 +5,23 @@ const string1 = "My favorite dessert is jello"; -// Your code here... - - - +console.log(string1.indexOf("j")); /******************************************* Iteration 2 | Concatenate Characters *******************************************/ // Make a new string with the text "COOL" by using only the characters available in the provided string and the bracket notation - +//ABCDEFGHJKLO const string2 = "ABCDEFGHJKLO"; +const positionC = string2.indexOf("C"); +const positionO = string2.indexOf("O"); +const positionL = string2.indexOf("L"); -// Your code here... +console.log ( `${string2[positionC]}${string2[positionO]}${string2[positionO]}${string2[positionL]}`); +/*const cool = string2[2] + string2[11] + string2[11] + string2[10]; +console.log(cool);*/ /***************************************************** @@ -28,10 +30,9 @@ const string2 = "ABCDEFGHJKLO"; // Using the method .repeat() and the provided string, print out the text "NaNaNaNa Batman!" in the console. const string3 = "Na"; - -// Your code here... - - +const bat = "Batman" +const repeatNa = string3.repeat(4); +console.log(`${repeatNa} ${bat}`); /******************************************* @@ -39,11 +40,35 @@ const string3 = "Na"; *******************************************/ // Using the string method .slice(), access and print to the console the name of your favorite fruit from a given string -const fruit = "banana apple mango orange lemon kiwi watermelon grapes pear pineapple"; - -// Your code here... +//const fruit = "banana apple mango orange lemon kiwi watermelon grapes pear pineapple" +const fruit = "banana apple mango orange lemon kiwi watermelon grapes pear pineapple"; +const favoriteFruit = "pear"; +const fruitCaracteres = favoriteFruit.length; // 5 +let indiceInicial = -1; // Inicializamos con un valor que indica "no encontrado" + +// Iteramos a través de la cadena. El ciclo se detiene +// justo antes de que el corte (.slice) exceda la longitud de la cadena. +for (let i = 0; i < fruit.length; i++) { + + // 1. Usamos .slice(i, i + n) para tomar una porción de caracteres. + // 2. Comparamos esa porción con la fruta buscada . + if (favoriteFruit === fruit.slice(i, i + fruitCaracteres)) { + + indiceInicial = i; // + break; // ¡Salimos del ciclo! No necesitamos seguir buscando. + } +} +// Verificación y Salida +if (indiceInicial !== -1) { + // Si encontramos la fruta, la extraemos usando el índice guardado. + const frutaEncontrada = fruit.slice(indiceInicial, indiceInicial + fruitCaracteres); + + console.log(frutaEncontrada); +} else { + console.log("Error: La fruta no fue encontrada."); +} /*************************************************** Iteration 5 | Check If Strings Include a Word @@ -57,12 +82,22 @@ const funnyHeadline2 = "Students Cook & Serve Grandparents"; // Check the first headline -// Your code here ... +// Your code here .. + +if (funnyHeadline1.includes("oxygen")) { + console.log("The string includes the word 'oxygen'"); +} else { + console.log("The string does not include the word 'oxygen'") +} // Check the second headline // Your code here ... - +if (funnyHeadline2.includes("oxygen")) { + console.log("The string includes the word 'oxygen'"); +} else { + console.log("The string does not include the word 'oxygen'") +} /******************************************* @@ -75,7 +110,8 @@ const string4 = "zEAWrTC9EgtxmK9w1"; // a) Print the string length // Your code here ... - +console.log(string4.length); // b) Print the last character in the string // Your code here ... +console.log(`${string4[string4.length-1]}`);