From 8fd412a1c00f5ac7228ddd88787914ac02eb28db Mon Sep 17 00:00:00 2001 From: shariyerShazan Date: Sat, 11 Apr 2026 07:22:01 +0600 Subject: [PATCH] string & some problem solving --- .../7_string/1_string_not_mutable_.js | 23 ++++++++++++ .../7_string/2_string_details_.js | 36 +++++++++++++++++++ .../7_string/3_check_palindrome_string_.js | 21 +++++++++++ .../7_string/4_toggle_upper_lowwer_.js | 13 +++++++ .../7_string/5_cherecter_frequency_.js | 14 ++++++++ .../7_string/6_cher_freq_with_map_.js | 13 +++++++ node-environment/7_string/7_roman_to_int_.js | 26 ++++++++++++++ .../7_string/8_temperateure_dif_count_.js | 18 ++++++++++ node-environment/7_string/9_char_count_.js | 11 ++++++ 9 files changed, 175 insertions(+) create mode 100644 node-environment/7_string/1_string_not_mutable_.js create mode 100644 node-environment/7_string/2_string_details_.js create mode 100644 node-environment/7_string/3_check_palindrome_string_.js create mode 100644 node-environment/7_string/4_toggle_upper_lowwer_.js create mode 100644 node-environment/7_string/5_cherecter_frequency_.js create mode 100644 node-environment/7_string/6_cher_freq_with_map_.js create mode 100644 node-environment/7_string/7_roman_to_int_.js create mode 100644 node-environment/7_string/8_temperateure_dif_count_.js create mode 100644 node-environment/7_string/9_char_count_.js diff --git a/node-environment/7_string/1_string_not_mutable_.js b/node-environment/7_string/1_string_not_mutable_.js new file mode 100644 index 0000000..7b6da49 --- /dev/null +++ b/node-environment/7_string/1_string_not_mutable_.js @@ -0,0 +1,23 @@ +let arr = [ 0 , 39 , 93 , 45 , 32, 67 , 9 , 56] +arr.push(100) +console.log(arr) // [ 0 , 39 , 93 , 45 , 32, 67 , 9 , 56 , 100] +arr[0] = 100 +console.log(arr)// [ 100 , 39 , 93 , 45 , 32, 67 , 9 , 56 , 100] + + +//! but it's push pop not work in the string + +let name = "shazan" +// name.push("f") //! error +console.log(name) // shazan +console.log(name[0]) // s +console.log(name[4]) // a + +//! concarinate +let firstName = "shariyer" +console.log(firstName + " shazan") // shariyer shazan + +//! or +let fName = "atiya" + fName += " Priya" + console.log(fName) // atiya Priya \ No newline at end of file diff --git a/node-environment/7_string/2_string_details_.js b/node-environment/7_string/2_string_details_.js new file mode 100644 index 0000000..66693e8 --- /dev/null +++ b/node-environment/7_string/2_string_details_.js @@ -0,0 +1,36 @@ +let name = "shazan"; +console.log(name.length); // 6 + +console.log(name.slice(0, 3)); // sha +console.log(name.slice(3, name.length)); // zan +console.log(name.slice(-3, name.length)); // zan + +//! substring +console.log(name.substring(2)); // azan +console.log(name.substring(2, name.length - 1)); // aza +name = name.toUpperCase(); +console.log(name); //SHAZAN +console.log(name.toLowerCase()); // shazan + +console.log(name.concat(" ", "priya")); // SHAZAN priya +console.log(name.concat(" ", "priya").toLowerCase()); // shazan priya + +let me = " shariyer shazan "; +console.log(me); // shariyer shazan +console.log(me.trim()); // shariyer shazan + +name = name.toLowerCase(); +console.log(name.charAt(2)); // a +console.log(name.charCodeAt(2)); // 97 +console.log("a".charCodeAt()); // 97 +console.log("A".charCodeAt()); // 65 + +for (let i = name.length - 1; i >= 0; i--) { + console.log(name.charAt(i)); +} + +let rev = ""; +for (let i = name.length - 1; i >= 0; i--) { + rev += name.charAt(i); +} +console.log(rev) // nazahs \ No newline at end of file diff --git a/node-environment/7_string/3_check_palindrome_string_.js b/node-environment/7_string/3_check_palindrome_string_.js new file mode 100644 index 0000000..8a8e21d --- /dev/null +++ b/node-environment/7_string/3_check_palindrome_string_.js @@ -0,0 +1,21 @@ +import { prompt } from "../4_js_pattern/1_node_prompt_sync_.js"; + +const str = prompt("Enter a string: ").toLowerCase().trim(); + +console.log(str.split("")); +console.log(str.length); + +let isPalindrome = true; +let i = 0, + j = str.length - 1; + +while (i < j) { + if (str[i] !== str[j]) { + isPalindrome = false; + break; + } + i++; + j--; +} + +console.log(isPalindrome ? "It's a palindrome" : "Not a palindrome"); // if mam //? It's a palindrome diff --git a/node-environment/7_string/4_toggle_upper_lowwer_.js b/node-environment/7_string/4_toggle_upper_lowwer_.js new file mode 100644 index 0000000..be44b94 --- /dev/null +++ b/node-environment/7_string/4_toggle_upper_lowwer_.js @@ -0,0 +1,13 @@ +import { prompt } from "../4_js_pattern/1_node_prompt_sync_.js"; + +let name = prompt("Enter a string: ") +console.log(name) // ShAzAn +let toggle = "" +for(let i = 0 ; i= 65 && name.charCodeAt(i) <= 90){ + toggle += name[i].toLowerCase() + } else{ + toggle += name[i].toUpperCase(); + } +} +console.log(toggle); // sHaZaN \ No newline at end of file diff --git a/node-environment/7_string/5_cherecter_frequency_.js b/node-environment/7_string/5_cherecter_frequency_.js new file mode 100644 index 0000000..57066c0 --- /dev/null +++ b/node-environment/7_string/5_cherecter_frequency_.js @@ -0,0 +1,14 @@ +import { prompt } from "../4_js_pattern/1_node_prompt_sync_.js"; + +let str = prompt("Enter a string: ").trim() +let freq = {}; + +for (let ch of str) { + if (freq[ch]) { + freq[ch]++; + } else { + freq[ch] = 1; + } +} + +console.log(freq); diff --git a/node-environment/7_string/6_cher_freq_with_map_.js b/node-environment/7_string/6_cher_freq_with_map_.js new file mode 100644 index 0000000..b76525d --- /dev/null +++ b/node-environment/7_string/6_cher_freq_with_map_.js @@ -0,0 +1,13 @@ +import { prompt } from "../4_js_pattern/1_node_prompt_sync_.js"; + +let str = prompt("Enter a string: ").trim(); +let map = new Map(); + +for (let ch of str) { + map.set(ch, (map.get(ch) || 0) + 1); +} + +console.log(map); + +let head = [2,3,4,52,3 ,45] +head.sort() diff --git a/node-environment/7_string/7_roman_to_int_.js b/node-environment/7_string/7_roman_to_int_.js new file mode 100644 index 0000000..7886c1e --- /dev/null +++ b/node-environment/7_string/7_roman_to_int_.js @@ -0,0 +1,26 @@ +/** + * @param {string} s + * @return {number} + */ +var romanToInt = function (s) { + let map = { + I: 1, + V: 5, + X: 10, + L: 50, + C: 100, + D: 500, + M: 1000, + }; + let total = 0; + for (let i = 0; i < s.length; i++) { + let current = map[s[i]]; + let next = map[s[i + 1]]; + if (current < next) { + total -= current; + } else { + total += current; + } + } + return total; +}; diff --git a/node-environment/7_string/8_temperateure_dif_count_.js b/node-environment/7_string/8_temperateure_dif_count_.js new file mode 100644 index 0000000..a3ce147 --- /dev/null +++ b/node-environment/7_string/8_temperateure_dif_count_.js @@ -0,0 +1,18 @@ +let temperature = [ -4 , -10 , 33 , 54 , "error",34 ,21 , 18 , -12 , -3, 8 , -9 ] + +let tempDiff = (tempArr) => { + let lower = tempArr[0] + let higher = tempArr[0] + + for(let i = 0 ; i< tempArr.length ; i++){ + if(typeof tempArr[i] !== "number"){ continue} + if(lower > tempArr[i]){ + lower = tempArr[i] + } + if(higher < tempArr[i]){ + higher = tempArr[i] + } + } + return higher - lower +} +console.log(tempDiff(temperature)) \ No newline at end of file diff --git a/node-environment/7_string/9_char_count_.js b/node-environment/7_string/9_char_count_.js new file mode 100644 index 0000000..7325bb7 --- /dev/null +++ b/node-environment/7_string/9_char_count_.js @@ -0,0 +1,11 @@ +let charCount = (chars) => { + let char = {} + for(let c of chars){ + if(char[c]){ + char[c]++ + } else { + char[c] == 1 + } + } + return char +} \ No newline at end of file